using Learun.Cache.Base; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Configuration; using System.Threading.Tasks; namespace Learun.Cache.Redis { /// /// 版 本 V2018.3.28 /// Copyright (c) 2013-2050 Hexagon PPM /// 创建人:-系统开发组 /// 日 期:2017.03.06 /// 描 述:定义缓存接口。核心是 /// 2018.4.6 bertchen 增加Redis 前缀 /// public class CacheByRedis : ICache { /// /// 24 9 4新增。 /// /// /// /// /// /// /// public T GetRedisOrDBData(string cacheKey, string projId, Func dbQuery, TimeSpan? expireTime = null, int dbId = 0) { cacheKey = cacheKey + projId; RedisCache redisCache = new RedisCache(dbId, null); if (expireTime == null) { expireTime = TimeSpan.FromHours(1); // 如果参数为null,设置为1小时 } // 先从Redis缓存中获取数据 var cachedData = redisCache.StringGet(cacheKey); if (cachedData != null) { return cachedData; } // 如果缓存中没有命中,从数据库中查询 var dbData = dbQuery(); //运行委托,运行外面真正的sql逻辑。 // 将查询结果写入Redis缓存 if (dbData != null) { redisCache.StringSet(cacheKey, dbData, expireTime); } return dbData; } #region Key-Value /// /// 读取缓存 /// /// 键 /// public T Read(string cacheKey, int dbId = 0) where T : class { return new RedisCache(dbId, null).StringGet(cacheKey); } /// /// 写入缓存 /// /// 对象数据 /// 键 public void Write(string cacheKey, T value, int dbId = 0) where T : class { new RedisCache(dbId, null).StringSet(cacheKey, value); } /// /// 写入缓存 /// /// 对象数据 /// 键 /// 到期时间 public void Write(string cacheKey, T value, TimeSpan expireTime, int dbId = 0) where T : class { new RedisCache(dbId, null).StringSet(cacheKey, value, expireTime); } /// /// 移除指定数据缓存 /// /// 键 public void Remove(string cacheKey, int dbId = 0) { new RedisCache(dbId, null).KeyDelete(cacheKey); } public void Remove(string cacheKey, string projId, int dbId = 0) { new RedisCache(dbId, null).KeyDelete(cacheKey + projId); } /// /// 移除全部缓存 /// public void RemoveAll(int dbId = 0) { new RedisCache().FlushDatabase(dbId); } #endregion #region List #region 同步方法 /// /// 移除指定ListId的内部List的值 /// /// /// public void ListRemove(string cacheKey, T value, int dbId = 0) where T : class { new RedisCache(dbId, null).ListRemove(cacheKey, value); } /// /// 获取指定key的List /// /// /// public List ListRange(string cacheKey, int dbId = 0) where T : class { return new RedisCache(dbId, null).ListRange(cacheKey); } /// /// 入队 /// /// /// public void ListRightPush(string cacheKey, T value, int dbId = 0) where T : class { new RedisCache(dbId, null).ListRightPush(cacheKey, value); } /// /// 出队 /// /// /// /// public T ListRightPop(string cacheKey, int dbId = 0) where T : class { return new RedisCache(dbId, null).ListRightPop(cacheKey); } /// /// 入栈 /// /// /// /// public void ListLeftPush(string cacheKey, T value, int dbId = 0) where T : class { new RedisCache(dbId, null).ListLeftPush(cacheKey, value); } /// /// 出栈 /// /// /// /// public T ListLeftPop(string cacheKey, int dbId = 0) where T : class { return new RedisCache(dbId, null).ListLeftPop(cacheKey); } /// /// 获取集合中的数量 /// /// /// public long ListLength(string cacheKey, int dbId = 0) { return new RedisCache(dbId, null).ListLength(cacheKey); } #endregion 同步方法 #endregion List } }