1.前言
a.Redis是一个开源,先进的key-value(键/值对)存储,并且勇于构建高性能,可扩展的Web应用程序的完美解决方案
b.Redis和Memcached的对比
b.1 Redis数据库完全在内存中,使用磁盘仅用于持久性
b.2 相比较许多键值对存储,redis拥有更加丰富的数据类型,
Redis提供的五种数据类型: string(字符串)、hash(哈希表) list(双向队列)、set(集合)和zset(有序集合)
b.3 Redis可以将数据复制到任意数量的从服务器
c.Redis拥有的优势
c.1 Redis的执行响应速度非常快
c.2 支持丰富的数据类型
c.3 原子性,保证了如果两个客户端同事访问的Redis服务器将获得更新后的值
c.4 多功能实用工具,Redis是一个多实用的工具,可以在多个用例如缓存,消息,队列实用,任何短暂的数据,应用程序。
d.Github下载地址:https://github.com/kencery/Common/tree/master/KenceryCommonMethod/%E7%BC%93%E5%AD%98
e.Redis学习:http://www.redis.io/
// 源文件头信息: // <copyright file="RedisHelper.cs"> // Copyright(c)2014-2034 Kencery.All rights reserved. // 个人博客:http://www.cnblogs.com/hanyinglong // 创建人:韩迎龙(kencery) // 创建时间:2015-8-18 // </copyright> using System; using System.Collections.Generic; using System.Configuration; using ServiceStack.Redis; namespace KenceryCommonMethod { /// <summary> /// Redis缓存读取设置 封装 /// <auther> /// <name>Kencery</name> /// <date>2015-8-18</date> /// </auther> /// </summary> public static class RedisHelper { /// <summary> /// 创建Redis连接池管理对象(添加ServiceStack.Interfaces.dll、ServiceStack.Redis.dll) /// </summary> /// <param name="readWriteHosts">只写服务器</param> /// <param name="readOnlyHosts">只读服务器</param> /// <returns></returns> private static PooledRedisClientManager CreateRedisManager(IEnumerable<string> readWriteHosts, IEnumerable<string> readOnlyHosts) { //支持读写分离,均衡负载 return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig { MaxWritePoolSize = 5, //“写”链接池数 MaxReadPoolSize = 5, //“读”链接池数 AutoStart = true, }); } /// <summary> /// 调用CreateRedisManager方法,创建连接池管理对象,Redis服务器地址在配置文件中配置(创建只读,只写连接池) /// <add key="RedisHosts" value="127.0.0.1:6379" /> /// </summary> private static readonly PooledRedisClientManager Prcm = CreateRedisManager( ConfigurationManager.AppSettings["Hosts"].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries), ConfigurationManager.AppSettings["Hosts"].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)); /// <summary> /// 给缓存中添加数据,使用:RedisHelper.Set(key,值(需要存放的值)); /// </summary> /// <typeparam name="T">缓存类型</typeparam> /// <param name="key">键</param> /// <param name="val">值</param> public static void Set<T>(string key, T val) { using (IRedisClient rdc = Prcm.GetClient()) { rdc.Set<T>(key, val); } } /// <summary> /// 读取缓存中的数据,使用:var result=RedisHelper.Get<T>(key); /// </summary> /// <typeparam name="T">返回读取的数据</typeparam> /// <param name="key">键</param> /// <returns></returns> public static T Get<T>(string key) where T : class { using (IRedisClient rdc = Prcm.GetReadOnlyClient()) { return rdc.Get<T>(key); } } /// <summary> /// 删除缓存中的数据,使用 RedisHelper.Remove(key); /// </summary> /// <param name="key">键</param> public static void Remove(string key) { using (IRedisClient rdc = Prcm.GetClient()) { rdc.Remove(key); } } /// <summary> /// 缓存中是否包含查询的键数据,使用 var isTrue=RedisHelper.ContainsKey(key); /// </summary> /// <param name="key">键</param> /// <returns>如果包含,返回true,否则返回false</returns> public static bool ContainsKey(string key) { using (IRedisClient rdc = Prcm.GetReadOnlyClient()) { return rdc.ContainsKey(key); } } /// <summary> /// 给缓存中添加Object对象,使用:RedisHelper.Add(key,值(需要存放的值)) /// </summary> /// <param name="key">键</param> /// <param name="value">值</param> public static void Add(string key, object value) { using (IRedisClient rdc = Prcm.GetClient()) { if (!rdc.ContainsKey(key)) { rdc.Add(key, value, DateTime.Now.AddMinutes(30)); } else { rdc.Set(key, value); } } } /// <summary> /// 根据key刷新缓存中的数据信息,使用:RedisHelper.RefreshCache(key) /// </summary> /// <typeparam name="T">缓存类型</typeparam> /// <param name="key">键</param> public static void RefreshCache<T>(string key) where T : class { using (IRedisClient rdc = Prcm.GetClient()) { var value = rdc.Get<T>(key); rdc.Remove(key); rdc.Set<T>(key, value); } } /// <summary> /// 根据key集合信息读取缓存中的键值对,返回字典形式的数据存放,使用:RedisHelper.GetList(keys); /// </summary> /// <param name="keys">key集合</param> /// <returns>返回字典集合</returns> public static Dictionary<string, string> GetList(List<string> keys) { using (IRedisClient rdc = Prcm.GetReadOnlyClient()) { return rdc.GetValuesMap<string>(keys); } } /// <summary> /// 将字典集合添加到缓存中,使用:RedisHelper.Set(values); /// </summary> /// <param name="values">字典集合信息</param> public static void Set(Dictionary<string, string> values) { using (IRedisClient rdc = Prcm.GetReadOnlyClient()) { rdc.SetAll(values); } } } }