MemoryCache
This commit is contained in:
parent
3fd3160c0c
commit
0f91ae168d
|
|
@ -180,6 +180,7 @@
|
|||
<PackageReference Include="DistributedLock.Redis" Version="1.0.3" />
|
||||
<PackageReference Include="EntityFrameworkCore.BootKit" Version="8.5.1" />
|
||||
<PackageReference Include="Fluid.Core" Version="2.11.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="8.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0" />
|
||||
<PackageReference Include="Nanoid" Version="3.1.0" />
|
||||
</ItemGroup>
|
||||
|
|
|
|||
|
|
@ -30,8 +30,10 @@ public static class BotSharpCoreExtensions
|
|||
var cacheSettings = new SharpCacheSettings();
|
||||
config.Bind("SharpCache", cacheSettings);
|
||||
services.AddSingleton(x => cacheSettings);
|
||||
services.AddSingleton<ICacheService, CacheService>();
|
||||
|
||||
services.AddSingleton<ICacheService, RedisCacheService>();
|
||||
|
||||
services.AddMemoryCache();
|
||||
|
||||
RegisterPlugins(services, config);
|
||||
ConfigureBotSharpOptions(services, configOptions);
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,35 @@
|
|||
using BotSharp.Abstraction.Infrastructures;
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace BotSharp.Core.Infrastructures;
|
||||
|
||||
public class MemoryCacheService : ICacheService
|
||||
{
|
||||
private static IMemoryCache _cache = new MemoryCache(new MemoryCacheOptions
|
||||
{
|
||||
});
|
||||
private readonly BotSharpDatabaseSettings _settings;
|
||||
|
||||
public MemoryCacheService(BotSharpDatabaseSettings settings)
|
||||
{
|
||||
_settings = settings;
|
||||
}
|
||||
|
||||
public async Task<T?> GetAsync<T>(string key)
|
||||
{
|
||||
return (T?)(_cache.Get(key) ?? default(T));
|
||||
}
|
||||
|
||||
public async Task<object> GetAsync(string key, Type type)
|
||||
{
|
||||
return _cache.Get(key) ?? default;
|
||||
}
|
||||
|
||||
public async Task SetAsync<T>(string key, T value, TimeSpan? expiry)
|
||||
{
|
||||
_cache.Set(key, value, new MemoryCacheEntryOptions
|
||||
{
|
||||
AbsoluteExpirationRelativeToNow = expiry
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -4,12 +4,12 @@ using StackExchange.Redis;
|
|||
|
||||
namespace BotSharp.Core.Infrastructures;
|
||||
|
||||
public class CacheService : ICacheService
|
||||
public class RedisCacheService : ICacheService
|
||||
{
|
||||
private readonly BotSharpDatabaseSettings _settings;
|
||||
private static ConnectionMultiplexer redis = null!;
|
||||
|
||||
public CacheService(BotSharpDatabaseSettings settings)
|
||||
public RedisCacheService(BotSharpDatabaseSettings settings)
|
||||
{
|
||||
_settings = settings;
|
||||
}
|
||||
|
|
@ -68,6 +68,11 @@ public class CacheService : ICacheService
|
|||
return;
|
||||
}
|
||||
|
||||
if (redis == null)
|
||||
{
|
||||
redis = ConnectionMultiplexer.Connect(_settings.Redis);
|
||||
}
|
||||
|
||||
var db = redis.GetDatabase();
|
||||
await db.StringSetAsync(key, JsonConvert.SerializeObject(value), expiry);
|
||||
}
|
||||
Loading…
Reference in a new issue