Use Cache/Uncache instead of OnCached/OnUncached

This commit is contained in:
RogueException
2015-10-25 03:05:39 -03:00
parent 84bfaa82e7
commit 0086537872
7 changed files with 9 additions and 12 deletions

View File

@@ -9,7 +9,7 @@ namespace Discord
internal sealed class Channels : AsyncCollection<Channel>
{
public Channels(DiscordClient client, object writerLock)
: base(client, writerLock, x => x.OnCached(), x => x.OnUncached()) { }
: base(client, writerLock) { }
public Channel GetOrAdd(string id, string serverId, string recipientId = null)
=> GetOrAdd(id, () => new Channel(_client, id, serverId, recipientId));

View File

@@ -8,7 +8,7 @@ namespace Discord
internal sealed class Users : AsyncCollection<User>
{
public Users(DiscordClient client, object writerLock)
: base(client, writerLock, x => x.OnCached(), x => x.OnUncached()) { }
: base(client, writerLock) { }
private string GetKey(string userId, string serverId)
=> User.GetId(userId, serverId);

View File

@@ -13,7 +13,7 @@ namespace Discord
private bool _isEnabled;
public Messages(DiscordClient client, object writerLock, bool isEnabled)
: base(client, writerLock, x => x.OnCached(), x => x.OnUncached())
: base(client, writerLock)
{
_isEnabled = isEnabled;
}

View File

@@ -10,7 +10,7 @@ namespace Discord
public Role VirtualEveryone { get; private set; }
public Roles(DiscordClient client, object writerLock)
: base(client, writerLock, x => x.OnCached(), x => x.OnUncached())
: base(client, writerLock)
{
VirtualEveryone = new Role(client, "Private", null);
}

View File

@@ -9,7 +9,7 @@ namespace Discord
internal sealed class Servers : AsyncCollection<Server>
{
public Servers(DiscordClient client, object writerLock)
: base(client, writerLock, x => x.OnCached(), x => x.OnUncached()) { }
: base(client, writerLock) { }
public Server GetOrAdd(string id)
=> GetOrAdd(id, () => new Server(_client, id));

View File

@@ -9,7 +9,7 @@ namespace Discord
internal sealed class GlobalUsers : AsyncCollection<GlobalUser>
{
public GlobalUsers(DiscordClient client, object writerLock)
: base(client, writerLock, x => x.OnCached(), x => x.OnUncached()) { }
: base(client, writerLock) { }
public GlobalUser GetOrAdd(string id) => GetOrAdd(id, () => new GlobalUser(_client, id));
}

View File

@@ -52,15 +52,12 @@ namespace Discord
protected readonly DiscordClient _client;
protected readonly ConcurrentDictionary<string, TValue> _dictionary;
private readonly Action<TValue> _onCache, _onUncache;
protected AsyncCollection(DiscordClient client, object writerLock, Action<TValue> onCache, Action<TValue> onUncache)
protected AsyncCollection(DiscordClient client, object writerLock)
{
_client = client;
_writerLock = writerLock;
_dictionary = new ConcurrentDictionary<string, TValue>();
_onCache = onCache;
_onUncache = onUncache;
}
public TValue this[string key]
@@ -88,7 +85,7 @@ namespace Discord
result = _dictionary.GetOrAdd(key, newItem);
if (result == newItem)
{
_onCache(result);
result.Cache();
RaiseItemCreated(result);
}
}
@@ -103,7 +100,7 @@ namespace Discord
TValue result;
if (_dictionary.TryRemove(key, out result))
{
_onUncache(result); //TODO: If this object is accessed before OnRemoved finished firing, properties such as Server.Channels will have null elements
result.Uncache(); //TODO: If this object is accessed before OnRemoved finished firing, properties such as Server.Channels will have null elements
return result;
}
}