|
|
|
|
using FreeRedis;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text.Json;
|
|
|
|
|
using Common.Extensions;
|
|
|
|
|
|
|
|
|
|
namespace Common.Tools
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ysredis工具 必须要配置在配置文件中配置redis链接 或指定
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class YsRedisHelp
|
|
|
|
|
{
|
|
|
|
|
private static string _RedisContext = sysOptionConfig.Webconfig.Redis;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Redis 地址 默认使用 sysOptionConfig.Webconfig.Redis 配置信息的
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static string RedisContext { set {
|
|
|
|
|
_RedisContext = value;
|
|
|
|
|
}
|
|
|
|
|
get { return _RedisContext; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// rendisDB
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static RedisClient DbRedis = new RedisClient(RedisContext);
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 删除单个键值对
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="Key"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static long Del(string Key)
|
|
|
|
|
{
|
|
|
|
|
return DbRedis.Del(Key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 清理redis键值对
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="keymodel">key样式 xxx* *匹配符</param>
|
|
|
|
|
public static long RedisClena(string keymodel)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var keylist = DbRedis.Keys(keymodel);
|
|
|
|
|
return DbRedis.Del(keylist);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 清理统计redis键值 谨慎清理建议指定特定时间执行一次
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static long RedisClenaRuncount() {
|
|
|
|
|
return RedisClena("runcount_*");
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
///增加 通过redis统计计数
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="key"></param>
|
|
|
|
|
public static void RunCountAdd(string key)
|
|
|
|
|
{
|
|
|
|
|
key = "runcount_" + key;
|
|
|
|
|
DbRedis.LPush(key, DateTime.Now.ToTimeStamp());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取过去分钟内的统计量
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="key"></param>
|
|
|
|
|
/// <param name="TimeMinute">分钟数</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static int GetRunCountTime(string key, int TimeMinute)
|
|
|
|
|
{
|
|
|
|
|
return GetRunCountTime(key, DateTime.Now.AddMinutes(-TimeMinute));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 获取指定时间内的数量
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="key"></param>
|
|
|
|
|
/// <param name=""></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static int GetRunCountTime(string key, DateTime StartTime)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
key = "runcount_" + key;
|
|
|
|
|
var _time = StartTime.ToTimeStamp();
|
|
|
|
|
var listarry = DbRedis.LRange(key, 0, DbRedis.LLen(key) - 1);
|
|
|
|
|
if (listarry != null)
|
|
|
|
|
{
|
|
|
|
|
return listarry.ToList().ConvertAll(s => long.Parse(s)).Count(s => s >= _time);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch { return 0; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// redis 缓存写入
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="setData"></param>
|
|
|
|
|
/// <param name="Key"></param>
|
|
|
|
|
/// <param name="TimeOut">过期秒数</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static bool RedisSet<T>(string Key, T setData, int TimeOut = 0)
|
|
|
|
|
{
|
|
|
|
|
var val = "";
|
|
|
|
|
if (typeof(T) != typeof(string))
|
|
|
|
|
{
|
|
|
|
|
val = JsonSerializer.Serialize(setData);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
val = setData.ToString();
|
|
|
|
|
}
|
|
|
|
|
if (TimeOut > 0)
|
|
|
|
|
{
|
|
|
|
|
DbRedis.Set(Key, val, TimeOut);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
DbRedis.Set(Key, val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 读取redis中的对象数据 不存在返回null
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="Key"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public static T RedisGet<T>(string Key)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
var val = DbRedis.Get(Key);
|
|
|
|
|
if (val.IsNull())
|
|
|
|
|
{
|
|
|
|
|
return default(T);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return Json.JsonToObject<T>(val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|