├── Abp.CSRedisCache
├── Abp.CSRedisCache.csproj
└── Abp
│ └── Runtime
│ └── Caching
│ └── CSRedis
│ ├── AbpCSRedisCacheModule.cs
│ ├── AbpRedisCacheManager.cs
│ ├── IRedisCacheSerializer.cs
│ ├── AbpRedisCacheOptions.cs
│ ├── RedisCacheConfigurationExtensions.cs
│ ├── DefaultRedisCacheSerializer.cs
│ └── AbpRedisCache.cs
├── README.md
├── Abp.CSRedisCache.sln
├── .gitignore
└── .gitattributes
/Abp.CSRedisCache/Abp.CSRedisCache.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netstandard2.0
5 | 6.0.0
6 | cuizhijiang
7 | false
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/Abp.CSRedisCache/Abp/Runtime/Caching/CSRedis/AbpCSRedisCacheModule.cs:
--------------------------------------------------------------------------------
1 | using Abp.Modules;
2 | using Abp.Reflection.Extensions;
3 | using CSRedis;
4 |
5 | namespace Abp.Runtime.Caching.CSRedis
6 | {
7 | ///
8 | /// This modules is used to replace ABP's cache system with Redis server.
9 | ///
10 | [DependsOn(typeof(AbpKernelModule))]
11 | public class AbpCSRedisCacheModule : AbpModule
12 | {
13 | public override void PreInitialize()
14 | {
15 | IocManager.Register();
16 | }
17 |
18 | public override void Initialize()
19 | {
20 | IocManager.RegisterAssemblyByConvention(typeof(AbpCSRedisCacheModule).GetAssembly());
21 | }
22 | }
23 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Abp.CSRedisCache
2 | [](https://www.nuget.org/packages/Abp.CSRedisCache/)
3 | ## Getting Started
4 | ### Configuration
5 | Add a DependsOn attribute for the AbpRedisCacheModule and call the UseRedis extension method in the PreInitialize method of your module, as shown below:
6 | ```csharp
7 | [DependsOn(
8 | //...other module dependencies
9 | typeof(AbpCSRedisCacheModule))]
10 | public class MyProjectWebModule : AbpModule
11 | {
12 | public override void PreInitialize()
13 | {
14 | //...other configurations
15 |
16 | Configuration.Caching.UseCSRedis(options =>
17 | {
18 | options.ConnectionString = "127.0.0.1:6379,pass=123,defaultDatabase=13,poolsize=50,ssl=false,writeBuffer=10240";
19 | });
20 |
21 | }
22 |
23 | //...other code
24 | }
25 |
--------------------------------------------------------------------------------
/Abp.CSRedisCache.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.28307.329
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Abp.CSRedisCache", "Abp.CSRedisCache\Abp.CSRedisCache.csproj", "{D0F76555-95E0-435B-A437-D4B91CBD208E}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {D0F76555-95E0-435B-A437-D4B91CBD208E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {D0F76555-95E0-435B-A437-D4B91CBD208E}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {D0F76555-95E0-435B-A437-D4B91CBD208E}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {D0F76555-95E0-435B-A437-D4B91CBD208E}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(ExtensibilityGlobals) = postSolution
23 | SolutionGuid = {097C16D4-9947-4A1C-A61C-9A9C2B7908EE}
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/Abp.CSRedisCache/Abp/Runtime/Caching/CSRedis/AbpRedisCacheManager.cs:
--------------------------------------------------------------------------------
1 | using Abp.Dependency;
2 | using Abp.Runtime.Caching.Configuration;
3 |
4 | namespace Abp.Runtime.Caching.CSRedis
5 | {
6 | ///
7 | /// Used to create instances.
8 | ///
9 | public class AbpRedisCacheManager : CacheManagerBase, ICacheManager
10 | {
11 | private readonly IIocManager _iocManager;
12 |
13 | ///
14 | /// Initializes a new instance of the class.
15 | ///
16 | public AbpRedisCacheManager(IIocManager iocManager, ICachingConfiguration configuration)
17 | : base(configuration)
18 | {
19 | _iocManager = iocManager;
20 | _iocManager.RegisterIfNot(DependencyLifeStyle.Transient);
21 | }
22 |
23 | protected override ICache CreateCacheImplementation(string name)
24 | {
25 | return _iocManager.Resolve(new { name });
26 | }
27 | protected override void DisposeCaches()
28 | {
29 | foreach (var cache in Caches)
30 | {
31 | _iocManager.Release(cache.Value);
32 | }
33 | }
34 | }
35 | }
--------------------------------------------------------------------------------
/Abp.CSRedisCache/Abp/Runtime/Caching/CSRedis/IRedisCacheSerializer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace Abp.Runtime.Caching.CSRedis
4 | {
5 | ///
6 | /// Interface to be implemented by all custom (de)serialization methods used when persisting and retrieving
7 | /// objects from the Redis cache.
8 | ///
9 | public interface IRedisCacheSerializer
10 | {
11 | ///
12 | /// Creates an instance of the object from its serialized string representation.
13 | ///
14 | /// String representation of the object from the Redis server.
15 | /// Returns a newly constructed object.
16 | ///
17 | object Deserialize(string objbyte);
18 |
19 | ///
20 | /// Produce a string representation of the supplied object.
21 | ///
22 | /// Instance to serialize.
23 | /// Type of the object.
24 | /// Returns a string representing the object instance that can be placed into the Redis cache.
25 | ///
26 | string Serialize(object value, Type type);
27 | }
28 | }
--------------------------------------------------------------------------------
/Abp.CSRedisCache/Abp/Runtime/Caching/CSRedis/AbpRedisCacheOptions.cs:
--------------------------------------------------------------------------------
1 | using System.Configuration;
2 | using Abp.Configuration.Startup;
3 | using Abp.Extensions;
4 |
5 | namespace Abp.Runtime.Caching.CSRedis
6 | {
7 | public class AbpRedisCacheOptions
8 | {
9 | private const string ConnectionStringKey = "Abp.Redis.Cache";
10 |
11 | private const string DatabaseIdSettingKey = "Abp.Redis.Cache.DatabaseId";
12 |
13 | public AbpRedisCacheOptions(IAbpStartupConfiguration abpStartupConfiguration)
14 | {
15 | AbpStartupConfiguration = abpStartupConfiguration;
16 |
17 | ConnectionString = GetDefaultConnectionString();
18 | DatabaseId = GetDefaultDatabaseId();
19 | }
20 |
21 | public IAbpStartupConfiguration AbpStartupConfiguration { get; }
22 |
23 | public string ConnectionString { get; set; }
24 |
25 | public int DatabaseId { get; set; }
26 |
27 | private static int GetDefaultDatabaseId()
28 | {
29 | var appSetting = ConfigurationManager.AppSettings[DatabaseIdSettingKey];
30 | if (appSetting.IsNullOrEmpty()) return -1;
31 |
32 | int databaseId;
33 | if (!int.TryParse(appSetting, out databaseId)) return -1;
34 |
35 | return databaseId;
36 | }
37 |
38 | private static string GetDefaultConnectionString()
39 | {
40 | var connStr = ConfigurationManager.ConnectionStrings[ConnectionStringKey];
41 | if (connStr == null || connStr.ConnectionString.IsNullOrWhiteSpace()) return "localhost";
42 |
43 | return connStr.ConnectionString;
44 | }
45 | }
46 | }
--------------------------------------------------------------------------------
/Abp.CSRedisCache/Abp/Runtime/Caching/CSRedis/RedisCacheConfigurationExtensions.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Abp.Dependency;
3 | using Abp.Runtime.Caching.Configuration;
4 | using CSRedis;
5 |
6 | namespace Abp.Runtime.Caching.CSRedis
7 | {
8 | ///
9 | /// Extension methods for .
10 | ///
11 | public static class RedisCacheConfigurationExtensions
12 | {
13 | ///
14 | /// Configures caching to use Redis as cache server.
15 | ///
16 | /// The caching configuration.
17 | public static void UseCSRedis(this ICachingConfiguration cachingConfiguration)
18 | {
19 | cachingConfiguration.UseCSRedis(options => { });
20 | }
21 |
22 | ///
23 | /// Configures caching to use Redis as cache server.
24 | ///
25 | /// The caching configuration.
26 | /// Ac action to get/set options
27 | public static void UseCSRedis(this ICachingConfiguration cachingConfiguration,
28 | Action optionsAction)
29 | {
30 | var iocManager = cachingConfiguration.AbpConfiguration.IocManager;
31 |
32 | iocManager.RegisterIfNot();
33 |
34 | var options= iocManager.Resolve();
35 | optionsAction(options);
36 |
37 | var connectionString = options.ConnectionString;
38 | if (options.DatabaseId > -1)
39 | connectionString = options.ConnectionString.TrimEnd(';') + ",defaultDatabase=" + options.DatabaseId;
40 | RedisHelper.Initialization(new CSRedisClient(connectionString));
41 | }
42 | }
43 | }
--------------------------------------------------------------------------------
/Abp.CSRedisCache/Abp/Runtime/Caching/CSRedis/DefaultRedisCacheSerializer.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Abp.Dependency;
3 | using Abp.Json;
4 | using Newtonsoft.Json;
5 |
6 | namespace Abp.Runtime.Caching.CSRedis
7 | {
8 | ///
9 | /// Default implementation uses JSON as the underlying persistence mechanism.
10 | ///
11 | public class DefaultRedisCacheSerializer : IRedisCacheSerializer, ITransientDependency
12 | {
13 | ///
14 | /// Creates an instance of the object from its serialized string representation.
15 | ///
16 | /// String representation of the object from the Redis server.
17 | /// Returns a newly constructed object.
18 | ///
19 | public virtual object Deserialize(string objbyte)
20 | {
21 | var serializerSettings = new JsonSerializerSettings();
22 | serializerSettings.Converters.Insert(0, new AbpDateTimeConverter());
23 |
24 | var cacheData = AbpCacheData.Deserialize(objbyte);
25 |
26 | return cacheData.Payload.FromJsonString(
27 | Type.GetType(cacheData.Type, true, true),
28 | serializerSettings);
29 | }
30 |
31 | ///
32 | /// Produce a string representation of the supplied object.
33 | ///
34 | /// Instance to serialize.
35 | /// Type of the object.
36 | /// Returns a string representing the object instance that can be placed into the Redis cache.
37 | ///
38 | public virtual string Serialize(object value, Type type)
39 | {
40 | return JsonConvert.SerializeObject(AbpCacheData.Serialize(value));
41 | }
42 | }
43 | }
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Build Folders (you can keep bin if you'd like, to store dlls and pdbs)
2 | [Bb]in/
3 | [Oo]bj/
4 |
5 | # Logs
6 | Logs/
7 |
8 | # Generated files
9 | project.lock.json
10 | .vs/
11 | .dotnet/
12 | .idea/
13 |
14 | # Nuget packages
15 | nupkg/*.nupkg
16 | nupkg/push.bat
17 |
18 | # Profile images
19 | ProfileImages/
20 |
21 | # mstest test results
22 | TestResults
23 |
24 | ## Ignore Visual Studio temporary files, build results, and
25 | ## files generated by popular Visual Studio add-ons.
26 |
27 | # User-specific files
28 | *.suo
29 | *.user
30 | *.sln.docstates
31 |
32 | # Build results
33 | [Dd]ebug/
34 | [Rr]elease/
35 | x64/
36 | *_i.c
37 | *_p.c
38 | *.ilk
39 | *.meta
40 | *.obj
41 | *.pch
42 | *.pdb
43 | *.pgc
44 | *.pgd
45 | *.rsp
46 | *.sbr
47 | *.tlb
48 | *.tli
49 | *.tlh
50 | *.tmp
51 | *.log
52 | *.vspscc
53 | *.vssscc
54 | .builds
55 |
56 | # Visual C++ cache files
57 | ipch/
58 | *.aps
59 | *.ncb
60 | *.opensdf
61 | *.sdf
62 |
63 | # Visual Studio profiler
64 | *.psess
65 | *.vsp
66 | *.vspx
67 |
68 | # Guidance Automation Toolkit
69 | *.gpState
70 |
71 | # ReSharper is a .NET coding add-in
72 | _ReSharper*
73 |
74 | # NCrunch
75 | *.ncrunch*
76 | .*crunch*.local.xml
77 |
78 | # Installshield output folder
79 | [Ee]xpress
80 |
81 | # DocProject is a documentation generator add-in
82 | DocProject/buildhelp/
83 | DocProject/Help/*.HxT
84 | DocProject/Help/*.HxC
85 | DocProject/Help/*.hhc
86 | DocProject/Help/*.hhk
87 | DocProject/Help/*.hhp
88 | DocProject/Help/Html2
89 | DocProject/Help/html
90 |
91 | # Click-Once directory
92 | publish
93 |
94 | # Publish Web Output
95 | *.Publish.xml
96 |
97 | # NuGet Packages Directory
98 | packages
99 |
100 | # Windows Azure Build Output
101 | csx
102 | *.build.csdef
103 |
104 | # Windows Store app package directory
105 | AppPackages/
106 |
107 | # Others
108 | [Bb]in
109 | [Oo]bj
110 | sql
111 | TestResults
112 | [Tt]est[Rr]esult*
113 | *.Cache
114 | ClientBin
115 | [Ss]tyle[Cc]op.*
116 | ~$*
117 | *.dbmdl
118 | Generated_Code #added for RIA/Silverlight projects
119 |
120 | # Backup & report files from converting an old project file to a newer
121 | # Visual Studio version. Backup files are not needed, because we have git ;-)
122 | _UpgradeReport_Files/
123 | Backup*/
124 | UpgradeLog*.XML
125 | src/.vs/config/applicationhost.config
126 |
127 | # GitLink
128 | !GitLink.exe
129 | !nuget.exe
130 | !SQLite.Interop.dll
131 | /tools
132 | nupkg/push-hikalkan.bat
133 |
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/Abp.CSRedisCache/Abp/Runtime/Caching/CSRedis/AbpRedisCache.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Reflection;
5 | using System.Threading.Tasks;
6 | using Abp.Data;
7 | using Abp.Domain.Entities;
8 | using Abp.Reflection.Extensions;
9 |
10 | namespace Abp.Runtime.Caching.CSRedis
11 | {
12 | ///
13 | /// Used to store cache in a Redis server.
14 | ///
15 | public class AbpRedisCache : CacheBase
16 | {
17 | private readonly IRedisCacheSerializer _serializer;
18 |
19 | ///
20 | /// Constructor.
21 | ///
22 | public AbpRedisCache(
23 | string name,
24 | IRedisCacheSerializer redisCacheSerializer)
25 | : base(name)
26 | {
27 | _serializer = redisCacheSerializer;
28 | }
29 |
30 | public override object GetOrDefault(string key)
31 | {
32 | var objbyte = RedisHelper.Get(GetLocalizedRedisKey(key));
33 | return !string.IsNullOrWhiteSpace(objbyte) ? Deserialize(objbyte) : null;
34 | }
35 |
36 | public override object[] GetOrDefault(string[] keys)
37 | {
38 | var redisKeys = keys.Select(GetLocalizedRedisKey);
39 | var redisValues = RedisHelper.MGet(redisKeys.ToArray());
40 | var objbytes = redisValues.Select(obj => !string.IsNullOrWhiteSpace(obj) ? Deserialize(obj) : null);
41 | return objbytes.ToArray();
42 | }
43 |
44 | public override async Task