一、redis自定义配置节点

  <configSections>
<section name ="RedisConfig" type="Amy.Toolkit.RedisCache.SectionHandler"/>
</configSections> <RedisConfig>
<add key="ReadWriteHosts" value="192.168.1.101:6379"></add>
<add key="ReadOnlyHosts" value="192.168.1.101:6379"></add>
<add key="MaxWritePoolSize" value=""></add>
<add key="MaxReadPoolSize" value=""></add>
</RedisConfig>

二、实现自定义配置功能

    public class Config
{
/// <summary>
/// “写”服务器地址
/// </summary>
public string[] ReadWriteHosts { get; set; } /// <summary>
/// “读”服务器地址
/// </summary>
public string[] ReadOnlyHosts { get; set; } /// <summary>
/// “写”链接池链接数
/// </summary>
public int MaxWritePoolSize { get; set; } /// <summary>
/// “读”链接池链接数
/// </summary>
public int MaxReadPoolSize { get; set; }
} public class SectionHandler : IConfigurationSectionHandler
{
public object Create(object parent, object configContext, XmlNode section)
{
Config config = new Config(); //解析配置文件信息,返回对象
if (section != null)
{
foreach (XmlNode item in section.ChildNodes)
{
switch (item.Attributes["key"].InnerText)
{
case "ReadWriteHosts":
config.ReadWriteHosts = item.Attributes["value"].InnerText.Split(';');
break;
case "ReadOnlyHosts":
config.ReadOnlyHosts = item.Attributes["value"].InnerText.Split(';');
break;
case "MaxWritePoolSize":
config.MaxWritePoolSize = string.IsNullOrEmpty(item.Attributes["value"].InnerText) ? : item.Attributes["value"].InnerText.ToInt32();
break;
case "MaxReadPoolSize":
config.MaxReadPoolSize = string.IsNullOrEmpty(item.Attributes["value"].InnerText) ? : item.Attributes["value"].InnerText.ToInt32();
break;
default:
config.ReadWriteHosts = new string[] { };
config.ReadOnlyHosts = new string[] { };
config.MaxWritePoolSize = ;
config.MaxReadPoolSize = ;
break;
} }
}
return config;
}
}

