diff --git a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj index ff10a517..99f8b6ae 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj +++ b/src/Infrastructure/BotSharp.Core/BotSharp.Core.csproj @@ -180,6 +180,7 @@ + diff --git a/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs b/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs index 20088981..26a5ccb5 100644 --- a/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs +++ b/src/Infrastructure/BotSharp.Core/BotSharpCoreExtensions.cs @@ -30,8 +30,10 @@ public static class BotSharpCoreExtensions var cacheSettings = new SharpCacheSettings(); config.Bind("SharpCache", cacheSettings); services.AddSingleton(x => cacheSettings); - services.AddSingleton(); - + services.AddSingleton(); + + services.AddMemoryCache(); + RegisterPlugins(services, config); ConfigureBotSharpOptions(services, configOptions); diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/MemoryCacheService.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/MemoryCacheService.cs new file mode 100644 index 00000000..2d2fe13b --- /dev/null +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/MemoryCacheService.cs @@ -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 GetAsync(string key) + { + return (T?)(_cache.Get(key) ?? default(T)); + } + + public async Task GetAsync(string key, Type type) + { + return _cache.Get(key) ?? default; + } + + public async Task SetAsync(string key, T value, TimeSpan? expiry) + { + _cache.Set(key, value, new MemoryCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = expiry + }); + } +} diff --git a/src/Infrastructure/BotSharp.Core/Infrastructures/CacheService.cs b/src/Infrastructure/BotSharp.Core/Infrastructures/RedisCacheService.cs similarity index 87% rename from src/Infrastructure/BotSharp.Core/Infrastructures/CacheService.cs rename to src/Infrastructure/BotSharp.Core/Infrastructures/RedisCacheService.cs index 45767fc2..279f9014 100644 --- a/src/Infrastructure/BotSharp.Core/Infrastructures/CacheService.cs +++ b/src/Infrastructure/BotSharp.Core/Infrastructures/RedisCacheService.cs @@ -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); }