Skip to content

Commit

Permalink
Merge pull request #6507 from aspnetboilerplate/feat/redis-online-cli…
Browse files Browse the repository at this point in the history
…ent-store

Implemented Redis online client store
  • Loading branch information
ismcagdas committed Jul 29, 2022
2 parents 9fc31e7 + 5e7ec14 commit da325e0
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public class AbpRedisCacheOptions

public int DatabaseId { get; set; }

public string OnlineClientsStoreKey = "Abp.RealTime.OnlineClients";

public AbpRedisCacheOptions(IAbpStartupConfiguration abpStartupConfiguration)
{
AbpStartupConfiguration = abpStartupConfiguration;
Expand Down
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();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using Abp.Dependency;
using Abp.RealTime;
using Abp.Runtime.Caching.Configuration;
using Abp.Runtime.Caching.Redis.RealTime;

namespace Abp.Runtime.Caching.Redis
{
Expand Down Expand Up @@ -28,7 +30,9 @@ public static void UseRedis(this ICachingConfiguration cachingConfiguration, Act
var iocManager = cachingConfiguration.AbpConfiguration.IocManager;

iocManager.RegisterIfNot<ICacheManager, AbpRedisCacheManager>();

iocManager.RegisterIfNot<IOnlineClientStore, RedisOnlineClientStore>();
iocManager.RegisterIfNot(typeof(IOnlineClientStore<>), typeof(RedisOnlineClientStore<>));

optionsAction(iocManager.Resolve<AbpRedisCacheOptions>());
}
}
Expand Down

0 comments on commit da325e0

Please sign in to comment.