Skip to content

Commit

Permalink
Merge pull request #287 from Cysharp/improve-redisgroup
Browse files Browse the repository at this point in the history
Add RedisGroupOptions
  • Loading branch information
neuecc authored Mar 10, 2020
2 parents abd5fc5 + 99c58cf commit 802d2fe
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/MagicOnion.GeneratorCore/MagicOnion.GeneratorCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
</PropertyGroup>
<ItemGroup>
<!-- Keep the same version with MessagePack, MasterMemory -->
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="3.1.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="3.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Workspaces.Common" Version="3.4.0" />
<PackageReference Include="Microsoft.Extensions.FileSystemGlobbing" Version="3.0.1" />
<PackageReference Include="System.CodeDom" Version="4.5.0" />
</ItemGroup>
Expand Down
38 changes: 29 additions & 9 deletions src/MagicOnion.Redis/RedisGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,35 @@

namespace MagicOnion.Redis
{
public class RedisGroupOptions
{
public ConnectionMultiplexer ConnectionMultiplexer { get; }
public int Db { get; }

public RedisGroupOptions(ConnectionMultiplexer connectionMultiplexer, int db = -1)
{
ConnectionMultiplexer = connectionMultiplexer;
Db = db;
}
}

public class RedisGroupRepositoryFactory : IGroupRepositoryFactory
{
public IGroupRepository CreateRepository(MessagePackSerializerOptions serializerOptions, IMagicOnionLogger logger, IServiceLocator serviceLocator)
{
var connection = serviceLocator.GetService<ConnectionMultiplexer>();
if (connection == null)
var options = serviceLocator.GetService<RedisGroupOptions>();
if (options == null)
{
throw new InvalidOperationException("RedisGroup requires add ConnectionMultiplexer to MagicOnionOptions.ServiceLocator before create it. Please try new MagicOnionOptions{DefaultServiceLocator.Register(new ConnectionMultiplexer)}");
var connection = serviceLocator.GetService<ConnectionMultiplexer>();
if (connection == null)
{
throw new InvalidOperationException("RedisGroup requires add ConnectionMultiplexer to MagicOnionOptions.ServiceLocator before create it. Please try new MagicOnionOptions{DefaultServiceLocator.Register(new ConnectionMultiplexer)}");
}

options = new RedisGroupOptions(connection);
}

return new RedisGroupRepository(serializerOptions, connection, logger);
return new RedisGroupRepository(serializerOptions, options, logger);
}
}

Expand All @@ -28,16 +46,18 @@ public class RedisGroupRepository : IGroupRepository
MessagePackSerializerOptions serializerOptions;
IMagicOnionLogger logger;
ConnectionMultiplexer connection;
int db;

readonly Func<string, IGroup> factory;
ConcurrentDictionary<string, IGroup> dictionary = new ConcurrentDictionary<string, IGroup>();

public RedisGroupRepository(MessagePackSerializerOptions serializerOptions, ConnectionMultiplexer connection, IMagicOnionLogger logger)
public RedisGroupRepository(MessagePackSerializerOptions serializerOptions, RedisGroupOptions redisGroupOptions, IMagicOnionLogger logger)
{
this.serializerOptions = serializerOptions;
this.logger = logger;
this.factory = CreateGroup;
this.connection = connection;
this.connection = redisGroupOptions.ConnectionMultiplexer;
this.db = redisGroupOptions.Db;
}

public IGroup GetOrAdd(string groupName)
Expand All @@ -47,7 +67,7 @@ public IGroup GetOrAdd(string groupName)

IGroup CreateGroup(string groupName)
{
return new RedisGroup(groupName, serializerOptions, new ConcurrentDictionaryGroup(groupName, this, serializerOptions, logger), connection.GetSubscriber(), connection.GetDatabase());
return new RedisGroup(groupName, serializerOptions, new ConcurrentDictionaryGroup(groupName, this, serializerOptions, logger), connection.GetSubscriber(), connection.GetDatabase(db));
}

public bool TryGet(string groupName, out IGroup group)
Expand Down Expand Up @@ -142,9 +162,9 @@ static Task PublishFromRedisToMemoryGroup(RedisValue value, IGroup group)
return Task.CompletedTask;
}

public ValueTask<int> GetMemberCountAsync()
public async ValueTask<int> GetMemberCountAsync()
{
throw new NotImplementedException();
return (int)await database.StringGetAsync(counterKey);
}

public Task WriteAllAsync<T>(int methodId, T value, bool fireAndForget)
Expand Down

0 comments on commit 802d2fe

Please sign in to comment.