namedhao 发表于 2017-12-21 20:52:17

c#使用 NServiceKit.Redis 封装 RedisHelper

using System;  

using System.Collections.Generic;  

using System.Configuration;  

using System.Linq;  

using NServiceKit.Redis;  

using NServiceKit.Redis.Support;  

  

namespace>
{public>{/// <summary>  /// redis客户端连接池信息
  /// </summary>
  private PooledRedisClientManager prcm;
  

  public Redis()
  {
  CreateManager();
  

  }
  /// <summary>
  /// 创建链接池管理对象
  /// </summary>
  private void CreateManager()
  {
  try
  {
  // ip1:端口1,ip2:端口2
  var serverlist = ConfigurationManager.AppSettings["RedisServer"].Split(',');
  prcm = new PooledRedisClientManager(serverlist, serverlist,
  new RedisClientManagerConfig
  {
  MaxWritePoolSize = 32,
  MaxReadPoolSize = 32,
  AutoStart = true
  });
  //    prcm.Start();
  
            }
  catch (Exception e)
  {
  
#if DEBUG
  throw;
  
#endif
  }
  }
  /// <summary>
  /// 客户端缓存操作对象
  /// </summary>
  public IRedisClient GetClient()
  {
  if (prcm == null)
  CreateManager();
  

  return prcm.GetClient();
  }
  /// <summary>
  /// 删除
  /// </summary>
  /// <param name="key"></param>
  /// <returns></returns>
  public bool Remove(string key)
  {
  using (var client = prcm.GetClient())
  {
  return client.Remove(key);
  }
  }
  /// <summary>
  /// 获取
  /// </summary>
  /// <param name="key"></param>
  /// <returns></returns>
  public object Get(string key)
  {
  using (var client = prcm.GetClient())
  {
  var bytes = client.Get<byte[]>(key);
  var obj = new ObjectSerializer().Deserialize(bytes);
  return obj;
  }
  }
  /// <summary>
  /// 获取
  /// </summary>
  /// <typeparam name="T"></typeparam>
  /// <param name="key"></param>
  /// <returns></returns>

  public T GetT<T>(string key) where T :>  {
  //return Get(key) as T;
  using (var client = prcm.GetClient())
  {
  return client.Get<T>(key);
  }
  }
  /// <summary>
  /// 获取到值到内存中,在删除
  /// </summary>
  /// <param name="key"></param>
  /// <returns></returns>
  public object GetWithDelete(string key)
  {
  var result = Get(key);
  if (result != null)
  Remove(key);
  return result;
  }
  /// <summary>
  /// 获取到值到内存中,在删除
  /// </summary>
  /// <typeparam name="T"></typeparam>
  /// <param name="key"></param>
  /// <returns></returns>

  public T GetWithDelete<T>(string key) where T :>  {
  var result = GetT<T>(key);
  if (result != null)
  Remove(key);
  return result;
  }
  /// <summary>
  /// 写
  /// </summary>
  /// <param name="key"></param>
  /// <param name="value"></param>
  /// <returns></returns>
  public bool Set(string key, object value)
  {
  using (var client = prcm.GetClient())
  {
  if (client.ContainsKey(key))
  {
  return client.Set<byte[]>(key, new ObjectSerializer().Serialize(value));
  }
  else
  {
  return client.Add<byte[]>(key, new ObjectSerializer().Serialize(value));
  }
  }
  

  }
  /// <summary>
  /// 写
  /// </summary>
  /// <param name="key"></param>
  /// <param name="value"></param>
  /// <param name="expireTime"></param>
  /// <returns></returns>
  public bool Set(string key, object value, DateTime expireTime)
  {
  using (var client = prcm.GetClient())
  {
  if (client.ContainsKey(key))
  {
  return client.Set<byte[]>(key, new ObjectSerializer().Serialize(value), expireTime);
  }
  else
  {
  return client.Add<byte[]>(key, new ObjectSerializer().Serialize(value), expireTime);
  }
  

  }
  }
  /// <summary>
  /// 写
  /// </summary>
  /// <typeparam name="T"></typeparam>
  /// <param name="key"></param>
  /// <param name="value"></param>
  /// <param name="expire"></param>
  /// <returns></returns>

  public bool SetT<T>(string key, T value, DateTime expire) where T :>  {
  try
  {
  using (var client = prcm.GetClient())
  {
  return client.Set<T>(key, value, expire);
  }
  }
  catch
  {
  return false;
  }
  }
  /// <summary>
  /// 写
  /// </summary>
  /// <typeparam name="T"></typeparam>
  /// <param name="key"></param>
  /// <param name="value"></param>
  /// <returns></returns>

  public bool SetT<T>(string key, T value) where T :>  {
  try
  {
  using (var client = prcm.GetClient())
  {
  return client.Set<T>(key, value);
  }
  }
  catch
  {
  return false;
  }
  }
  

  public void Dispose()
  {
  Close();
  }
  

  public void Close()
  {
  var client = prcm.GetClient();
  prcm.Dispose();
  }
  

  }
  
}
页: [1]
查看完整版本: c#使用 NServiceKit.Redis 封装 RedisHelper