Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented Redis online client store #6507

Merged
merged 2 commits into from Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
@@ -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();
}
}
}
@@ -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