三、实现redis客户端功能

    public class RedisFactory
{
private static Config config = (Config)System.Configuration.ConfigurationManager.GetSection("RedisConfig"); #region -- 连接信息 --
public static PooledRedisClientManager prcm = new PooledRedisClientManager(config.ReadWriteHosts, config.ReadOnlyHosts, new RedisClientManagerConfig
{
MaxWritePoolSize = config.MaxWritePoolSize, // “写”链接池链接数
MaxReadPoolSize = config.MaxReadPoolSize, // “读”链接池链接数
AutoStart = true,
}); #endregion #region -- Item --
/// <summary>
/// 设置单体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="t"></param>
/// <param name="timeSpan"></param>
/// <returns></returns>
public static bool Item_Set<T>(string key, T t)
{
try
{
using (IRedisClient redis = prcm.GetClient())
{
return redis.Set<T>(key, t, new TimeSpan(, , ));
}
}
catch (Exception ex)
{
// LogInfo
}
return false;
} /// <summary>
/// 获取单体
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public static T Item_Get<T>(string key) where T : class
{
using (IRedisClient redis = prcm.GetReadOnlyClient())
{
return redis.Get<T>(key);
}
} /// <summary>
/// 移除单体
/// </summary>
/// <param name="key"></param>
public static bool Item_Remove(string key)
{
using (IRedisClient redis = prcm.GetClient())
{
return redis.Remove(key);
}
} #endregion #region -- List -- public static void List_Add<T>(string key, T t)
{
using (IRedisClient redis = prcm.GetClient())
{
var redisTypedClient = redis.GetTypedClient<T>();
redisTypedClient.AddItemToList(redisTypedClient.Lists[key], t);
}
} public static bool List_Remove<T>(string key, T t)
{
using (IRedisClient redis = prcm.GetClient())
{
var redisTypedClient = redis.GetTypedClient<T>();
return redisTypedClient.RemoveItemFromList(redisTypedClient.Lists[key], t) > ;
}
}
public static void List_RemoveAll<T>(string key)
{
using (IRedisClient redis = prcm.GetClient())
{
var redisTypedClient = redis.GetTypedClient<T>();
redisTypedClient.Lists[key].RemoveAll();
}
} public static long List_Count(string key)
{
using (IRedisClient redis = prcm.GetReadOnlyClient())
{
return redis.GetListCount(key);
}
} public static List<T> List_GetRange<T>(string key, int start, int count)
{
using (IRedisClient redis = prcm.GetReadOnlyClient())
{
var c = redis.GetTypedClient<T>();
return c.Lists[key].GetRange(start, start + count - );
}
} public static List<T> List_GetList<T>(string key)
{
using (IRedisClient redis = prcm.GetReadOnlyClient())
{
var c = redis.GetTypedClient<T>();
return c.Lists[key].GetRange(, c.Lists[key].Count);
}
} public static List<T> List_GetList<T>(string key, int pageIndex, int pageSize)
{
int start = pageSize * (pageIndex - );
return List_GetRange<T>(key, start, pageSize);
} /// <summary>
/// 设置缓存过期
/// </summary>
/// <param name="key"></param>
/// <param name="datetime"></param>
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<T>(string key, T t)
{
using (IRedisClient redis = prcm.GetClient())
{
var redisTypedClient = redis.GetTypedClient<T>();
redisTypedClient.Sets[key].Add(t);
}
}
public static bool Set_Contains<T>(string key, T t)
{
using (IRedisClient redis = prcm.GetClient())
{
var redisTypedClient = redis.GetTypedClient<T>();
return redisTypedClient.Sets[key].Contains(t);
}
}
public static bool Set_Remove<T>(string key, T t)
{
using (IRedisClient redis = prcm.GetClient())
{
var redisTypedClient = redis.GetTypedClient<T>();
return redisTypedClient.Sets[key].Remove(t);
}
}
#endregion #region -- Hash --
/// <summary>
/// 判断某个数据是否已经被缓存
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="dataKey"></param>
/// <returns></returns>
public static bool Hash_Exist<T>(string key, string dataKey)
{
using (IRedisClient redis = prcm.GetReadOnlyClient())
{
return redis.HashContainsEntry(key, dataKey);
}
} /// <summary>
/// 存储数据到hash表
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="dataKey"></param>
/// <returns></returns>
public static bool Hash_Set<T>(string key, string dataKey, T t)
{
using (IRedisClient redis = prcm.GetClient())
{
string value = ServiceStack.Text.JsonSerializer.SerializeToString<T>(t);
return redis.SetEntryInHash(key, dataKey, value);
}
}
/// <summary>
/// 移除hash中的某值
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="dataKey"></param>
/// <returns></returns>
public static bool Hash_Remove(string key, string dataKey)
{
using (IRedisClient redis = prcm.GetClient())
{
return redis.RemoveEntryFromHash(key, dataKey);
}
}
/// <summary>
/// 移除整个hash
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="dataKey"></param>
/// <returns></returns>
public static bool Hash_Remove(string key)
{
using (IRedisClient redis = prcm.GetClient())
{
return redis.Remove(key);
}
}
/// <summary>
/// 从hash表获取数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="dataKey"></param>
/// <returns></returns>
public static T Hash_Get<T>(string key, string dataKey)
{
using (IRedisClient redis = prcm.GetReadOnlyClient())
{
string value = redis.GetValueFromHash(key, dataKey);
return ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(value);
}
}
/// <summary>
/// 获取整个hash的数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <returns></returns>
public static List<T> Hash_GetAll<T>(string key)
{
using (IRedisClient redis = prcm.GetReadOnlyClient())
{
var list = redis.GetHashValues(key);
if (list != null && list.Count > )
{
List<T> result = new List<T>();
foreach (var item in list)
{
var value = ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(item);
result.Add(value);
}
return result;
}
return null;
}
}
/// <summary>
/// 设置缓存过期
/// </summary>
/// <param name="key"></param>
/// <param name="datetime"></param>
public static void Hash_SetExpire(string key, DateTime datetime)
{
using (IRedisClient redis = prcm.GetClient())
{
redis.ExpireEntryAt(key, datetime);
}
}
#endregion #region -- SortedSet --
/// <summary>
/// 添加数据到 SortedSet
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="t"></param>
/// <param name="score"></param>
public static bool SortedSet_Add<T>(string key, T t, double score)
{
using (IRedisClient redis = prcm.GetClient())
{
string value = ServiceStack.Text.JsonSerializer.SerializeToString<T>(t);
return redis.AddItemToSortedSet(key, value, score);
}
}
/// <summary>
/// 移除数据从SortedSet
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="t"></param>
/// <returns></returns>
public static bool SortedSet_Remove<T>(string key, T t)
{
using (IRedisClient redis = prcm.GetClient())
{
string value = ServiceStack.Text.JsonSerializer.SerializeToString<T>(t);
return redis.RemoveItemFromSortedSet(key, value);
}
}
/// <summary>
/// 修剪SortedSet
/// </summary>
/// <param name="key"></param>
/// <param name="size">保留的条数</param>
/// <returns></returns>
public static long SortedSet_Trim(string key, int size)
{
using (IRedisClient redis = prcm.GetClient())
{
return redis.RemoveRangeFromSortedSet(key, size, );
}
}
/// <summary>
/// 获取SortedSet的长度
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static long SortedSet_Count(string key)
{
using (IRedisClient redis = prcm.GetReadOnlyClient())
{
return redis.GetSortedSetCount(key);
}
} /// <summary>
/// 获取SortedSet的分页数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
public static List<T> SortedSet_GetList<T>(string key, int pageIndex, int pageSize)
{
using (IRedisClient redis = prcm.GetReadOnlyClient())
{
var list = redis.GetRangeFromSortedSet(key, (pageIndex - ) * pageSize, pageIndex * pageSize - );
if (list != null && list.Count > )
{
List<T> result = new List<T>();
foreach (var item in list)
{
var data = ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(item);
result.Add(data);
}
return result;
}
}
return null;
} /// <summary>
/// 获取SortedSet的全部数据
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="key"></param>
/// <param name="pageIndex"></param>
/// <param name="pageSize"></param>
/// <returns></returns>
public static List<T> SortedSet_GetListALL<T>(string key)
{
using (IRedisClient redis = prcm.GetReadOnlyClient())
{
var list = redis.GetRangeFromSortedSet(key, , );
if (list != null && list.Count > )
{
List<T> result = new List<T>();
foreach (var item in list)
{
var data = ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(item);
result.Add(data);
}
return result;
}
}
return null;
} /// <summary>
/// 设置缓存过期
/// </summary>
/// <param name="key"></param>
/// <param name="datetime"></param>
public static void SortedSet_SetExpire(string key, DateTime datetime)
{
using (IRedisClient redis = prcm.GetClient())
{
redis.ExpireEntryAt(key, datetime);
}
}
#endregion
}

