初始化
This commit is contained in:
220
Common/CSRedisCacheHelper.cs
Normal file
220
Common/CSRedisCacheHelper.cs
Normal file
@@ -0,0 +1,220 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Configuration;
|
||||
using CSRedis;
|
||||
using LogCap.Common;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Common
|
||||
{
|
||||
/// <summary>
|
||||
/// Redis缓存辅助类
|
||||
/// </summary>
|
||||
public class CSRedisCacheHelper
|
||||
{
|
||||
|
||||
public static CSRedisClient? redis = null;
|
||||
public static CSRedisClient? redis1 = null;
|
||||
public static CSRedisClient? redis2 = null;
|
||||
public static CSRedisClient? redis3 = null;
|
||||
public static CSRedisClient? redis4 = null;
|
||||
public static CSRedisClient? redis5 = null;
|
||||
public static int SessionExpireMinutes = 5;
|
||||
|
||||
public static IConfiguration Configuration { get; set; }
|
||||
|
||||
static CSRedisCacheHelper()
|
||||
{
|
||||
var redisHostStr = ReadConfig.Instance.get_redis_server_session;
|
||||
var get_redis_auth = ReadConfig.Instance.get_redis_auth;
|
||||
if (!string.IsNullOrEmpty(redisHostStr))
|
||||
{
|
||||
redis = new CSRedisClient(redisHostStr + string.Format(",password={0},defaultDatabase=0", get_redis_auth));
|
||||
redis1 = new CSRedisClient(redisHostStr + string.Format(",password={0},defaultDatabase=1", get_redis_auth));
|
||||
redis2 = new CSRedisClient(redisHostStr + string.Format(",password={0},defaultDatabase=2", get_redis_auth));
|
||||
redis3 = new CSRedisClient(redisHostStr + string.Format(",password={0},defaultDatabase=3", get_redis_auth));
|
||||
redis4 = new CSRedisClient(redisHostStr + string.Format(",password={0},defaultDatabase=4", get_redis_auth));
|
||||
redis5 = new CSRedisClient(redisHostStr + string.Format(",password={0},defaultDatabase=5", get_redis_auth));
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 添加缓存
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
public static void Set<T>(string key, T value)
|
||||
{
|
||||
redis.Set(key, value, SessionExpireMinutes * 60);
|
||||
}
|
||||
|
||||
public static T Get<T>(string key)
|
||||
{
|
||||
return redis.Get<T>(key);
|
||||
}
|
||||
public static void Del(string[] key)
|
||||
{
|
||||
redis.Del(key.ToArray());
|
||||
}
|
||||
|
||||
public static T ForeverGet<T>(string key)
|
||||
{
|
||||
return redis1.Get<T>(key);
|
||||
}
|
||||
|
||||
public static void Forever<T>(string key, T value)
|
||||
{
|
||||
redis1.Set(key, value, 0);
|
||||
}
|
||||
|
||||
public static T Get_Partition<T>(string key, int SliceNo = 2)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
return client.Get<T>(key);
|
||||
}
|
||||
public static void Set_Partition<T>(string key, T value, int SliceNo = 2, int ExpireMinutes = 0)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
client.Set(key, value, ExpireMinutes * 60);
|
||||
}
|
||||
public static void Del_Partition(string[] key, int SliceNo = 2)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
client.Del(key);
|
||||
}
|
||||
public static bool Contains_Partition(string key, int SliceNo = 2)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
bool result = client.Exists(key);
|
||||
return result;
|
||||
}
|
||||
private static CSRedisClient WhitchRedisSlice(int SliceNo)
|
||||
{
|
||||
CSRedisClient client = null;
|
||||
if (SliceNo == 1)
|
||||
{
|
||||
client = redis1;
|
||||
}
|
||||
else if (SliceNo == 2)
|
||||
{
|
||||
client = redis2;
|
||||
}
|
||||
else
|
||||
{
|
||||
client = redis;
|
||||
}
|
||||
return client;
|
||||
}
|
||||
|
||||
public static long GetForever_ExpireMinutes<T>(string key)
|
||||
{
|
||||
return redis.Ttl(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static T Get<T>(string key, string mac)
|
||||
{
|
||||
T obj = redis.Get<T>(mac);
|
||||
if (obj == null)
|
||||
{
|
||||
return redis.Get<T>(key);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
/// <summary>
|
||||
/// 判断是否存在
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="mac"></param>
|
||||
/// <returns></returns>
|
||||
public static bool Contains(string key, string mac)
|
||||
{
|
||||
bool result = redis.Exists(mac);
|
||||
if (!result)
|
||||
{
|
||||
result = redis.Exists(key);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static bool Contains(string key)
|
||||
{
|
||||
bool result = redis.Exists(key);
|
||||
return result;
|
||||
}
|
||||
public static void Publish(string Topic, string Payload)
|
||||
{
|
||||
redis.PublishAsync(Topic,Payload);
|
||||
}
|
||||
/// <summary>
|
||||
/// 添加Hash缓存
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <param name="value"></param>
|
||||
public static void HMSet(int SliceNo, string key, params object[] value)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
client.HMSet(key, value);
|
||||
}
|
||||
|
||||
public static void HMSet(int SliceNo, int ExpireTime_M, string key, params object[] value)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
client.HMSet(key, value);
|
||||
client.Expire(key, new TimeSpan(0, ExpireTime_M, 0));
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取Hash缓存
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static T[] HMGet<T>(int SliceNo, string key, string KeyV)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
return client.HMGet<T>(key, KeyV);
|
||||
}
|
||||
public static Dictionary<string, string> HMGetAll(int SliceNo, string key)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
return client.HGetAll(key);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 删除Hash缓存
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static long HDelAll<T>(int SliceNo, string key)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
return client.HDel(key);
|
||||
}
|
||||
public static long HDel(int SliceNo, string key, string key1)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
return client.HDel(key, key1);
|
||||
}
|
||||
|
||||
public static void ListAdd(int SliceNo, string key, params object[] obj)
|
||||
{
|
||||
CSRedisClient client = WhitchRedisSlice(SliceNo);
|
||||
client.LPush(key, obj);
|
||||
}
|
||||
|
||||
|
||||
public static void GenericSerNo(string key)
|
||||
{
|
||||
redis1.IncrBy(key, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user