From dc4429d66c68b0bf2c190e8b6a5f2d4e6e707575 Mon Sep 17 00:00:00 2001 From: "454313500@qq.com" <454313500@qq.com> Date: Sun, 21 Mar 2021 15:51:49 +0800 Subject: [PATCH] v1.0.3 --- CC.Yi.API/Controllers/StudentController.cs | 2 +- CC.Yi.API/Startup.cs | 7 +- CC.Yi.API/appsettings.json | 4 + CC.Yi.Common/CC.Yi.Common.csproj | 3 +- CC.Yi.Common/Cache/CacheHelper.cs | 53 ++++++++++ CC.Yi.Common/Cache/ICacheWriter.cs | 31 ++++++ CC.Yi.Common/Cache/RedisCacheService.cs | 96 +++++++++++++++++++ .../{JsonFactory.cs => JsonHelper.cs} | 2 +- 8 files changed, 194 insertions(+), 4 deletions(-) create mode 100644 CC.Yi.Common/Cache/CacheHelper.cs create mode 100644 CC.Yi.Common/Cache/ICacheWriter.cs create mode 100644 CC.Yi.Common/Cache/RedisCacheService.cs rename CC.Yi.Common/{JsonFactory.cs => JsonHelper.cs} (84%) diff --git a/CC.Yi.API/Controllers/StudentController.cs b/CC.Yi.API/Controllers/StudentController.cs index 41de2620..66fc4c0b 100644 --- a/CC.Yi.API/Controllers/StudentController.cs +++ b/CC.Yi.API/Controllers/StudentController.cs @@ -30,7 +30,7 @@ namespace CC.Yi.API.Controllers public IActionResult GetTest()//查 { var data = _studentBll.GetAllEntities().ToList(); - return Content(Common.JsonFactory.JsonToString(data)); + return Content(Common.JsonHelper.JsonToString(data)); } [HttpGet] public IActionResult AddTest()//增 diff --git a/CC.Yi.API/Startup.cs b/CC.Yi.API/Startup.cs index ec647137..3206fc1c 100644 --- a/CC.Yi.API/Startup.cs +++ b/CC.Yi.API/Startup.cs @@ -2,6 +2,7 @@ using Autofac; using Autofac.Extras.DynamicProxy; using CC.Yi.BLL; +using CC.Yi.Common.Cache; using CC.Yi.Common.Castle; using CC.Yi.DAL; using CC.Yi.IBLL; @@ -51,6 +52,11 @@ namespace CC.Yi.API //עתAutofac //services.AddScoped(typeof(IBaseDal<>), typeof(BaseDal<>)); //services.AddScoped(typeof(IstudentBll), typeof(studentBll)); + services.AddSingleton(typeof(ICacheWriter), new RedisCacheService(new Microsoft.Extensions.Caching.Redis.RedisCacheOptions() + { + Configuration = Configuration.GetSection("Cache.ConnectionString").Value, + InstanceName = Configuration.GetSection("Cache.InstanceName").Value + })); } //̬ AOP˼ע Autofac @@ -59,7 +65,6 @@ namespace CC.Yi.API builder.RegisterType(typeof(CustomAutofacAop)); builder.RegisterGeneric(typeof(BaseDal<>)).As(typeof(IBaseDal<>)) ; builder.RegisterType().As().EnableInterfaceInterceptors();//ʾעǰҪִCastle - } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/CC.Yi.API/appsettings.json b/CC.Yi.API/appsettings.json index 0112e26e..b5156473 100644 --- a/CC.Yi.API/appsettings.json +++ b/CC.Yi.API/appsettings.json @@ -7,6 +7,10 @@ } }, "AllowedHosts": "*", + "Cache": { + "InstanceName": "Redis", + "ConnectionString": "127.0.0.1:12345,password=123456" + }, "ConnectionStringBySQL": "server=.;Database=cctest;UId=sa;PWD=Qz52013142020.", "ConnectionStringByMySQL": "Data Source=.;Database=cctest;User ID=root;Password=Qz52013142020.;pooling=true;port=3306;sslmode=none;CharSet=utf8;" } diff --git a/CC.Yi.Common/CC.Yi.Common.csproj b/CC.Yi.Common/CC.Yi.Common.csproj index efd8020e..4230a367 100644 --- a/CC.Yi.Common/CC.Yi.Common.csproj +++ b/CC.Yi.Common/CC.Yi.Common.csproj @@ -1,4 +1,4 @@ - + netcoreapp3.1 @@ -7,6 +7,7 @@ + diff --git a/CC.Yi.Common/Cache/CacheHelper.cs b/CC.Yi.Common/Cache/CacheHelper.cs new file mode 100644 index 00000000..0bc435b0 --- /dev/null +++ b/CC.Yi.Common/Cache/CacheHelper.cs @@ -0,0 +1,53 @@ +using Autofac; +using System; +using System.Collections.Generic; +using System.Text; + +namespace CC.Yi.Common.Cache +{ + public class CacheHelper + { + public static ICacheWriter CacheWriter { get; set; } + static CacheHelper() + { + + //这里存在一些问题 + ContainerBuilder containerBuilder = new ContainerBuilder(); + IContainer container = containerBuilder.Build(); + CacheHelper.CacheWriter = container.Resolve(); + } + + public static void AddCache(string key, string value, int expDate) + { + CacheWriter.Add(key, value, expDate); + } + + //没有过期参数,表示用不过期 + public static void AddCache(string key, string value) + { + CacheWriter.Add(key, value); + } + + public static object GetCache(string key) + { + return CacheWriter.Get(key); + } + public static void SetCache(string key, string value, int expDate) + { + + CacheWriter.Replace(key, value, expDate); + } + + //没有过期参数,表示用不过期 + public static void SetCache(string key, string value) + { + CacheWriter.Replace(key, value); + } + + public static void Remove(string key) + { + CacheWriter.Remove(key); + } + + } +} diff --git a/CC.Yi.Common/Cache/ICacheWriter.cs b/CC.Yi.Common/Cache/ICacheWriter.cs new file mode 100644 index 00000000..21a6c943 --- /dev/null +++ b/CC.Yi.Common/Cache/ICacheWriter.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CC.Yi.Common.Cache +{ + public interface ICacheWriter + { + //void AddCache(string key, object value, DateTime expDate); + //void AddCache(string key, object value); + //object GetCache(string key); + //T GetCache(string key); + //void SetCache(string key, object value, DateTime expDate); + //void SetCache(string key, object value); + //bool Delete(string key); + + string Get(string key); + + string GetString(string key); + + void AddString(string key, string value); + + void Add(string key, string value, int ExpirationTime = 20); + + void Remove(string key); + + void Replace(string key, string value, int ExpirationTime = 20); + } +} diff --git a/CC.Yi.Common/Cache/RedisCacheService.cs b/CC.Yi.Common/Cache/RedisCacheService.cs new file mode 100644 index 00000000..c4dd22e9 --- /dev/null +++ b/CC.Yi.Common/Cache/RedisCacheService.cs @@ -0,0 +1,96 @@ +using Microsoft.Extensions.Caching.Distributed; +using Microsoft.Extensions.Caching.Redis; +using System; +using System.Collections.Generic; +using System.Text; + +namespace CC.Yi.Common.Cache +{ + public class RedisCacheService :ICacheWriter + { + private RedisCache _redisCache = null; + public RedisCacheService(RedisCacheOptions options) + { + _redisCache = new RedisCache(options); + } + /// + /// 获取缓存 + /// + /// 缓存key + /// + public string Get(string key) + { + try + { + if (!string.IsNullOrEmpty(key)) + { + return Encoding.UTF8.GetString(_redisCache.Get(key)); + } + else + { + return string.Empty; + } + } + catch + { + return null; + } + } + /// + /// 添加缓存 + /// + /// 缓存key + /// 缓存值 + /// 绝对过期时间(分钟) + public void Add(string key, string value, int ExpirationTime = 20) + { + if (!string.IsNullOrEmpty(key)) + { + _redisCache.Set(key, Encoding.UTF8.GetBytes(value), new DistributedCacheEntryOptions() + { + AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(ExpirationTime) + }); + } + } + + public void AddString(string key, string value) + { + _redisCache.SetString(key, value); + } + + public string GetString(string key) + + { + return _redisCache.GetString(key); + } + + /// + /// 移除缓存 + /// + /// 缓存key + public void Remove(string key) + { + if (!string.IsNullOrEmpty(key)) + { + _redisCache.Remove(key); + } + } + /// + /// 更新缓存 + /// + /// 缓存key + /// 缓存值 + /// + public void Replace(string key, string value, int ExpirationTime = 20) + { + if (!string.IsNullOrEmpty(key)) + { + _redisCache.Remove(key); + _redisCache.Set(key, Encoding.UTF8.GetBytes(value), new DistributedCacheEntryOptions() + { + AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(ExpirationTime) + }); + } + } + } +} diff --git a/CC.Yi.Common/JsonFactory.cs b/CC.Yi.Common/JsonHelper.cs similarity index 84% rename from CC.Yi.Common/JsonFactory.cs rename to CC.Yi.Common/JsonHelper.cs index f51b56c4..b427195b 100644 --- a/CC.Yi.Common/JsonFactory.cs +++ b/CC.Yi.Common/JsonHelper.cs @@ -2,7 +2,7 @@ namespace CC.Yi.Common { - public static class JsonFactory + public static class JsonHelper { public static string JsonToString(object q) {