111 lines
3.2 KiB
C#
111 lines
3.2 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Configuration;
|
|||
|
|
using CSRedis;
|
|||
|
|
|
|||
|
|
namespace Common
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// Redis缓存辅助类
|
|||
|
|
/// </summary>
|
|||
|
|
public class CSRedisCacheHelper
|
|||
|
|
{
|
|||
|
|
public static CSRedisClient redis;
|
|||
|
|
private static int SessionExpireMinutes = 60;
|
|||
|
|
private const string ip = "127.0.0.1";
|
|||
|
|
private const string port = "7555";
|
|||
|
|
public static string password = "cw_oy_lsh";
|
|||
|
|
|
|||
|
|
static CSRedisCacheHelper()
|
|||
|
|
{
|
|||
|
|
var redisHostStr = string.Format("{0}:{1}",ip,port);
|
|||
|
|
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 + string.Format(",password={0},defaultDatabase=1",password));
|
|||
|
|
//redis[1] = new CSRedisClient(redisHostStr + ",password=,defaultDatabase=1");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <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 Forever<T>(string key, T value)
|
|||
|
|
{
|
|||
|
|
redis.Set(key, value, -1);
|
|||
|
|
}
|
|||
|
|
public static void Del(string key)
|
|||
|
|
{
|
|||
|
|
redis.Del(key);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <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;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 添加Hash缓存
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T"></typeparam>
|
|||
|
|
/// <param name="key"></param>
|
|||
|
|
/// <param name="value"></param>
|
|||
|
|
public static void HMSet<T>(string key, T value)
|
|||
|
|
{
|
|||
|
|
redis.HMSet(key, value);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 获取Hash缓存
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T"></typeparam>
|
|||
|
|
/// <param name="key"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static T[] HMGet<T>(string key)
|
|||
|
|
{
|
|||
|
|
return redis.HMGet<T>(key);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// 删除Hash缓存
|
|||
|
|
/// </summary>
|
|||
|
|
/// <typeparam name="T"></typeparam>
|
|||
|
|
/// <param name="key"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static long HDel<T>(string key)
|
|||
|
|
{
|
|||
|
|
return redis.HDel(key);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|