-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6507 from aspnetboilerplate/feat/redis-online-cli…
…ent-store Implemented Redis online client store
- Loading branch information
Showing
3 changed files
with
130 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
src/Abp.RedisCache/Runtime/Caching/Redis/RealTime/RedisOnlineClientStore.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using Abp.Dependency; | ||
using Abp.RealTime; | ||
using Newtonsoft.Json; | ||
using StackExchange.Redis; | ||
|
||
namespace Abp.Runtime.Caching.Redis.RealTime | ||
{ | ||
public class RedisOnlineClientStore<T> : RedisOnlineClientStore, IOnlineClientStore<T> | ||
{ | ||
public RedisOnlineClientStore( | ||
IAbpRedisCacheDatabaseProvider database, | ||
AbpRedisCacheOptions options) : base(database, options) | ||
{ | ||
} | ||
} | ||
|
||
public class RedisOnlineClientStore : IOnlineClientStore, ISingletonDependency | ||
{ | ||
private readonly IAbpRedisCacheDatabaseProvider _database; | ||
|
||
private readonly string _clientStoreKey; | ||
|
||
public RedisOnlineClientStore( | ||
IAbpRedisCacheDatabaseProvider database, | ||
AbpRedisCacheOptions options) | ||
{ | ||
_database = database; | ||
|
||
_clientStoreKey = options.OnlineClientsStoreKey + ".Clients"; | ||
} | ||
|
||
public void Add(IOnlineClient client) | ||
{ | ||
var database = GetDatabase(); | ||
database.HashSet(_clientStoreKey, new[] | ||
{ | ||
new HashEntry(client.ConnectionId, client.ToString()) | ||
}); | ||
} | ||
|
||
public bool Remove(string connectionId) | ||
{ | ||
var database = GetDatabase(); | ||
|
||
var clientValue = database.HashGet(_clientStoreKey, connectionId); | ||
if (clientValue.IsNullOrEmpty) | ||
{ | ||
return true; | ||
} | ||
|
||
database.HashDelete(_clientStoreKey, connectionId); | ||
return true; | ||
} | ||
|
||
public bool TryRemove(string connectionId, out IOnlineClient client) | ||
{ | ||
try | ||
{ | ||
var database = GetDatabase(); | ||
|
||
var clientValue = database.HashGet(_clientStoreKey, connectionId); | ||
if (clientValue.IsNullOrEmpty) | ||
{ | ||
client = null; | ||
return true; | ||
} | ||
|
||
client = JsonConvert.DeserializeObject<OnlineClient>(clientValue); | ||
|
||
database.HashDelete(_clientStoreKey, connectionId); | ||
return true; | ||
} | ||
catch (Exception e) | ||
{ | ||
Console.WriteLine(e); | ||
client = null; | ||
return false; | ||
} | ||
} | ||
|
||
public bool TryGet(string connectionId, out IOnlineClient client) | ||
{ | ||
var database = GetDatabase(); | ||
var clientValue = database.HashGet(_clientStoreKey, connectionId); | ||
if (clientValue.IsNullOrEmpty) | ||
{ | ||
client = null; | ||
return false; | ||
} | ||
|
||
client = JsonConvert.DeserializeObject<OnlineClient>(clientValue); | ||
return true; | ||
} | ||
|
||
public bool Contains(string connectionId) | ||
{ | ||
var database = GetDatabase(); | ||
var clientValue = database.HashGet(_clientStoreKey, connectionId); | ||
return !clientValue.IsNullOrEmpty; | ||
} | ||
|
||
public IReadOnlyList<IOnlineClient> GetAll() | ||
{ | ||
var database = GetDatabase(); | ||
var clientsEntries = database.HashGetAll(_clientStoreKey); | ||
var clients = new List<IOnlineClient>(); | ||
foreach (var entry in clientsEntries) | ||
{ | ||
clients.Add(JsonConvert.DeserializeObject<OnlineClient>(entry.Value)); | ||
} | ||
|
||
return clients.ToImmutableList(); | ||
} | ||
|
||
private IDatabase GetDatabase() | ||
{ | ||
return _database.GetDatabase(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters