|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.Redis;
namespace Com.QFGame.QNX.Community.Redis
{
public class RedisBase
{
private static string RedisPath = System.Configuration.ConfigurationSettings.AppSettings["RedisPath"];
#region -- 连接信息 --
//10.0.18.8:6379
public static PooledRedisClientManager prcm = CreateManager(new string[] { RedisPath }, new string[] { RedisPath });
private static PooledRedisClientManager CreateManager(string[] readWriteHosts, string[] readOnlyHosts)
{
// 支持读写分离,均衡负载
return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig
{
MaxWritePoolSize = 5, // “写”链接池链接数
MaxReadPoolSize = 5, // “读”链接池链接数
AutoStart = true,
});
}
#endregion
#region -- Item --
///
/// 设置单体
///
///
///
///
///
///
public static bool Item_Set(string key, T t)
{
try
{
using (IRedisClient redis = prcm.GetClient())
{
return redis.Set(key, t, new TimeSpan(1, 0, 0));
}
}
catch (Exception ex)
{
// LogInfo
}
return false;
}
///
/// 获取单体
///
///
///
///
public static T Item_Get(string key) where T : class
{
using (IRedisClient redis = prcm.GetClient())
{
return redis.Get(key);
}
}
///
/// 移除单体
///
///
public static bool Item_Remove(string key)
{
using (IRedisClient redis = prcm.GetClient())
{
return redis.Remove(key);
}
}
#endregion
#region -- List --
public static void List_Add(string key, T t)
{
using (IRedisClient redis = prcm.GetClient())
{
var redisTypedClient = redis.GetTypedClient();
redisTypedClient.AddItemToList(redisTypedClient.Lists[key], t);
}
}
public static bool List_Remove(string key, T t)
{
using (IRedisClient redis = prcm.GetClient())
{
var redisTypedClient = redis.GetTypedClient();
return redisTypedClient.RemoveItemFromList(redisTypedClient.Lists[key], t) > 0;
}
}
public static void List_RemoveAll(string key)
{
using (IRedisClient redis = prcm.GetClient())
{
var redisTypedClient = redis.GetTypedClient();
redisTypedClient.Lists[key].RemoveAll();
}
}
public static int List_Count(string key)
{
using (IRedisClient redis = prcm.GetClient())
{
return redis.GetListCount(key);
}
}
public static List List_GetRange(string key, int start, int count)
{
using (IRedisClient redis = prcm.GetClient())
{
var c = redis.GetTypedClient();
return c.Lists[key].GetRange(start, start + count - 1);
}
}
public static List List_GetList(string key)
{
using (IRedisClient redis = prcm.GetClient())
{
var c = redis.GetTypedClient();
return c.Lists[key].GetRange(0, c.Lists[key].Count);
}
}
public static List List_GetList(string key, int pageIndex, int pageSize)
{
int start = pageSize * (pageIndex - 1);
return List_GetRange(key, start, pageSize);
}
///
/// 设置缓存过期
///
///
///
public static void List_SetExpire(string key, DateTime datetime)
{
using (IRedisClient redis = prcm.GetClient())
{
redis.ExpireEntryAt(key, datetime);
}
}
#endregion
#region -- Set --
public static void Set_Add(string key, T t)
{
using (IRedisClient redis = prcm.GetClient())
{
var redisTypedClient = redis.GetTypedClient();
redisTypedClient.Sets[key].Add(t);
}
}
public static bool Set_Contains(string key, T t)
{
using (IRedisClient redis = prcm.GetClient())
{
var redisTypedClient = redis.GetTypedClient();
return redisTypedClient.Sets[key].Contains(t);
}
}
public static bool Set_Remove(string key, T t)
{
using (IRedisClient redis = prcm.GetClient())
{
var redisTypedClient = redis.GetTypedClient();
return redisTypedClient.Sets[key].Remove(t);
}
}
#endregion
#region -- Hash --
///
/// 判断某个数据是否已经被缓存
///
///
///
///
///
public static bool Hash_Exist(string key, string dataKey)
{
using (IRedisClient redis = prcm.GetClient())
{
return redis.HashContainsEntry(key, dataKey);
}
}
///
/// 存储数据到hash表
///
///
///
///
///
public static bool Hash_Set(string key, string dataKey, T t)
{
using (IRedisClient redis = prcm.GetClient())
{
string value = ServiceStack.Text.JsonSerializer.SerializeToString(t);
return redis.SetEntryInHash(key, dataKey, value);
}
}
///
/// 移除hash中的某值
///
///
///
///
///
public static bool Hash_Remove(string key, string dataKey)
{
using (IRedisClient redis = prcm.GetClient())
{
return redis.RemoveEntryFromHash(key, dataKey);
}
}
///
/// 移除整个hash
///
///
///
///
///
public static bool Hash_Remove(string key)
{
using (IRedisClient redis = prcm.GetClient())
{
return redis.Remove(key);
}
}
///
/// 从hash表获取数据
///
///
///
///
///
public static T Hash_Get(string key, string dataKey)
{
using (IRedisClient redis = prcm.GetClient())
{
string value = redis.GetValueFromHash(key, dataKey);
return ServiceStack.Text.JsonSerializer.DeserializeFromString(value);
}
}
///
/// 获取整个hash的数据
///
///
///
///
public static List Hash_GetAll(string key)
{
using (IRedisClient redis = prcm.GetClient())
{
var list = redis.GetHashValues(key);
if (list != null && list.Count > 0)
{
List result = new List();
foreach (var item in list)
{
var value = ServiceStack.Text.JsonSerializer.DeserializeFromString(item);
result.Add(value);
}
return result;
}
return null;
}
}
///
/// 设置缓存过期
///
///
///
public static void Hash_SetExpire(string key, DateTime datetime)
{
using (IRedisClient redis = prcm.GetClient())
{
redis.ExpireEntryAt(key, datetime);
}
}
#endregion
#region -- SortedSet --
///
/// 添加数据到 SortedSet
///
///
///
///
///
public static bool SortedSet_Add(string key, T t, double score)
{
using (IRedisClient redis = prcm.GetClient())
{
string value = ServiceStack.Text.JsonSerializer.SerializeToString(t);
return redis.AddItemToSortedSet(key, value, score);
}
}
///
/// 移除数据从SortedSet
///
///
///
///
///
public static bool SortedSet_Remove(string key, T t)
{
using (IRedisClient redis = prcm.GetClient())
{
string value = ServiceStack.Text.JsonSerializer.SerializeToString(t);
return redis.RemoveItemFromSortedSet(key, value);
}
}
///
/// 修剪SortedSet
///
///
/// 保留的条数
///
public static int SortedSet_Trim(string key, int size)
{
using (IRedisClient redis = prcm.GetClient())
{
return redis.RemoveRangeFromSortedSet(key, size, 9999999);
}
}
///
/// 获取SortedSet的长度
///
///
///
public static int SortedSet_Count(string key)
{
using (IRedisClient redis = prcm.GetClient())
{
return redis.GetSortedSetCount(key);
}
}
///
/// 获取SortedSet的分页数据
///
///
///
///
///
///
public static List SortedSet_GetList(string key, int pageIndex, int pageSize)
{
using (IRedisClient redis = prcm.GetClient())
{
var list = redis.GetRangeFromSortedSet(key, (pageIndex - 1) * pageSize, pageIndex * pageSize - 1);
if (list != null && list.Count > 0)
{
List result = new List();
foreach (var item in list)
{
var data = ServiceStack.Text.JsonSerializer.DeserializeFromString(item);
result.Add(data);
}
return result;
}
}
return null;
}
///
/// 获取SortedSet的全部数据
///
///
///
///
///
///
public static List SortedSet_GetListALL(string key)
{
using (IRedisClient redis = prcm.GetClient())
{
var list = redis.GetRangeFromSortedSet(key, 0, 9999999);
if (list != null && list.Count > 0)
{
List result = new List();
foreach (var item in list)
{
var data = ServiceStack.Text.JsonSerializer.DeserializeFromString(item);
result.Add(data);
}
return result;
}
}
return null;
}
///
/// 设置缓存过期
///
///
///
public static void SortedSet_SetExpire(string key, DateTime datetime)
{
using (IRedisClient redis = prcm.GetClient())
{
redis.ExpireEntryAt(key, datetime);
}
}
//public static double SortedSet_GetItemScore(string key,T t)
//{
// using (IRedisClient redis = prcm.GetClient())
// {
// var data = ServiceStack.Text.JsonSerializer.SerializeToString(t);
// return redis.GetItemScoreInSortedSet(key, data);
// }
// return 0;
//}
#endregion
}
} |
|