Redis 客户端配置及示例的更多相关文章

  1. Enyim.Caching 客户端配置及示例

    一.工作准备 memcached客户端:Enyim.Caching.2.13 memcached服务器:memcached-win64-1.4.4-14 备注:不建议使用windows服务器,开发环境 ...

  2. spring 5.x 系列第8篇 —— 整合Redis客户端 Jedis和Redisson (代码配置方式)

    文章目录 一.说明 1.1 Redis 客户端说明 1.2 Redis可视化软件 1.3 项目结构说明 1.3 依赖说明 二.spring 整合 jedis 2.1 新建基本配置文件和其映射类 2.2 ...

  3. spring 5.x 系列第7篇 —— 整合Redis客户端 Jedis和Redisson (xml配置方式)

    文章目录 一.说明 1.1 Redis 客户端说明 1.2 Redis可视化软件 1.3 项目结构说明 1.3 依赖说明 二.spring 整合 jedis 2.1 新建基本配置文件 2.2 单机配置 ...

  4. StackExchange.Redis客户端读写主从配置,以及哨兵配置。

    今天简单分享一下StackExchange.Redis客户端中配置主从分离以及哨兵的配置. 关于哨兵如果有不了解的朋友,可以看我之前的一篇分享,当然主从复制文章也可以找到.http://www.cnb ...

  5. Redis客户端之Spring整合Jedis,ShardedJedisPool集群配置

    Jedis设计 Jedis作为推荐的java语言redis客户端,其抽象封装为三部分: 对象池设计:Pool,JedisPool,GenericObjectPool,BasePoolableObjec ...

  6. 005.iSCSI客户端配置示例-Windows

    一 环境 Linux作为iSCSI服务端,Windows2008R2作为iSCSI客户端 二 服务端配置过程 2.1 客户端配置 在Linux上参照之前的配置建立三个LUN卷作为共享盘,最终配置如下: ...

  7. Redis安装配置与Jedis访问数据库

    一.NOSQL概要 NoSQL(NoSQL = Not Only SQL ),意即“不仅仅是SQL”,泛指非关系型的数据库.NoSQL数据库的四大分类 键值(Key-Value)存储数据库 这一类数据 ...

  8. ASP.NET Core 使用 Redis 客户端

    Mac OS 安装 Redis(用于连 Redis 服务器,方便查看数据):https://redis.io/topics/quickstart wget http://download.redis. ...

  9. windows下远程访问Redis,windows Redis绑定ip无效,Redis设置密码无效,Windows Redis 配置不生效,Windows Redis requirepass不生效,windows下远程访问redis的配置

    转载:http://fanshuyao.iteye.com/blog/2384074 一.Redis下载地址: https://github.com/MicrosoftArchive/redis/re ...

