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实 ...
随机推荐
- C++指针和引用
★ 相同点: 1. 都是地址的概念: 指针指向一块内存,它的内容是所指内存的地址:引用是某块内存的别名. ★ 区别: 1. 指针是一个实体,而引用仅是个别名: 2. 引用使用时无需解引用(*),指 ...
- Step-by-Step XML Free Spring MVC 3 Configuration--reference
The release of Spring 2.5 reduce the burden of XML by introduction annotation based configuration, b ...
- HBase-java api 基本操作
使用的是完全分布式,详细信息为: 操作的Java 代码(抄的别人的) package org.admln.hbase; import java.util.ArrayList; import java. ...
- BootStrap2学习日记3--响应式布局实用类
BootStrap2中常用的响应式布局类如: visible-phone 仅在 手机平台显示 visible-tablet 仅在 平板电脑显示 visible-desktop 仅 ...
- cocos2d-x使用ant打包
1. 下载apache-ant-1.9.3,然后添加环境变量ANT_HOME = D:\dev_envir\apache-ant-1.9.3(你自己的ant根目录),再在path中添加路径:%ANT_ ...
- Android(java)学习笔记198:Android下的逐帧动画(Drawable Animation)
1.帧动画: 帧动画顾名思义,一帧一帧播放的动画就是帧动画. 帧动画和我们小时候看的动画片的原理是一样的,在相同区域快速切换图片给人们呈现一种视觉的假象感觉像是在播放动画,其实不过是N张图片在一帧一帧 ...
- css笔记14:css文件之间可以相互引用
css文件之间相互引用是通过@import指令完成的 格式: @import url("被引用的css文件"); 顺便说一句,如果希望在html或者php文件中引用某个xxx.c ...
- MYSQL基础笔记(七)- 数据类型二
字符串类型 在SQL中,讲字符串类型分成了六类:char,varchar,text,blob,enum,set char,定长字符串 磁盘(二维表)在定义结构的时候,就已经确定了最终数据的存储长度. ...
- a code snip
import java.util.ArrayList; import java.util.HashMap; import java.util.regex.Matcher; import java.ut ...
- 【Android Studio 小技巧】一键查看文件方法结构目录File Structure
看源代码的时候,如果可以查看class中的所有方法,可以提高效率.Android Studio 中可以使用快捷键一键显示所有方法的目录. Mac: command + fn + F12 (在mac中的 ...