85 lines
3.0 KiB
C#
85 lines
3.0 KiB
C#
using HZQR.Common;
|
||
using RedisHelp;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace EShang.Common
|
||
{
|
||
/// <summary>
|
||
/// redis帮助类
|
||
/// </summary>
|
||
public class Redis
|
||
{
|
||
#region 方法 -> string类型缓存操作
|
||
/// <summary>
|
||
/// 写入redis
|
||
/// </summary>
|
||
/// <param name="dbNum">redis用下标指定数据库</param>
|
||
/// <param name="key">redis中的key</param>
|
||
/// <param name="model">数据实体对象</param>
|
||
/// <param name="validDay">有效期,按天</param>
|
||
public static void StringSet(int dbNum, string key, object model, int? validDay = null)
|
||
{
|
||
RedisHelper redisDb = new RedisHelper(dbNum);//从redis中取
|
||
//写入redis,后面的逻辑判断都从redis中取
|
||
TimeSpan? ts = null;
|
||
if (validDay != null)
|
||
{
|
||
DateTime dt1 = DateTime.Now;
|
||
DateTime dt2 = dt1.AddDays(validDay.TryParseToInt());//redis中存放的有效期
|
||
ts = dt2 - dt1;
|
||
|
||
}
|
||
redisDb.StringSet(key, model, ts);//键值对写入redis
|
||
}
|
||
|
||
public static T StringGet<T>(int dbNum, string key)
|
||
{
|
||
RedisHelper redisDb = new RedisHelper(dbNum);//从redis中取
|
||
if (redisDb.KeyExists(key))//判断redis缓存中是否存在key
|
||
{
|
||
return redisDb.StringGet<T>(key);//根据key取出redis中的缓存
|
||
}
|
||
return default(T);
|
||
}
|
||
#endregion
|
||
|
||
#region 方法 -> hash类型缓存操作
|
||
/// <summary>
|
||
/// 将hash数据类型写入redis
|
||
/// </summary>
|
||
/// <param name="dbNum">redis用下标指定数据库</param>
|
||
/// <param name="key">redis中的key</param>
|
||
/// <param name="dataKey">数据值</param>
|
||
/// <param name="model">数据实体对象</param>
|
||
public static bool HashSet(int dbNum, string key, string dataKey, object model)
|
||
{
|
||
RedisHelper redisDb = new RedisHelper(dbNum);//从redis中取
|
||
//写入redis,后面的逻辑判断都从redis中取
|
||
return redisDb.HashSet(key, dataKey, model);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将hash数据类型写入redis
|
||
/// </summary>
|
||
/// <typeparam name="T">泛型对象</typeparam>
|
||
/// <param name="dbNum">redis用下标指定数据库</param>
|
||
/// <param name="key">redis中的key</param>
|
||
/// <param name="dataKey">数据值</param>
|
||
/// <returns></returns>
|
||
public static T HashGet<T>(int dbNum, string key, string dataKey)
|
||
{
|
||
RedisHelper redisDb = new RedisHelper(dbNum);//从redis中取
|
||
if (redisDb.HashExists(key, dataKey))//判断redis缓存中是否存在key
|
||
{
|
||
return redisDb.HashGet<T>(key, dataKey);//根据key取出redis中的缓存
|
||
}
|
||
return default(T);
|
||
}
|
||
#endregion
|
||
}
|
||
}
|