mirror of https://github.com/abpframework/abp.git
7 changed files with 126 additions and 0 deletions
@ -0,0 +1,49 @@ |
|||||
|
using System; |
||||
|
using System.Reflection; |
||||
|
using System.Threading; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.Extensions.Caching.StackExchangeRedis; |
||||
|
using Microsoft.Extensions.Options; |
||||
|
using StackExchange.Redis; |
||||
|
using Volo.Abp.DependencyInjection; |
||||
|
|
||||
|
namespace Volo.Abp.Caching.StackExchangeRedis |
||||
|
{ |
||||
|
[DisableConventionalRegistration] |
||||
|
public class AbpRedisCache : RedisCache |
||||
|
{ |
||||
|
private static readonly FieldInfo RedisDatabaseField; |
||||
|
private static readonly MethodInfo ConnectMethod; |
||||
|
private static readonly MethodInfo ConnectAsyncMethod; |
||||
|
|
||||
|
protected IDatabase RedisDatabase => RedisDatabaseField.GetValue(this) as IDatabase; |
||||
|
|
||||
|
static AbpRedisCache() |
||||
|
{ |
||||
|
RedisDatabaseField = typeof(RedisCache) |
||||
|
.GetField("_cache", BindingFlags.Instance | BindingFlags.NonPublic); |
||||
|
|
||||
|
ConnectMethod = typeof(RedisCache) |
||||
|
.GetMethod("Connect", BindingFlags.Instance | BindingFlags.NonPublic); |
||||
|
|
||||
|
ConnectAsyncMethod = typeof(RedisCache) |
||||
|
.GetMethod("ConnectAsync", BindingFlags.Instance | BindingFlags.NonPublic); |
||||
|
} |
||||
|
|
||||
|
public AbpRedisCache(IOptions<RedisCacheOptions> optionsAccessor) |
||||
|
: base(optionsAccessor) |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
protected virtual void Connect() |
||||
|
{ |
||||
|
ConnectMethod.Invoke(this, Array.Empty<object>()); |
||||
|
} |
||||
|
|
||||
|
protected virtual Task ConnectAsync(CancellationToken token = default) |
||||
|
{ |
||||
|
return (Task) ConnectAsyncMethod.Invoke(this, new object[] {token}); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,17 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<Import Project="..\..\..\common.test.props" /> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>netcoreapp3.1</TargetFramework> |
||||
|
<RootNamespace /> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" /> |
||||
|
<ProjectReference Include="..\..\src\Volo.Abp.Autofac\Volo.Abp.Autofac.csproj" /> |
||||
|
<ProjectReference Include="..\..\src\Volo.Abp.Caching.StackExchangeRedis\Volo.Abp.Caching.StackExchangeRedis.csproj" /> |
||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,12 @@ |
|||||
|
using Volo.Abp.Testing; |
||||
|
|
||||
|
namespace Volo.Abp.Caching.StackExchangeRedis |
||||
|
{ |
||||
|
public abstract class AbpCachingStackExchangeRedisTestBase : AbpIntegratedTest<AbpCachingStackExchangeRedisTestModule> |
||||
|
{ |
||||
|
protected override void SetAbpApplicationCreationOptions(AbpApplicationCreationOptions options) |
||||
|
{ |
||||
|
options.UseAutofac(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,15 @@ |
|||||
|
using Volo.Abp.Autofac; |
||||
|
using Volo.Abp.Modularity; |
||||
|
|
||||
|
namespace Volo.Abp.Caching.StackExchangeRedis |
||||
|
{ |
||||
|
[DependsOn( |
||||
|
typeof(AbpCachingStackExchangeRedisModule), |
||||
|
typeof(AbpTestBaseModule), |
||||
|
typeof(AbpAutofacModule) |
||||
|
)] |
||||
|
public class AbpCachingStackExchangeRedisTestModule : AbpModule |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
using Microsoft.Extensions.Caching.Distributed; |
||||
|
using Shouldly; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.Caching.StackExchangeRedis |
||||
|
{ |
||||
|
public class AbpRedisCache_Tests : AbpCachingStackExchangeRedisTestBase |
||||
|
{ |
||||
|
private readonly IDistributedCache _distributedCache; |
||||
|
|
||||
|
public AbpRedisCache_Tests() |
||||
|
{ |
||||
|
_distributedCache = GetRequiredService<IDistributedCache>(); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Replace_RedisCache() |
||||
|
{ |
||||
|
(_distributedCache is AbpRedisCache).ShouldBeTrue(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue