博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
高性能文件缓存key-value存储—Redis
阅读量:6971 次
发布时间:2019-06-27

本文共 4871 字,大约阅读时间需要 16 分钟。

  

  

  备注:三篇博文结合阅读,简单理解并且使用,如果想深入学习,请多参考文章中给出的博文地址。

1.前言

  a.Redis是一个开源,先进的key-value(键/值对)存储,并且勇于构建高性能,可扩展的Web应用程序的完美解决方案

  b.Redis和Memcached的对比

    b.1 Redis数据库完全在内存中,使用磁盘仅用于持久性

    b.2 相比较许多键值对存储,redis拥有更加丰富的数据类型,Redis提供的五种数据类型: strings、map、 list、sets、 sorted sets

    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/

2.代码展示

1 // 源文件头信息:  2 // 
3 // Copyright(c)2014-2034 Kencery.All rights reserved. 4 // 个人博客:http://www.cnblogs.com/hanyinglong 5 // 创建人:韩迎龙(kencery) 6 // 创建时间:2015-8-18 7 //
8 9 using System; 10 using System.Collections.Generic; 11 using System.Configuration; 12 using ServiceStack.Redis; 13 14 namespace KenceryCommonMethod 15 { 16 /// 17 /// Redis缓存读取设置 封装 18 ///
19 ///
Kencery
20 ///
2015-8-18
21 ///
22 ///
23 public static class RedisHelper 24 { 25 /// 26 /// 创建Redis连接池管理对象(添加ServiceStack.Interfaces.dll、ServiceStack.Redis.dll) 27 /// 28 /// 只写服务器 29 /// 只读服务器 30 ///
31 private static PooledRedisClientManager CreateRedisManager(IEnumerable
readWriteHosts, 32 IEnumerable
readOnlyHosts) 33 { 34 //支持读写分离,均衡负载 35 return new PooledRedisClientManager(readWriteHosts, readOnlyHosts, new RedisClientManagerConfig 36 { 37 MaxWritePoolSize = 5, //“写”链接池数 38 MaxReadPoolSize = 5, //“读”链接池数 39 AutoStart = true, 40 }); 41 } 42 43 ///
44 /// 调用CreateRedisManager方法,创建连接池管理对象,Redis服务器地址在配置文件中配置(创建只读,只写连接池) 45 ///
46 ///
47 private static readonly PooledRedisClientManager Prcm = CreateRedisManager( 48 ConfigurationManager.AppSettings["Hosts"].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries), 49 ConfigurationManager.AppSettings["Hosts"].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)); 50 51 ///
52 /// 给缓存中添加数据,使用:RedisHelper.Set(key,值(需要存放的值)); 53 /// 54 ///
缓存类型
55 ///
键 56 ///
值 57 public static void Set
(string key, T val) 58 { 59 using (IRedisClient rdc = Prcm.GetClient()) 60 { 61 rdc.Set
(key, val); 62 } 63 } 64 65 ///
66 /// 读取缓存中的数据,使用:var result=RedisHelper.Get
(key); 67 ///
68 ///
返回读取的数据
69 ///
键 70 ///
71 public static T Get
(string key) where T : class 72 { 73 using (IRedisClient rdc = Prcm.GetReadOnlyClient()) 74 { 75 return rdc.Get
(key); 76 } 77 } 78 79 ///
80 /// 删除缓存中的数据,使用 RedisHelper.Remove(key); 81 /// 82 ///
键 83 public static void Remove(string key) 84 { 85 using (IRedisClient rdc = Prcm.GetClient()) 86 { 87 rdc.Remove(key); 88 } 89 } 90 91 ///
92 /// 缓存中是否包含查询的键数据,使用 var isTrue=RedisHelper.ContainsKey(key); 93 /// 94 ///
键 95 ///
如果包含,返回true,否则返回false
96 public static bool ContainsKey(string key) 97 { 98 using (IRedisClient rdc = Prcm.GetReadOnlyClient()) 99 {100 return rdc.ContainsKey(key);101 }102 }103 104 ///
105 /// 给缓存中添加Object对象,使用:RedisHelper.Add(key,值(需要存放的值))106 /// 107 ///
键108 ///
值109 public static void Add(string key, object value)110 {111 using (IRedisClient rdc = Prcm.GetClient())112 {113 if (!rdc.ContainsKey(key))114 {115 rdc.Add(key, value, DateTime.Now.AddMinutes(30));116 }117 else118 {119 rdc.Set(key, value);120 }121 }122 }123 124 ///
125 /// 根据key刷新缓存中的数据信息,使用:RedisHelper.RefreshCache(key)126 /// 127 ///
缓存类型
128 ///
键129 public static void RefreshCache
(string key) where T : class130 {131 using (IRedisClient rdc = Prcm.GetClient())132 {133 var value = rdc.Get
(key);134 rdc.Remove(key);135 rdc.Set
(key, value);136 }137 }138 139 ///
140 /// 根据key集合信息读取缓存中的键值对,返回字典形式的数据存放,使用:RedisHelper.GetList(keys);141 /// 142 ///
key集合143 ///
返回字典集合
144 public static Dictionary
GetList(List
keys)145 {146 using (IRedisClient rdc = Prcm.GetReadOnlyClient())147 {148 return rdc.GetValuesMap
(keys);149 }150 }151 152 ///
153 /// 将字典集合添加到缓存中,使用:RedisHelper.Set(values);154 /// 155 ///
字典集合信息156 public static void Set(Dictionary
values)157 {158 using (IRedisClient rdc = Prcm.GetReadOnlyClient())159 {160 rdc.SetAll(values);161 }162 }163 164 }165 }
你可能感兴趣的文章
vi vim配置
查看>>
PP日志-Day 3
查看>>
eclipse 调试 jdk 看不到变量的值
查看>>
如何解决分配到Autoconfiguration IPV4 地址
查看>>
.NET 远程操作MSMSQ无权限或操作出错问题解决
查看>>
浅谈企业信息泄密:信任不存,效益焉在?
查看>>
Mysql密码管理及授权
查看>>
JAVA线程安全之synchronized关键字的正确用法
查看>>
springmvc+mybatis+dubbo分布式平台-maven构建根项目
查看>>
一个小常识
查看>>
Nginx防盗链 Nginx访问控制 Nginx解析php相关配置 Nginx代理
查看>>
解决虚拟机中使用ntpdate报错:ntpdate[46700]: no server suitab
查看>>
基于虚拟主机的FTP配置
查看>>
分享一个iptables防火墙的脚本和防御ddos***的脚本
查看>>
域控之间角色转换(BDC转换为PDC)
查看>>
如何杀掉(kill)Oracle中的会话(Session)
查看>>
ESP定律的原理
查看>>
opcode的执行
查看>>
管理大量定时任务,如果高效触发超时?
查看>>
input file图片上传预览
查看>>