随机推荐

  1. centreon 画图x轴乱码

    rrdtool默认不指定locale,使用本地locale.乱码我估记是中文字体,由于操作系统最小化安装,本地没有中文字体,导致乱码. 1 安装中文字体 yum -y install wqy-zenh ...

  2. css框模型

    元素的背景是内容.内边距和边框区的背景. css中:width 和 height 指的是内容区域的宽度和高度.增加内边距.边框和外边距不会影响内容区域的尺寸,但是会增加元素框的总尺寸. bootstr ...

  3. dubbo使用遇到的问题

    转自:http://blog.csdn.net/liwf_/article/details/40297121?utm_source=tuicool&utm_medium=referral 把一 ...

  4. Android IOS WebRTC 音视频开发总结(六四)-- webrtc能走多远我不知道,但这个市场真实存在

    本文主要总结目前都有哪些使用场景用到webrtc,文章最早发表在我们的微信公众号上,详见这里, 欢迎关注微信公众号blackerteam,更多详见www.blackerteam.com webrtc只 ...

  5. import static与import的区别

    import static(静态导入)是JDK1.5中的新特性,一般我们导入一个类都用 import com.....ClassName;而静态导入是这样:import static com..... ...

  6. 华为OJ-合唱队

    华为OJ-初级题-合唱队 思路与分析 本题可以用DP的方法,分别从正向和逆向的两个方向求,该数组即186 186 150 200 160 130 197 200的上升对大序列.正向为[1, 1, 1, ...

  7. Windows Server 2008 R2 实现多用户连接远程桌面

    前提 1. 确认自己的计算机开启了远程连接 2. 在远程桌面会话主机配置中将"限制每个用户只能进行一个会话"的勾去掉. 实现方法 1. 需要在角色里面安装远程桌面服务: 2. 在用 ...

  8. c# TextReader/TextWriter 的类

    TextReader以及TextWriter这两个类,非常有用,很多方法都接受它们作为参数. TextReader有两个子类: StringReader/StringWriter 用于读取字符串: S ...

  9. TextView字符串波浪式跳动--第三方开源---JumpingBeans

    在github上有一个开源项目:JumpingBeans,其项目主页是:https://github.com/frakbot/JumpingBeans JumpingBeans将一个普通的Androi ...

  10. 网页绘制图表 Google Charts with JavaScript #2 ....与ASP.NET网页结合 (ClientScriptManager.RegisterStartupScript 方法)

    此为文章备份,原文出处(我的网站) 网页绘制图表 Google Charts with JavaScript #2 ....与ASP.NET网页结合 (ClientScriptManager.Regi ...