Use Cache/Uncache instead of OnCached/OnUncached
This commit is contained in:
@@ -9,7 +9,7 @@ namespace Discord
|
|||||||
internal sealed class Channels : AsyncCollection<Channel>
|
internal sealed class Channels : AsyncCollection<Channel>
|
||||||
{
|
{
|
||||||
public Channels(DiscordClient client, object writerLock)
|
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)
|
public Channel GetOrAdd(string id, string serverId, string recipientId = null)
|
||||||
=> GetOrAdd(id, () => new Channel(_client, id, serverId, recipientId));
|
=> GetOrAdd(id, () => new Channel(_client, id, serverId, recipientId));
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ namespace Discord
|
|||||||
internal sealed class Users : AsyncCollection<User>
|
internal sealed class Users : AsyncCollection<User>
|
||||||
{
|
{
|
||||||
public Users(DiscordClient client, object writerLock)
|
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)
|
private string GetKey(string userId, string serverId)
|
||||||
=> User.GetId(userId, serverId);
|
=> User.GetId(userId, serverId);
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ namespace Discord
|
|||||||
private bool _isEnabled;
|
private bool _isEnabled;
|
||||||
|
|
||||||
public Messages(DiscordClient client, object writerLock, bool isEnabled)
|
public Messages(DiscordClient client, object writerLock, bool isEnabled)
|
||||||
: base(client, writerLock, x => x.OnCached(), x => x.OnUncached())
|
: base(client, writerLock)
|
||||||
{
|
{
|
||||||
_isEnabled = isEnabled;
|
_isEnabled = isEnabled;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ namespace Discord
|
|||||||
public Role VirtualEveryone { get; private set; }
|
public Role VirtualEveryone { get; private set; }
|
||||||
|
|
||||||
public Roles(DiscordClient client, object writerLock)
|
public Roles(DiscordClient client, object writerLock)
|
||||||
: base(client, writerLock, x => x.OnCached(), x => x.OnUncached())
|
: base(client, writerLock)
|
||||||
{
|
{
|
||||||
VirtualEveryone = new Role(client, "Private", null);
|
VirtualEveryone = new Role(client, "Private", null);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ namespace Discord
|
|||||||
internal sealed class Servers : AsyncCollection<Server>
|
internal sealed class Servers : AsyncCollection<Server>
|
||||||
{
|
{
|
||||||
public Servers(DiscordClient client, object writerLock)
|
public Servers(DiscordClient client, object writerLock)
|
||||||
: base(client, writerLock, x => x.OnCached(), x => x.OnUncached()) { }
|
: base(client, writerLock) { }
|
||||||
|
|
||||||
public Server GetOrAdd(string id)
|
public Server GetOrAdd(string id)
|
||||||
=> GetOrAdd(id, () => new Server(_client, id));
|
=> GetOrAdd(id, () => new Server(_client, id));
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ namespace Discord
|
|||||||
internal sealed class GlobalUsers : AsyncCollection<GlobalUser>
|
internal sealed class GlobalUsers : AsyncCollection<GlobalUser>
|
||||||
{
|
{
|
||||||
public GlobalUsers(DiscordClient client, object writerLock)
|
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));
|
public GlobalUser GetOrAdd(string id) => GetOrAdd(id, () => new GlobalUser(_client, id));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,15 +52,12 @@ namespace Discord
|
|||||||
|
|
||||||
protected readonly DiscordClient _client;
|
protected readonly DiscordClient _client;
|
||||||
protected readonly ConcurrentDictionary<string, TValue> _dictionary;
|
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;
|
_client = client;
|
||||||
_writerLock = writerLock;
|
_writerLock = writerLock;
|
||||||
_dictionary = new ConcurrentDictionary<string, TValue>();
|
_dictionary = new ConcurrentDictionary<string, TValue>();
|
||||||
_onCache = onCache;
|
|
||||||
_onUncache = onUncache;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public TValue this[string key]
|
public TValue this[string key]
|
||||||
@@ -88,7 +85,7 @@ namespace Discord
|
|||||||
result = _dictionary.GetOrAdd(key, newItem);
|
result = _dictionary.GetOrAdd(key, newItem);
|
||||||
if (result == newItem)
|
if (result == newItem)
|
||||||
{
|
{
|
||||||
_onCache(result);
|
result.Cache();
|
||||||
RaiseItemCreated(result);
|
RaiseItemCreated(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -103,7 +100,7 @@ namespace Discord
|
|||||||
TValue result;
|
TValue result;
|
||||||
if (_dictionary.TryRemove(key, out 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;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user