redis

工作中写的一个redis缓存服务类

/// <summary>
/// 缓存服务
/// </summary>
public class CacheService
{
#region redis

/// <summary>
/// redis连接池
/// </summary>
private readonly static PooledRedisClientManager pooledClientManager;
/// <summary>
/// redis读服务器集合
/// </summary>
private static IEnumerable<string> readHosts;
/// <summary>
/// redis写服务器集合
/// </summary>
private static IEnumerable<string> writeHosts;

/// <summary>
/// 初始化redis服务
/// </summary>
static CacheService()
{
readHosts = InitRedisServerIP("read");
writeHosts = InitRedisServerIP("write");
pooledClientManager = new PooledRedisClientManager(writeHosts, readHosts);
}

/// <summary>
/// key是否存在
/// </summary>
/// <param name="key">key</param>
/// <returns>bool结果</returns>
public bool IsKeyExist(string key)
{
var result = false;
try
{
using (var redis = pooledClientManager.GetReadOnlyClient())
{
if (redis != null)
{
result = redis.ContainsKey(key);
}
}
}
catch (Exception ex)
{
//日志
}
return result;
}

/// <summary>
/// 根据传入的key,获取记录的值
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="key">key</param>
/// <param name="isWrite">是否写节点</param>
/// <returns>缓存对象</returns>
public T Get<T>(string key, bool isWrite = false)
{
using (var redis = (isWrite == false ? pooledClientManager.GetReadOnlyClient() : pooledClientManager.GetClient()))
{
if (redis != null)
{
return redis.Get<T>(key);
}
}
return default(T);
}

/// <summary>
/// 根据传入的key修改一条记录的值,当key不存在则添加
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="key">key</param>
/// <param name="value">实体值</param>
/// <returns>bool结果</returns>
public bool Set<T>(string key, T value)
{
using (var redis = pooledClientManager.GetClient())
{
if (redis != null)
{
return redis.Set(key, value);
}
}
return false;
}

/// <summary>
/// 根据传入的key 删除缓存
/// </summary>
/// <param name="key">key</param>
/// <returns>bool结果</returns>
public void Remove(string key)
{
using (var redis = pooledClientManager.GetClient())
{
if (redis != null)
{
redis.Remove(key);
}
}
}
#endregion

#region redis xml
/// <summary>
/// 读取redis服务器ip配置文件
/// </summary>
/// <param name="parentElement">父节点</param>
/// <returns>ip集合</returns>
private static IEnumerable<string> InitRedisServerIP(string parentElement)
{
var list = new List<string>();
try
{

//读配置文件
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"RedisCache.xml");
var xml = XElement.Load(path);
var elements = from ele in xml.Elements(parentElement)
select ele;

list.AddRange(elements.Select(element => element.Value));
}
catch (Exception ex)
{
LogService.Instance.Log(new LogRecordEntity
{
//日志
}
return list;
}
#endregion

#region 序列化
/// <summary>
/// 将对象序列化成字符串
/// </summary>
/// <typeparam name="T">对象类型</typeparam>
/// <param name="t">实体</param>
/// <returns>字符串</returns>
internal static string ToJson<T>(T t)
{
var timeConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };
return JsonConvert.SerializeObject(t, timeConverter);
}

/// <summary>
/// 将字符串转换为对象
/// </summary>
/// <typeparam name="T">指定转换的对象</typeparam>
/// <param name="str">待转换的object</param>
/// <returns>对象类型</returns>
internal static T ToModel<T>(string str) where T : class,new()
{
return str == null ? new T() : JsonConvert.DeserializeObject<T>(str);
}
#endregion

#region queue
/// <summary>
/// 获取队列中记录的数量
/// </summary>
/// <param name="listKey">队列id</param>
/// <returns>记录条数</returns>
public int GetListCount(string listKey)
{
var count = 0;
using (var redis = pooledClientManager.GetReadOnlyClient())
{
if (redis != null)
{
if (IsKeyExist(listKey))
{
count = (int)redis.GetListCount(listKey);
}
}
}
return count;
}

/// <summary>
/// 入队列
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="listKey">队列id</param>
/// <param name="value">对象</param>
public void Push<T>(string listKey, List<T> value)
{
using (var redis = pooledClientManager.GetClient())
{
if (redis != null)
{
value.ForEach(x => redis.EnqueueItemOnList(listKey, ToJson(x)));
}
}
}
/// <summary>
/// 出队列
/// </summary>
/// <typeparam name="T">类型</typeparam>
/// <param name="listKey">队列id</param>
/// <param name="count">出队列数量</param>
/// <returns>对象实体</returns>
public List<T> Pull<T>(string listKey, int count) where T : class,new()
{
var result = new List<T>();
using (var redis = pooledClientManager.GetClient())
{
if (redis != null)
{
for (int i = 0; i < count; i++)
{
var entity = redis.DequeueItemFromList(listKey);
if (entity == null)
break;
result.Add(ToModel<T>(entity));
}
}
}
return result;
}
#endregion
}

 

附上xml配置

<?xml version="1.0" encoding="utf-8" ?>
<!--redis 缓存服务器-->
<redis>
<!--读节点-->
<read>
<ip>192.168.0.1:6779</ip>
<!--<ip>192.168.0.1:6779</ip>-->
<!--<ip>192.168.0.1:6779</ip>-->
</read>
<!--写节点-->
<write>
<ip>192.168.0.1:6779</ip>
<!--<ip>192.168.0.1:6779</ip>-->
<!--<ip>192.168.0.1:6779</ip-->
</write>
</redis>

posted @ 2015-03-07 11:43  茂茂  阅读(242)  评论(0编辑  收藏  举报