187 lines
6.0 KiB
C#
187 lines
6.0 KiB
C#
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
|
||
{
|
||
/// <summary>
|
||
/// 版 本 V2018.3.28
|
||
/// Copyright (c) 2013-2050 Hexagon PPM
|
||
/// 创建人:-系统开发组
|
||
/// 日 期:2017.03.06
|
||
/// 描 述:定义缓存接口。核心是<see cref="RedisCache"/>
|
||
/// 2018.4.6 bertchen 增加Redis 前缀
|
||
/// </summary>
|
||
public class CacheByRedis : ICache
|
||
{
|
||
/// <summary>
|
||
/// 24 9 4新增。
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="cacheKey"></param>
|
||
/// <param name="dbQuery"></param>
|
||
/// <param name="expireTime"></param>
|
||
/// <param name="dbId"></param>
|
||
/// <returns></returns>
|
||
public T GetRedisOrDBData<T>(string cacheKey, string projId, Func<T> 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<T>(cacheKey);
|
||
if (cachedData != null)
|
||
{
|
||
return cachedData;
|
||
}
|
||
|
||
// 如果缓存中没有命中,从数据库中查询
|
||
var dbData = dbQuery(); //运行委托,运行外面真正的sql逻辑。
|
||
|
||
// 将查询结果写入Redis缓存
|
||
if (dbData != null)
|
||
{
|
||
redisCache.StringSet<T>(cacheKey, dbData, expireTime);
|
||
}
|
||
return dbData;
|
||
}
|
||
|
||
#region Key-Value
|
||
/// <summary>
|
||
/// 读取缓存
|
||
/// </summary>
|
||
/// <param name="cacheKey">键</param>
|
||
/// <returns></returns>
|
||
public T Read<T>(string cacheKey, int dbId = 0) where T : class
|
||
{
|
||
return new RedisCache(dbId, null).StringGet<T>(cacheKey);
|
||
}
|
||
/// <summary>
|
||
/// 写入缓存
|
||
/// </summary>
|
||
/// <param name="value">对象数据</param>
|
||
/// <param name="cacheKey">键</param>
|
||
public void Write<T>(string cacheKey, T value, int dbId = 0) where T : class
|
||
{
|
||
new RedisCache(dbId, null).StringSet<T>(cacheKey, value);
|
||
}
|
||
/// <summary>
|
||
/// 写入缓存
|
||
/// </summary>
|
||
/// <param name="value">对象数据</param>
|
||
/// <param name="cacheKey">键</param>
|
||
/// <param name="expireTime">到期时间</param>
|
||
public void Write<T>(string cacheKey, T value, TimeSpan expireTime, int dbId = 0) where T : class
|
||
{
|
||
new RedisCache(dbId, null).StringSet<T>(cacheKey, value, expireTime);
|
||
}
|
||
/// <summary>
|
||
/// 移除指定数据缓存
|
||
/// </summary>
|
||
/// <param name="cacheKey">键</param>
|
||
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);
|
||
}
|
||
/// <summary>
|
||
/// 移除全部缓存
|
||
/// </summary>
|
||
public void RemoveAll(int dbId = 0)
|
||
{
|
||
new RedisCache().FlushDatabase(dbId);
|
||
}
|
||
#endregion
|
||
#region List
|
||
|
||
#region 同步方法
|
||
|
||
/// <summary>
|
||
/// 移除指定ListId的内部List的值
|
||
/// </summary>
|
||
/// <param name="key"></param>
|
||
/// <param name="value"></param>
|
||
public void ListRemove<T>(string cacheKey, T value, int dbId = 0) where T : class
|
||
{
|
||
new RedisCache(dbId, null).ListRemove<T>(cacheKey, value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取指定key的List
|
||
/// </summary>
|
||
/// <param name="key"></param>
|
||
/// <returns></returns>
|
||
public List<T> ListRange<T>(string cacheKey, int dbId = 0) where T : class
|
||
{
|
||
return new RedisCache(dbId, null).ListRange<T>(cacheKey);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 入队
|
||
/// </summary>
|
||
/// <param name="key"></param>
|
||
/// <param name="value"></param>
|
||
public void ListRightPush<T>(string cacheKey, T value, int dbId = 0) where T : class
|
||
{
|
||
new RedisCache(dbId, null).ListRightPush(cacheKey, value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 出队
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="key"></param>
|
||
/// <returns></returns>
|
||
public T ListRightPop<T>(string cacheKey, int dbId = 0) where T : class
|
||
{
|
||
return new RedisCache(dbId, null).ListRightPop<T>(cacheKey);
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 入栈
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="key"></param>
|
||
/// <param name="value"></param>
|
||
public void ListLeftPush<T>(string cacheKey, T value, int dbId = 0) where T : class
|
||
{
|
||
new RedisCache(dbId, null).ListLeftPush<T>(cacheKey, value);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 出栈
|
||
/// </summary>
|
||
/// <typeparam name="T"></typeparam>
|
||
/// <param name="key"></param>
|
||
/// <returns></returns>
|
||
public T ListLeftPop<T>(string cacheKey, int dbId = 0) where T : class
|
||
{
|
||
return new RedisCache(dbId, null).ListLeftPop<T>(cacheKey);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取集合中的数量
|
||
/// </summary>
|
||
/// <param name="key"></param>
|
||
/// <returns></returns>
|
||
public long ListLength(string cacheKey, int dbId = 0)
|
||
{
|
||
return new RedisCache(dbId, null).ListLength(cacheKey);
|
||
}
|
||
|
||
#endregion 同步方法
|
||
|
||
#endregion List
|
||
}
|
||
}
|