From 577c131163a55803cbfaf79b6e0634168e46aade Mon Sep 17 00:00:00 2001 From: "454313500@qq.com" <454313500@qq.com> Date: Thu, 15 Apr 2021 16:49:35 +0800 Subject: [PATCH] v1.1.4 --- CC.Yi.API/Controllers/StudentController.cs | 10 +++ CC.Yi.Common/CC.Yi.Common.csproj | 1 + CC.Yi.Common/Cache/CacheHelper.cs | 40 +++++---- CC.Yi.Common/Cache/ICacheWriter.cs | 25 ++---- CC.Yi.Common/Cache/RedisCache.cs | 46 +++++++++++ CC.Yi.Common/Cache/RedisCacheService.cs | 96 ---------------------- 6 files changed, 82 insertions(+), 136 deletions(-) create mode 100644 CC.Yi.Common/Cache/RedisCache.cs delete mode 100644 CC.Yi.Common/Cache/RedisCacheService.cs diff --git a/CC.Yi.API/Controllers/StudentController.cs b/CC.Yi.API/Controllers/StudentController.cs index 45e52835..f0101f08 100644 --- a/CC.Yi.API/Controllers/StudentController.cs +++ b/CC.Yi.API/Controllers/StudentController.cs @@ -1,4 +1,5 @@ using CC.Yi.Common; +using CC.Yi.Common.Cache; using CC.Yi.IBLL; using CC.Yi.Model; using Microsoft.AspNetCore.Identity; @@ -49,6 +50,15 @@ namespace CC.Yi.API.Controllers // return Ok(); //} + #region + //下面,这里是操作reids + #endregion + [HttpGet] + public Result GetReids() + { + var data = CacheHelper.CacheWriter.GetCache("key01"); + return Result.Success(data); + } #region diff --git a/CC.Yi.Common/CC.Yi.Common.csproj b/CC.Yi.Common/CC.Yi.Common.csproj index c1361bd4..2d1ddc8f 100644 --- a/CC.Yi.Common/CC.Yi.Common.csproj +++ b/CC.Yi.Common/CC.Yi.Common.csproj @@ -10,6 +10,7 @@ + diff --git a/CC.Yi.Common/Cache/CacheHelper.cs b/CC.Yi.Common/Cache/CacheHelper.cs index 8b55955c..e77f8fbc 100644 --- a/CC.Yi.Common/Cache/CacheHelper.cs +++ b/CC.Yi.Common/Cache/CacheHelper.cs @@ -11,42 +11,40 @@ namespace CC.Yi.Common.Cache public static ICacheWriter CacheWriter { get; set; } static CacheHelper() { - //这里存在一些问题 - ContainerBuilder containerBuilder = new ContainerBuilder(); - IContainer container = containerBuilder.Build(); - CacheHelper.CacheWriter = container.Resolve(); + CacheHelper.CacheWriter = new RedisCache(); } - public static void AddCache(string key, string value, int expDate) + + + + public bool AddCache(string key, T value, DateTime expDate) { - CacheWriter.Add(key, value, expDate); + return CacheWriter.AddCache(key,value,expDate); } - //没有过期参数,表示用不过期 - public static void AddCache(string key, string value) + public bool AddCache(string key, T value) { - CacheWriter.Add(key, value); + return CacheWriter.AddCache(key, value); } - public static object GetCache(string key) + public bool RemoveCache(string key) { - return CacheWriter.Get(key); - } - public static void SetCache(string key, string value, int expDate) - { - - CacheWriter.Replace(key, value, expDate); + return CacheWriter.RemoveCache(key); } - //没有过期参数,表示用不过期 - public static void SetCache(string key, string value) + public T GetCache(string key) { - CacheWriter.Replace(key, value); + return CacheWriter.GetCache(key); } - public static void Remove(string key) + public bool SetCache(string key, T value, DateTime expDate) { - CacheWriter.Remove(key); + return CacheWriter.SetCache(key,value,expDate); + } + + public bool SetCache(string key, T value) + { + return CacheWriter.SetCache(key, value); } } diff --git a/CC.Yi.Common/Cache/ICacheWriter.cs b/CC.Yi.Common/Cache/ICacheWriter.cs index 21a6c943..712f6857 100644 --- a/CC.Yi.Common/Cache/ICacheWriter.cs +++ b/CC.Yi.Common/Cache/ICacheWriter.cs @@ -8,24 +8,11 @@ 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); + bool AddCache(string key, T value, DateTime expDate); + bool AddCache(string key, T value); + bool RemoveCache(string key); + T GetCache(string key); + bool SetCache(string key, T value, DateTime expDate); + bool SetCache(string key, T value); } } diff --git a/CC.Yi.Common/Cache/RedisCache.cs b/CC.Yi.Common/Cache/RedisCache.cs new file mode 100644 index 00000000..a1273002 --- /dev/null +++ b/CC.Yi.Common/Cache/RedisCache.cs @@ -0,0 +1,46 @@ +using ServiceStack.Redis; +using System; +using System.Collections.Generic; +using System.Text; + +namespace CC.Yi.Common.Cache +{ + public class RedisCache : ICacheWriter + { + private RedisClient client; + public RedisCache() + { + client = new RedisClient("127.0.0.1", 6379, "52013142020."); + } + + public bool AddCache(string key, T value, DateTime expDate) + { + return client.Add(key, value, expDate); + } + + public bool AddCache(string key, T value) + { + return client.Add(key, value); + } + + public bool RemoveCache(string key) + { + return client.Remove(key); + } + + public T GetCache(string key) + { + return client.Get(key); + } + + public bool SetCache(string key,T value, DateTime expDate) + { + return client.Set(key, value, expDate); + } + + public bool SetCache(string key, T value) + { + return client.Set(key, value); + } + } +} diff --git a/CC.Yi.Common/Cache/RedisCacheService.cs b/CC.Yi.Common/Cache/RedisCacheService.cs deleted file mode 100644 index c4dd22e9..00000000 --- a/CC.Yi.Common/Cache/RedisCacheService.cs +++ /dev/null @@ -1,96 +0,0 @@ -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) - }); - } - } - } -}