C# Redis实战
转自 :http://blog.csdn.net/qiujialongjjj/article/details/16945569
一、初步准备
二、Redis服务
三、程序配置
- <?xml version="1.0" encoding="utf-8"?>
- <!--
- 有关如何配置 ASP.NET 应用程序的详细信息,请访问
- http://go.microsoft.com/fwlink/?LinkId=169433
- -->
- <configuration>
- <configSections>
- <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
- <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
- <section name="RedisConfig" type="RedisDemo.RedisConfigInfo, RedisDemo"/>
- </configSections>
- <RedisConfig WriteServerList="127.0.0.1:6379" ReadServerList="127.0.0.1:6379" MaxWritePoolSize="60"
- MaxReadPoolSize="60" AutoStart="true" LocalCacheTime="180" RecordeLog="false">
- </RedisConfig>
- <connectionStrings>
- <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-RedisDemo-20131125110945;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-RedisDemo-20131125110945.mdf" />
- </connectionStrings>
- </configuration>
- public static RedisConfigInfo GetConfig()
- {
- RedisConfigInfo section = (RedisConfigInfo)ConfigurationManager.GetSection("RedisConfig");
- return section;
- }
- public static RedisConfigInfo GetConfig(string sectionName)
- {
- RedisConfigInfo section = (RedisConfigInfo)ConfigurationManager.GetSection("RedisConfig");
- if (section == null)
- throw new ConfigurationErrorsException("Section " + sectionName + " is not found.");
- return section;
- }
- /// <summary>
- /// redis配置文件信息
- /// </summary>
- private static RedisConfigInfo redisConfigInfo = RedisConfigInfo.GetConfig();
- private static PooledRedisClientManager prcm;
- /// <summary>
- /// 静态构造方法,初始化链接池管理对象
- /// </summary>
- static RedisManager()
- {
- CreateManager();
- }
- /// <summary>
- /// 创建链接池管理对象
- /// </summary>
- private static void CreateManager()
- {
- string[] writeServerList = SplitString(redisConfigInfo.WriteServerList, ",");
- string[] readServerList = SplitString(redisConfigInfo.ReadServerList, ",");
- prcm = new PooledRedisClientManager(readServerList, writeServerList,
- new RedisClientManagerConfig
- {
- MaxWritePoolSize = redisConfigInfo.MaxWritePoolSize,
- MaxReadPoolSize = redisConfigInfo.MaxReadPoolSize,
- AutoStart = redisConfigInfo.AutoStart,
- });
- }
- private static string[] SplitString(string strSource, string split)
- {
- return strSource.Split(split.ToArray());
- }
- /// <summary>
- /// 客户端缓存操作对象
- /// </summary>
- public static IRedisClient GetClient()
- {
- if (prcm == null)
- CreateManager();
- return prcm.GetClient();
- }
四、写入数据
在C# Redis实战(三)中我们已经配置好了web.config程序,并且能通过C#代码来读取和管理以上配置信息。接下来,就可以进行Redis的数据写入了。Redis中可以用Store和StoreAll分别保存单条和多条数据,C#中具体代码如下:1、保存多条数据- protected void btnOpenDB_Click(object sender, EventArgs e)
- {
- //System.Diagnostics.Process.Start("D:\\redis\\redis-server.exe");
- //lblShow.Text = "Redis已经打开!";
- using (var redisClient = RedisManager.GetClient())
- {
- var user = redisClient.GetTypedClient<User>();
- if (user.GetAll().Count > 0)
- user.DeleteAll();
- var qiujialong = new User
- {
- Id = user.GetNextSequence(),
- Name = "qiujialong",
- Job = new Job { Position = ".NET" }
- };
- var chenxingxing = new User
- {
- Id = user.GetNextSequence(),
- Name = "chenxingxing",
- Job = new Job { Position = ".NET" }
- };
- var luwei = new User
- {
- Id = user.GetNextSequence(),
- Name = "luwei",
- Job = new Job { Position = ".NET" }
- };
- var zhourui = new User
- {
- Id = user.GetNextSequence(),
- Name = "zhourui",
- Job = new Job { Position = "Java" }
- };
- var userToStore = new List<User> { qiujialong, chenxingxing, luwei, zhourui };
- user.StoreAll(userToStore);
- lblShow.Text = "目前共有:" + user.GetAll().Count.ToString() + "人!";
- }
- }
2、保存单条数据- protected void btnInsert_Click(object sender, EventArgs e)
- {
- if (!string.IsNullOrEmpty(txtName.Text) && !string.IsNullOrEmpty(txtPosition.Text))
- {
- using (var redisClient = RedisManager.GetClient())
- {
- var user = redisClient.GetTypedClient<User>();
- var newUser = new User
- {
- Id = user.GetNextSequence(),
- Name = txtName.Text,
- Job = new Job { Position = txtPosition.Text }
- };
- user.Store(newUser);
- if (user.GetAll().Count > 0)
- {
- var htmlStr = string.Empty;
- foreach (var u in user.GetAll())
- {
- htmlStr += "<li>ID=" + u.Id + " 姓名:" + u.Name + " 所在部门:" + u.Job.Position + "</li>";
- }
- lblPeople.Text = htmlStr;
- }
- lblShow.Text = "目前共有:" + user.GetAll().Count.ToString() + "人!";
- }
- }
- }
效果图:
C# Redis实战的更多相关文章
- Redis实战阅读笔记——开始
Redis实战这本书,看完以后最大的不是redis本身的东西,而是作者面对实际问题的分析而给出的设计方案,可以看成NoSql设计的应用.个人从这方面收获很多,至于Redis本身的东西,这个就花一两个小 ...
- Redis实战阅读笔记——第一章
Redis 实战 中文版 的20-21页看的人郁闷死了,最后看英文版才明白意思,哎,我理解能力差成这样了 其中,图 1-12 有错误,草,这个是英文版的错--应该是group:programming
- redis实战(01)_redis安装
早就想对redis进行实战操作了,最近看了一些视频和参考书籍,总结总结一下,redis实战内容: 实战前先对redis做一个大概的认识: 现在开始安装redis了... redis的安装下载地址 ht ...
- C# Redis实战(二) [转]
二.Redis服务 在C# Redis实战(一)中我将所有文件拷贝到了D盘redis文件夹下,其中redis-server.exe即为其服务端程序,双击即开始运行,如图 ...
- (转)国内外三个不同领域巨头分享的Redis实战经验及使用场景
随着应用对高性能需求的增加,NoSQL逐渐在各大名企的系统架构中生根发芽.这里我们将为大家分享社交巨头新浪微博.传媒巨头Viacom及图片分享领域佼佼者Pinterest带来的Redis实践,首先我们 ...
- Redis实战
大约一年多前,公司同事开始使用Redis,不清楚是配置,还是版本的问题,当时的Redis经常在使用一段时间后,连接爆满且不释放.印象中,Redis 2.4.8以下的版本由于设计上的主从库同步问题,就会 ...
- Redis实战之Redis + Jedis
用Memcached,对于缓存对象大小有要求,单个对象不得大于1MB,且不支持复杂的数据类型,譬如SET 等.基于这些限制,有必要考虑Redis! 相关链接: Redis实战 Redis实战之Redi ...
- Redis实战之征服 Redis + Jedis + Spring (一)
Redis + Jedis + Spring (一)—— 配置&常规操作(GET SET DEL)接着需要快速的调研下基于Spring框架下的Redis操作. 相关链接: Redis实战 Re ...
- Redis实战之征服 Redis + Jedis + Spring (二)
不得不说,用哈希操作来存对象,有点自讨苦吃! 不过,既然吃了苦,也做个记录,也许以后API升级后,能好用些呢?! 或许,是我的理解不对,没有真正的理解哈希表. 相关链接: Redis实战 Redis实 ...
随机推荐
- Sql语句中的truncate,delete,drop的区别
相同点: 1.truncate和不带where子句的delete.以及drop都会删除表内的数据. 不同点: 1. truncate 和 delete 只删除数据不删除表的结构(定义) drop 语句 ...
- python(3)-计数器,有序字典
计数器:Counter 在使用计数器之前需要先 import collections >>> import collections >>> obj = collec ...
- [改善Java代码]使用valueOf前必须进行校验
每个枚举都是java.lang.Enum的子类,都可以访问Enum类提供的方法,比如hashCode(),name(),valueOf()等..... 其中valueOf()方法会把一个String类 ...
- Wince 文本函数和字体应用
好像又进入了一个疲惫期了,晚上状态不好,但是还是想继续更新下博客,继上次分析了wince下设备环境以及怎么绘制相关图像后,,笔者在这片文章中讲到文本函数以及其相关应用.文本输出函数是输出文本的内容,也 ...
- Web系统大规模并发----电商秒杀与抢购
原文链接请参见:http://uule.iteye.com/blog/2186786
- mac端口占用查找进程并杀死
查找端口port被占用的进程 lsof -i tcp:port 杀死进程pid kill -9 pid
- 查锁表及kill
当一个表一直被锁住而无法进行操作的时候,可以用如下方法 select l.session_id sid, s.serial#, l.locked_mode 锁模式, l.oracle_username ...
- poj1472[模拟题]
Instant Complexity Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 2017 Accepted: 698 ...
- StringBuilder 类
表示可变字符字符串.无法继承此类. 此类表示值为可变字符序列的类似字符串的对象.之所以说值是可变的,是因为在通过追加.移除.替换或插入字符而创建它后可以对它进行修改.有关比较,请参见 String 类 ...
- PS基础学习 2---图层蒙版
1,蒙版,字面意思上的理解就是:把底层图片上面加上一层图层蒙着,通过画笔工具控制底层图片和上面一层图层的显示效果.常用于图层的无缝隙合成. 我们可以先看一下下面的这个小例子,这个就是蒙版的一个小应用: ...