diff --git a/common.props b/common.props index c20742a..2f9e037 100644 --- a/common.props +++ b/common.props @@ -1,7 +1,7 @@ latest - 2.0.0-rc.21 + 2.0.0-rc.22 $(NoWarn);CS1591 true EasyAbp Team diff --git a/src/Common/EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis/EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis.csproj b/src/Common/EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis/EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis.csproj index de88735..6c39894 100644 --- a/src/Common/EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis/EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis.csproj +++ b/src/Common/EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis/EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis.csproj @@ -7,7 +7,6 @@ EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis ABP vNext微信模块,基于StackExchangeRedis提供多服务间共享access_token等缓存数据的功能扩展。 true - 8 diff --git a/src/Common/EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis/Infrastructure/AccessToken/SharedStackExchangeRedisAccessTokenCache.cs b/src/Common/EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis/Infrastructure/AccessToken/SharedStackExchangeRedisAccessTokenCache.cs new file mode 100644 index 0000000..c2f8354 --- /dev/null +++ b/src/Common/EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis/Infrastructure/AccessToken/SharedStackExchangeRedisAccessTokenCache.cs @@ -0,0 +1,53 @@ +using System; +using System.Threading.Tasks; +using EasyAbp.Abp.WeChat.Common.Infrastructure.AccessToken; +using EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis.Settings; +using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.Caching.StackExchangeRedis; +using Volo.Abp.Caching.StackExchangeRedis; +using Volo.Abp.Settings; + +namespace EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis.Infrastructure.AccessToken; + +public class SharedStackExchangeRedisAccessTokenCache : IAccessTokenCache +{ + public static string CachePrefix { get; set; } = "WeChatTokens:"; + public static string SettingName { get; set; } = SharedCacheStackExchangeRedisSettings.RedisConfiguration; + + private readonly ISettingProvider _settingProvider; + + public SharedStackExchangeRedisAccessTokenCache(ISettingProvider settingProvider) + { + _settingProvider = settingProvider; + } + + public virtual async Task GetOrNullAsync(string key) + { + var redisCache = await CreateAbpRedisCacheAsync(); + + return await redisCache.GetStringAsync(await GetKeyAsync(key)); + } + + public virtual async Task SetAsync(string key, string value) + { + var redisCache = await CreateAbpRedisCacheAsync(); + + await redisCache.SetStringAsync(await GetKeyAsync(key), value, new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(115) + }); + } + + protected virtual Task GetKeyAsync(string key) + { + return Task.FromResult($"{CachePrefix}{key}"); + } + + protected virtual async Task CreateAbpRedisCacheAsync() + { + return new AbpRedisCache(new RedisCacheOptions + { + Configuration = await _settingProvider.GetOrNullAsync(SettingName) + }); + } +} \ No newline at end of file diff --git a/src/Common/EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis/Infrastructure/AccessToken/SharedStackExchangeRedisAccessTokenProvider.cs b/src/Common/EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis/Infrastructure/AccessToken/SharedStackExchangeRedisAccessTokenProvider.cs deleted file mode 100644 index 144c5aa..0000000 --- a/src/Common/EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis/Infrastructure/AccessToken/SharedStackExchangeRedisAccessTokenProvider.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System; -using System.Threading.Tasks; -using EasyAbp.Abp.WeChat.Common.Infrastructure.AccessToken; -using EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis.Settings; -using Microsoft.Extensions.Caching.Distributed; -using Microsoft.Extensions.Caching.StackExchangeRedis; -using Microsoft.Extensions.DependencyInjection; -using Volo.Abp.Caching.StackExchangeRedis; -using Volo.Abp.DependencyInjection; -using Volo.Abp.Settings; - -namespace EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis.Infrastructure.AccessToken -{ - [Dependency(ServiceLifetime.Transient, ReplaceServices = true)] - public class SharedStackExchangeRedisAccessTokenProvider : IAccessTokenCache - { - public const string CachePrefix = "CurrentAccessToken:"; - public const string SettingName = SharedCacheStackExchangeRedisSettings.RedisConfiguration; - - private readonly ISettingProvider _settingProvider; - - public SharedStackExchangeRedisAccessTokenProvider(ISettingProvider settingProvider) - { - _settingProvider = settingProvider; - } - - public virtual async Task GetOrNullAsync(string key) - { - var redisCache = await CreateAbpRedisCacheAsync(); - - return await redisCache.GetStringAsync($"{CachePrefix}{key}"); - } - - public virtual async Task SetAsync(string key, string value) - { - var redisCache = await CreateAbpRedisCacheAsync(); - - await redisCache.SetStringAsync(key, value, new DistributedCacheEntryOptions - { - AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(115) - }); - } - - protected virtual async Task CreateAbpRedisCacheAsync() - { - return new AbpRedisCache(new RedisCacheOptions - { - Configuration = await _settingProvider.GetOrNullAsync(SettingName) - }); - } - } -} \ No newline at end of file diff --git a/src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/AccessTokenCache.cs b/src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/DefaultAccessTokenCache.cs similarity index 81% rename from src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/AccessTokenCache.cs rename to src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/DefaultAccessTokenCache.cs index 67c9d51..6ca0a52 100644 --- a/src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/AccessTokenCache.cs +++ b/src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/DefaultAccessTokenCache.cs @@ -6,11 +6,11 @@ namespace EasyAbp.Abp.WeChat.Common.Infrastructure.AccessToken; -public class AccessTokenCache : IAccessTokenCache, ITransientDependency +public class DefaultAccessTokenCache : IAccessTokenCache, ITransientDependency { protected IDistributedCache DistributedCache { get; } - public AccessTokenCache(IDistributedCache distributedCache) + public DefaultAccessTokenCache(IDistributedCache distributedCache) { DistributedCache = distributedCache; }