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实 ...
随机推荐
- 从B树、B+树、B*树谈到R 树
从B 树.B+ 树.B* 树谈到R 树 作者:July.weedge.Frankie.编程艺术室出品. 说明:本文从B树开始谈起,然后论述B+树.B*树,最后谈到R 树.其中B树.B+树及B*树部分由 ...
- Cloudera集群中提交Spark任务出现java.lang.NoSuchMethodError: org.apache.hadoop.hbase.HTableDescriptor.addFamily错误解决
Cloudera及相关的组件版本 Cloudera: 5.7.0 Hbase: 1.20 Hadoop: 2.6.0 ZooKeeper: 3.4.5 就算是引用了相应的组件依赖,依然是报一样的错误! ...
- 称球问题(zt)
下面说的这个问题可能大家都看到过,它是这么描述的: 现在有n(n>=2)个球,n个球外观一模一样,但是重量有区别,其中有且仅有一个球的重量比其它n-1个球要重,现在有一个天平,天平是完好无损的, ...
- c++暂停
#include<cstdlib> 加入这个 然后 system("pause");
- Android进阶笔记16:ListView篇之ListView刷新显示(全局 和 局部)
一.ListView内容变化后,动态刷新的步骤(全局刷新): (1)更新适配器Adapter数据源:(不要使用匿名内部类) (2)调用适配器Adapter的刷新方法notifyDataSetChang ...
- request 和response 中的setCharacterEncoding区别
response和request的setCharacterEncoding 一.request.setCharacterEncoding():是设置从request中取得的值或从数据库中取出的值. 指 ...
- javaweb学习总结二十一(servlet开发入门、servlet生命周期以及调用过程)
一:servlet开发入门 servlet是sun公司一门开发动态web资源的技术,下面编写一个servlet入门程序: 1:在tomcat服务器webapps目录下新建firstServlet目录, ...
- P6Spy 、 SQL Profiler
P6Spy 在优化Hibernate性能的时候,很重要的一点就是要看到Hibernate底层执行的SQL 虽然通过打印日志配合Hibernate的show_sql属性能够拼凑出Hibernate底层执 ...
- [书目20131114]微软技术丛书•快速软件开发(珍藏版) Rapid Development Taming Wild Software Schedules By Steve McConnell
本文摘自:http://shop.oreilly.com/product/9781556159008.do EFFICIENT DEVELOPMENT Chapter 1 Welcome to Rap ...
- Hive中自定义函数
Hive的自定义的函数的步骤: 1°.自定义UDF extends org.apache.hadoop.hive.ql.exec.UDF 2°.需要实现evaluate函数,evaluate函数支持重 ...