From 0644bc8f6475d52cf45bf329afd480fb9a63be5f Mon Sep 17 00:00:00 2001 From: gdlcf88 Date: Fri, 13 Jan 2023 13:56:30 +0800 Subject: [PATCH] Share tokens and tickets with `IAbpWeChatSharableCache` --- common.props | 2 +- ...ackExchangeRedisAbpWeChatSharableCache.cs} | 19 ++++---- ...he.cs => DefaultAbpWeChatSharableCache.cs} | 12 ++--- .../AccessToken/DefaultAccessTokenProvider.cs | 14 +++--- ...kenCache.cs => IAbpWeChatSharableCache.cs} | 5 ++- .../JsTickets/DefaultJsTicketProvider.cs | 44 +++++++++++-------- .../AccessToken/AuthorizerAccessTokenCache.cs | 18 +++++--- .../CacheAuthorizerRefreshTokenStore.cs | 12 ++--- .../DefaultComponentAccessTokenProvider.cs | 32 +++++++++----- .../CacheComponentVerifyTicketStore.cs | 8 ++-- .../HybridAccessTokenProviderTests.cs | 8 ++-- 11 files changed, 103 insertions(+), 71 deletions(-) rename src/Common/EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis/Infrastructure/AccessToken/{SharedStackExchangeRedisAccessTokenCache.cs => SharedStackExchangeRedisAbpWeChatSharableCache.cs} (71%) rename src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/{DefaultAccessTokenCache.cs => DefaultAbpWeChatSharableCache.cs} (54%) rename src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/{IAccessTokenCache.cs => IAbpWeChatSharableCache.cs} (65%) diff --git a/common.props b/common.props index 086e146..b014efc 100644 --- a/common.props +++ b/common.props @@ -1,7 +1,7 @@ latest - 2.2.0 + 2.3.0 $(NoWarn);CS1591 true EasyAbp Team 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/SharedStackExchangeRedisAbpWeChatSharableCache.cs similarity index 71% rename from src/Common/EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis/Infrastructure/AccessToken/SharedStackExchangeRedisAccessTokenCache.cs rename to src/Common/EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis/Infrastructure/AccessToken/SharedStackExchangeRedisAbpWeChatSharableCache.cs index 34288b2..8a4d7ef 100644 --- a/src/Common/EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis/Infrastructure/AccessToken/SharedStackExchangeRedisAccessTokenCache.cs +++ b/src/Common/EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis/Infrastructure/AccessToken/SharedStackExchangeRedisAbpWeChatSharableCache.cs @@ -1,5 +1,4 @@ -using System; -using System.Threading.Tasks; +using System.Threading.Tasks; using EasyAbp.Abp.WeChat.Common.Infrastructure.AccessToken; using EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis.Settings; using Microsoft.Extensions.Caching.Distributed; @@ -10,14 +9,14 @@ namespace EasyAbp.Abp.WeChat.Common.SharedCache.StackExchangeRedis.Infrastructure.AccessToken; -public class SharedStackExchangeRedisAccessTokenCache : IAccessTokenCache, ITransientDependency +public class SharedStackExchangeRedisAbpWeChatSharableCache : IAbpWeChatSharableCache, ITransientDependency { public static string CachePrefix { get; set; } = "WeChatTokens:"; public static string SettingName { get; set; } = SharedCacheStackExchangeRedisSettings.RedisConfiguration; private readonly ISettingProvider _settingProvider; - public SharedStackExchangeRedisAccessTokenCache(ISettingProvider settingProvider) + public SharedStackExchangeRedisAbpWeChatSharableCache(ISettingProvider settingProvider) { _settingProvider = settingProvider; } @@ -29,14 +28,18 @@ public virtual async Task GetOrNullAsync(string key) return await redisCache.GetStringAsync(await GetKeyAsync(key)); } - public virtual async Task SetAsync(string key, string value) + public virtual async Task SetAsync(string key, string value, DistributedCacheEntryOptions options) { var redisCache = await CreateAbpRedisCacheAsync(); - await redisCache.SetStringAsync(await GetKeyAsync(key), value, new DistributedCacheEntryOptions + if (value is null) { - AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(115) - }); + await redisCache.RemoveAsync(await GetKeyAsync(key)); + } + else + { + await redisCache.SetStringAsync(await GetKeyAsync(key), value, options); + } } protected virtual Task GetKeyAsync(string key) diff --git a/src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/DefaultAccessTokenCache.cs b/src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/DefaultAbpWeChatSharableCache.cs similarity index 54% rename from src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/DefaultAccessTokenCache.cs rename to src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/DefaultAbpWeChatSharableCache.cs index 43b6d8d..9ac51ac 100644 --- a/src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/DefaultAccessTokenCache.cs +++ b/src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/DefaultAbpWeChatSharableCache.cs @@ -1,4 +1,3 @@ -using System; using System.Threading.Tasks; using Microsoft.Extensions.Caching.Distributed; using Volo.Abp.Caching; @@ -7,11 +6,11 @@ namespace EasyAbp.Abp.WeChat.Common.Infrastructure.AccessToken; [Dependency(TryRegister = true)] -public class DefaultAccessTokenCache : IAccessTokenCache, ITransientDependency +public class DefaultAbpWeChatSharableCache : IAbpWeChatSharableCache, ITransientDependency { protected IDistributedCache DistributedCache { get; } - public DefaultAccessTokenCache(IDistributedCache distributedCache) + public DefaultAbpWeChatSharableCache(IDistributedCache distributedCache) { DistributedCache = distributedCache; } @@ -21,11 +20,8 @@ public virtual Task GetOrNullAsync(string key) return DistributedCache.GetAsync(key); } - public virtual Task SetAsync(string key, string value) + public virtual Task SetAsync(string key, string value, DistributedCacheEntryOptions options) { - return DistributedCache.SetAsync(key, value, new DistributedCacheEntryOptions - { - AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(115) - }); + return DistributedCache.SetAsync(key, value, options); } } \ No newline at end of file diff --git a/src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/DefaultAccessTokenProvider.cs b/src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/DefaultAccessTokenProvider.cs index 5e2166b..bec2fe4 100644 --- a/src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/DefaultAccessTokenProvider.cs +++ b/src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/DefaultAccessTokenProvider.cs @@ -1,6 +1,7 @@ using System; using System.Net.Http; using System.Threading.Tasks; +using Microsoft.Extensions.Caching.Distributed; using Newtonsoft.Json.Linq; using Volo.Abp.DependencyInjection; @@ -8,14 +9,14 @@ namespace EasyAbp.Abp.WeChat.Common.Infrastructure.AccessToken { public class DefaultAccessTokenProvider : IAccessTokenProvider, ITransientDependency { - private readonly IAccessTokenCache _accessTokenCache; + private readonly IAbpWeChatSharableCache _abpWeChatSharableCache; private readonly IHttpClientFactory _httpClientFactory; public DefaultAccessTokenProvider( - IAccessTokenCache accessTokenCache, + IAbpWeChatSharableCache abpWeChatSharableCache, IHttpClientFactory httpClientFactory) { - _accessTokenCache = accessTokenCache; + _abpWeChatSharableCache = abpWeChatSharableCache; _httpClientFactory = httpClientFactory; } @@ -23,13 +24,16 @@ public virtual async Task GetAsync(string appId, string appSecret) { var cacheKey = await GetCacheKeyAsync(appId); - var token = await _accessTokenCache.GetOrNullAsync(cacheKey); + var token = await _abpWeChatSharableCache.GetOrNullAsync(cacheKey); if (token.IsNullOrWhiteSpace()) { token = await RequestAccessTokenAsync(appId, appSecret); - await _accessTokenCache.SetAsync(cacheKey, token); + await _abpWeChatSharableCache.SetAsync(cacheKey, token, new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(115) + }); } return token; diff --git a/src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/IAccessTokenCache.cs b/src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/IAbpWeChatSharableCache.cs similarity index 65% rename from src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/IAccessTokenCache.cs rename to src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/IAbpWeChatSharableCache.cs index 3443b25..4cde689 100644 --- a/src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/IAccessTokenCache.cs +++ b/src/Common/EasyAbp.Abp.WeChat.Common/Infrastructure/AccessToken/IAbpWeChatSharableCache.cs @@ -1,11 +1,12 @@ using System.Threading.Tasks; using JetBrains.Annotations; +using Microsoft.Extensions.Caching.Distributed; namespace EasyAbp.Abp.WeChat.Common.Infrastructure.AccessToken; -public interface IAccessTokenCache +public interface IAbpWeChatSharableCache { Task GetOrNullAsync([NotNull] string key); - Task SetAsync([NotNull] string key, [CanBeNull] string value); + Task SetAsync([NotNull] string key, [CanBeNull] string value, DistributedCacheEntryOptions options); } \ No newline at end of file diff --git a/src/Official/EasyAbp.Abp.WeChat.Official/JsTickets/DefaultJsTicketProvider.cs b/src/Official/EasyAbp.Abp.WeChat.Official/JsTickets/DefaultJsTicketProvider.cs index e45d85e..8960d77 100644 --- a/src/Official/EasyAbp.Abp.WeChat.Official/JsTickets/DefaultJsTicketProvider.cs +++ b/src/Official/EasyAbp.Abp.WeChat.Official/JsTickets/DefaultJsTicketProvider.cs @@ -7,7 +7,6 @@ using EasyAbp.Abp.WeChat.Official.Options; using Microsoft.Extensions.Caching.Distributed; using Newtonsoft.Json.Linq; -using Volo.Abp.Caching; using Volo.Abp.DependencyInjection; namespace EasyAbp.Abp.WeChat.Official.JsTickets @@ -17,45 +16,42 @@ public class DefaultJsTicketProvider : IJsTicketProvider, ITransientDependency private readonly IHttpClientFactory _httpClientFactory; private readonly IAccessTokenProvider _accessTokenProvider; private readonly IAbpWeChatOptionsProvider _optionsProvider; - private readonly IDistributedCache _distributedCache; + private readonly IAbpWeChatSharableCache _cache; public DefaultJsTicketProvider( IHttpClientFactory httpClientFactory, IAccessTokenProvider accessTokenProvider, IAbpWeChatOptionsProvider optionsProvider, - IDistributedCache distributedCache) + IAbpWeChatSharableCache cache) { _httpClientFactory = httpClientFactory; _accessTokenProvider = accessTokenProvider; _optionsProvider = optionsProvider; - _distributedCache = distributedCache; + _cache = cache; } public virtual async Task GetTicketJsonAsync(string appId, string appSecret) { - var options = await _optionsProvider.GetAsync(appId); + var cacheKey = await GetCacheKeyAsync(appId); - var accessToken = await _accessTokenProvider.GetAsync(appId, appSecret); + var cachedValue = await _cache.GetOrNullAsync(cacheKey); - return await _distributedCache.GetOrAddAsync(await GetJsTicketAsync(options), - async () => - { - var client = _httpClientFactory.CreateClient(AbpWeChatConsts.HttpClientName); - var requestUrl = - $"https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={accessToken}&type=jsapi"; + if (cachedValue.IsNullOrEmpty()) + { + cachedValue = await RequestTicketAsync(appId, appSecret); - return await (await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, requestUrl))) - .Content.ReadAsStringAsync(); - }, - () => new DistributedCacheEntryOptions + await _cache.SetAsync(cacheKey, cachedValue, new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(115) }); + } + + return cacheKey; } - protected virtual async Task GetJsTicketAsync(IAbpWeChatOptions options) + protected virtual Task GetCacheKeyAsync(string appId) { - return $"WeChatJsTicket:{options.AppId}"; + return Task.FromResult($"WeChatJsTicket:{appId}"); } public virtual async Task GetTicketAsync(string appId, string appSecret) @@ -65,5 +61,17 @@ public virtual async Task GetTicketAsync(string appId, string appSecret) return jObj.SelectToken("$.ticket")!.Value(); } + + protected virtual async Task RequestTicketAsync(string appId, string appSecret) + { + var accessToken = await _accessTokenProvider.GetAsync(appId, appSecret); + + var client = _httpClientFactory.CreateClient(AbpWeChatConsts.HttpClientName); + var requestUrl = + $"https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token={accessToken}&type=jsapi"; + + return await (await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, requestUrl))) + .Content.ReadAsStringAsync(); + } } } \ No newline at end of file diff --git a/src/OpenPlatform/EasyAbp.Abp.WeChat.OpenPlatform/ThirdPartyPlatform/AccessToken/AuthorizerAccessTokenCache.cs b/src/OpenPlatform/EasyAbp.Abp.WeChat.OpenPlatform/ThirdPartyPlatform/AccessToken/AuthorizerAccessTokenCache.cs index 2427b12..96ef3a5 100644 --- a/src/OpenPlatform/EasyAbp.Abp.WeChat.OpenPlatform/ThirdPartyPlatform/AccessToken/AuthorizerAccessTokenCache.cs +++ b/src/OpenPlatform/EasyAbp.Abp.WeChat.OpenPlatform/ThirdPartyPlatform/AccessToken/AuthorizerAccessTokenCache.cs @@ -1,26 +1,32 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using EasyAbp.Abp.WeChat.Common.Infrastructure.AccessToken; +using Microsoft.Extensions.Caching.Distributed; using Volo.Abp.DependencyInjection; namespace EasyAbp.Abp.WeChat.OpenPlatform.ThirdPartyPlatform.AccessToken; public class AuthorizerAccessTokenCache : IAuthorizerAccessTokenCache, ITransientDependency { - protected IAccessTokenCache AccessTokenCache { get; } + protected IAbpWeChatSharableCache AbpWeChatSharableCache { get; } - public AuthorizerAccessTokenCache(IAccessTokenCache accessTokenCache) + public AuthorizerAccessTokenCache(IAbpWeChatSharableCache abpWeChatSharableCache) { - AccessTokenCache = accessTokenCache; + AbpWeChatSharableCache = abpWeChatSharableCache; } public virtual async Task GetOrNullAsync(string componentAppId, string authorizerAppId) { - return await AccessTokenCache.GetOrNullAsync(await GetCacheKeyAsync(componentAppId, authorizerAppId)); + return await AbpWeChatSharableCache.GetOrNullAsync(await GetCacheKeyAsync(componentAppId, authorizerAppId)); } public virtual async Task SetAsync(string componentAppId, string authorizerAppId, string accessToken) { - await AccessTokenCache.SetAsync(await GetCacheKeyAsync(componentAppId, authorizerAppId), accessToken); + await AbpWeChatSharableCache.SetAsync(await GetCacheKeyAsync( + componentAppId, authorizerAppId), accessToken, new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(115) + }); } protected virtual async Task GetCacheKeyAsync(string componentAppId, string authorizerAppId) => diff --git a/src/OpenPlatform/EasyAbp.Abp.WeChat.OpenPlatform/ThirdPartyPlatform/AuthorizerRefreshToken/CacheAuthorizerRefreshTokenStore.cs b/src/OpenPlatform/EasyAbp.Abp.WeChat.OpenPlatform/ThirdPartyPlatform/AuthorizerRefreshToken/CacheAuthorizerRefreshTokenStore.cs index bd8c32b..5840467 100644 --- a/src/OpenPlatform/EasyAbp.Abp.WeChat.OpenPlatform/ThirdPartyPlatform/AuthorizerRefreshToken/CacheAuthorizerRefreshTokenStore.cs +++ b/src/OpenPlatform/EasyAbp.Abp.WeChat.OpenPlatform/ThirdPartyPlatform/AuthorizerRefreshToken/CacheAuthorizerRefreshTokenStore.cs @@ -1,5 +1,6 @@ using System.Threading.Tasks; -using Volo.Abp.Caching; +using EasyAbp.Abp.WeChat.Common.Infrastructure.AccessToken; +using Microsoft.Extensions.Caching.Distributed; using Volo.Abp.DependencyInjection; namespace EasyAbp.Abp.WeChat.OpenPlatform.ThirdPartyPlatform.AuthorizerRefreshToken; @@ -12,21 +13,22 @@ namespace EasyAbp.Abp.WeChat.OpenPlatform.ThirdPartyPlatform.AuthorizerRefreshTo [Dependency(TryRegister = true)] public class CacheAuthorizerRefreshTokenStore : IAuthorizerRefreshTokenStore, ITransientDependency { - private readonly IDistributedCache _cache; + private readonly IAbpWeChatSharableCache _cache; - public CacheAuthorizerRefreshTokenStore(IDistributedCache cache) + public CacheAuthorizerRefreshTokenStore(IAbpWeChatSharableCache cache) { _cache = cache; } public virtual async Task GetOrNullAsync(string componentAppId, string authorizerAppId) { - return await _cache.GetAsync(await GetCacheKeyAsync(componentAppId, authorizerAppId)); + return await _cache.GetOrNullAsync(await GetCacheKeyAsync(componentAppId, authorizerAppId)); } public virtual async Task SetAsync(string componentAppId, string authorizerAppId, string authorizerRefreshToken) { - await _cache.SetAsync(await GetCacheKeyAsync(componentAppId, authorizerAppId), authorizerRefreshToken); + await _cache.SetAsync(await GetCacheKeyAsync(componentAppId, authorizerAppId), authorizerRefreshToken, + new DistributedCacheEntryOptions()); } protected virtual async Task GetCacheKeyAsync(string componentAppId, string authorizerAppId) => diff --git a/src/OpenPlatform/EasyAbp.Abp.WeChat.OpenPlatform/ThirdPartyPlatform/ComponentAccessToken/DefaultComponentAccessTokenProvider.cs b/src/OpenPlatform/EasyAbp.Abp.WeChat.OpenPlatform/ThirdPartyPlatform/ComponentAccessToken/DefaultComponentAccessTokenProvider.cs index ff8ea33..4527254 100644 --- a/src/OpenPlatform/EasyAbp.Abp.WeChat.OpenPlatform/ThirdPartyPlatform/ComponentAccessToken/DefaultComponentAccessTokenProvider.cs +++ b/src/OpenPlatform/EasyAbp.Abp.WeChat.OpenPlatform/ThirdPartyPlatform/ComponentAccessToken/DefaultComponentAccessTokenProvider.cs @@ -1,28 +1,28 @@ using System; using System.Net.Http; using System.Threading.Tasks; +using EasyAbp.Abp.WeChat.Common.Infrastructure.AccessToken; using EasyAbp.Abp.WeChat.OpenPlatform.ThirdPartyPlatform.ApiRequests; using EasyAbp.Abp.WeChat.OpenPlatform.ThirdPartyPlatform.Models; using EasyAbp.Abp.WeChat.OpenPlatform.ThirdPartyPlatform.VerifyTicket; using Microsoft.Extensions.Caching.Distributed; using Volo.Abp; -using Volo.Abp.Caching; using Volo.Abp.DependencyInjection; namespace EasyAbp.Abp.WeChat.OpenPlatform.ThirdPartyPlatform.ComponentAccessToken; public class DefaultComponentAccessTokenProvider : IComponentAccessTokenProvider, ITransientDependency { - private readonly IDistributedCache _distributedCache; + private readonly IAbpWeChatSharableCache _cache; private readonly IComponentVerifyTicketStore _componentVerifyTicketStore; private readonly IWeChatThirdPartyPlatformApiRequester _apiRequester; public DefaultComponentAccessTokenProvider( - IDistributedCache distributedCache, + IAbpWeChatSharableCache cache, IComponentVerifyTicketStore componentVerifyTicketStore, IWeChatThirdPartyPlatformApiRequester apiRequester) { - _distributedCache = distributedCache; + _cache = cache; _componentVerifyTicketStore = componentVerifyTicketStore; _apiRequester = apiRequester; } @@ -32,12 +32,24 @@ public virtual async Task GetAsync(string componentAppId, string compone Check.NotNullOrWhiteSpace(componentAppId, nameof(componentAppId)); Check.NotNullOrWhiteSpace(componentAppSecret, nameof(componentAppSecret)); - return await _distributedCache.GetOrAddAsync($"ComponentAccessToken:{componentAppId}", - async () => await RequestComponentAccessTokenAsync(componentAppId, componentAppSecret), - () => new DistributedCacheEntryOptions - { - AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(115) - }); + var key = $"ComponentAccessToken:{componentAppId}"; + + var cachedValue = await _cache.GetOrNullAsync(key); + + if (cachedValue.IsNullOrEmpty()) + { + cachedValue = await RequestComponentAccessTokenAsync(componentAppId, componentAppSecret); + + await _cache.SetAsync( + key, + cachedValue, + new DistributedCacheEntryOptions + { + AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(115) + }); + } + + return cachedValue; } protected virtual async Task RequestComponentAccessTokenAsync( diff --git a/src/OpenPlatform/EasyAbp.Abp.WeChat.OpenPlatform/ThirdPartyPlatform/VerifyTicket/CacheComponentVerifyTicketStore.cs b/src/OpenPlatform/EasyAbp.Abp.WeChat.OpenPlatform/ThirdPartyPlatform/VerifyTicket/CacheComponentVerifyTicketStore.cs index 991fdd5..b518d4d 100644 --- a/src/OpenPlatform/EasyAbp.Abp.WeChat.OpenPlatform/ThirdPartyPlatform/VerifyTicket/CacheComponentVerifyTicketStore.cs +++ b/src/OpenPlatform/EasyAbp.Abp.WeChat.OpenPlatform/ThirdPartyPlatform/VerifyTicket/CacheComponentVerifyTicketStore.cs @@ -1,7 +1,7 @@ using System; using System.Threading.Tasks; +using EasyAbp.Abp.WeChat.Common.Infrastructure.AccessToken; using Microsoft.Extensions.Caching.Distributed; -using Volo.Abp.Caching; using Volo.Abp.DependencyInjection; namespace EasyAbp.Abp.WeChat.OpenPlatform.ThirdPartyPlatform.VerifyTicket; @@ -14,16 +14,16 @@ namespace EasyAbp.Abp.WeChat.OpenPlatform.ThirdPartyPlatform.VerifyTicket; [Dependency(TryRegister = true)] public class CacheComponentVerifyTicketStore : IComponentVerifyTicketStore, ITransientDependency { - private readonly IDistributedCache _cache; + private readonly IAbpWeChatSharableCache _cache; - public CacheComponentVerifyTicketStore(IDistributedCache cache) + public CacheComponentVerifyTicketStore(IAbpWeChatSharableCache cache) { _cache = cache; } public virtual async Task GetOrNullAsync(string componentAppId) { - return await _cache.GetAsync(await GetCacheKeyAsync(componentAppId)); + return await _cache.GetOrNullAsync(await GetCacheKeyAsync(componentAppId)); } public virtual async Task SetAsync(string componentAppId, string componentVerifyTicket) diff --git a/tests/EasyAbp.Abp.WeChat.OpenPlatform.Tests/ThirdPartyPlatform/HybridAccessTokenProviderTests.cs b/tests/EasyAbp.Abp.WeChat.OpenPlatform.Tests/ThirdPartyPlatform/HybridAccessTokenProviderTests.cs index 2e8e2e7..1d57632 100644 --- a/tests/EasyAbp.Abp.WeChat.OpenPlatform.Tests/ThirdPartyPlatform/HybridAccessTokenProviderTests.cs +++ b/tests/EasyAbp.Abp.WeChat.OpenPlatform.Tests/ThirdPartyPlatform/HybridAccessTokenProviderTests.cs @@ -20,7 +20,7 @@ public class HybridAccessTokenProviderTests : AbpWeChatOpenPlatformTestBase protected const string AuthorizerAppId = "my-authorizer-appid"; private IAuthorizerAccessTokenCache _authorizerAccessTokenCache; - private IAccessTokenCache _accessTokenCache; + private IAbpWeChatSharableCache _abpWeChatSharableCache; private readonly HybridAccessTokenProvider _hybridAccessTokenProvider; private readonly ICurrentWeChatThirdPartyPlatform _currentWeChatThirdPartyPlatform; @@ -37,12 +37,12 @@ protected override void AfterAddApplication(IServiceCollection services) _authorizerAccessTokenCache = Substitute.For(); services.Replace(ServiceDescriptor.Transient(s => _authorizerAccessTokenCache)); - _accessTokenCache = Substitute.For(); - services.Replace(ServiceDescriptor.Transient(s => _accessTokenCache)); + _abpWeChatSharableCache = Substitute.For(); + services.Replace(ServiceDescriptor.Transient(s => _abpWeChatSharableCache)); _authorizerAccessTokenCache.GetOrNullAsync(OptionsComponentAppId, AuthorizerAppId) .Returns(ComponentAccessToken); - _accessTokenCache.GetOrNullAsync(Arg.Any()).Returns(AppAccessToken); + _abpWeChatSharableCache.GetOrNullAsync(Arg.Any()).Returns(AppAccessToken); } [Fact]