using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using CSRedis;
namespace ViewModels.Common
{
///
/// Redis缓存辅助类
///
public class CSRedisCacheHelper
{
public static CSRedisClient redis;
private static int SessionExpireMinutes = int.Parse(ReadConfig.Instance.get_session_expire_minutes);
//private static int RedisMaxReadPool = int.Parse(ConfigurationManager.AppSettings["redis_max_read_pool"]);
//private static int RedisMaxWritePool = int.Parse(ConfigurationManager.AppSettings["redis_max_write_pool"]);
//private const string ip = "127.0.0.1";
//private const string port = "6379";
//private const string preheat = "100"; // 设置预热连接数
//private const string connectTimeout = "100"; // 设置连接超时时间
//private const string tryit = "1"; // 设置重试次数
//private const string prefix = "CSRedisTest."; // 设置前缀
//private static readonly string _connectString = $"{ip}:{port},preheat={preheat},connectTimeout={connectTimeout},tryit={tryit},prefix={prefix}";
static CSRedisCacheHelper()
{
//var redisHostStr = ConfigurationManager.AppSettings["redis_server_session"];
var redisHostStr = ReadConfig.Instance.get_redis_server_session;
if (!string.IsNullOrEmpty(redisHostStr))
{
//redis = new CSRedisClient(redisHostStr);//+ ",password=,defaultDatabase=0,poolsize=500,ssl=false,writeBuffer=10240,prefix=");
//RedisHelper.Initialization(redis);
//redis = new CSRedisClient[2];
redis = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=1");
//redis[1] = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=1");
}
}
///
/// 添加缓存
///
///
///
///
public static void Set(string key, T value)
{
redis.Set(key, value, SessionExpireMinutes * 60);
}
public static T Get(string key)
{
return redis.Get(key);
}
public static void Forever(string key, T value)
{
redis.Set(key, value, -1);
}
public static void Del(string key)
{
redis.Del(key);
}
///
/// 判断是否存在
///
///
///
///
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;
}
///
/// 添加Hash缓存
///
///
///
///
public static void HMSet(string key, T value)
{
redis.HMSet(key, value);
}
///
/// 获取Hash缓存
///
///
///
///
public static T[] HMGet(string key)
{
return redis.HMGet(key);
}
///
/// 删除Hash缓存
///
///
///
///
public static long HDel(string key)
{
return redis.HDel(key);
}
}
}