c# Redis 使用
1、服务端两个版本
窗口版与安装windows服务版
1.1、窗口版
下载地址:https://github.com/dmajkic/redis/downloads
redis-server.exe:服务程序
redis-check-dump.exe:本地数据库检查
redis-check-aof.exe:更新日志检查
redis-benchmark.exe:性能测试,用以模拟同时由N个客户端发送M个 SETs/GETs 查询.
redis-cli.exe: 服务端开启后,我们的客户端就可以输入各种命令测试了
首先以管理员身份打开cmd (窗口+R),进入到安装包下载的位置。输入:redis-server.exe redis.conf 开启Redis服务。提示信息没有报错表示启动成功。
客户端测试,双击打开redis-cli.exe 输入“set aaa 1234”,回车,提示ok,表示缓存写入成功, 再输入"get aaa",回车,即可取到缓存的值
1.2、安装windows服务版
下载地址:https://github.com/rgl/redis/downloads
下载完成后直接点击.exe下一步下一步OK。安装完后我们会在windows服务中找到Redis Service服务。即可跟窗口版效果一样测试客户端。
(注:如果启动失败肯定是端口被占用,如窗口版的测试窗口没关闭掉 redis 端口是 6379)
2、使用(服务端必须运行中)
新建控制台应用程序,下载dll,RedisClient 引用
粘贴以下代码即可马上使用,取消注释部分一块块测试
static void Main(string[] args)
{
//在Redis中存储常用的5种数据类型:String,Hash,List,SetSorted set RedisClient client = new RedisClient("localhost", 6379);
client.FlushAll(); #region string
//client.Add<string>("StringValueTime", "我已设置过期时间噢30秒后会消失", DateTime.Now.AddMilliseconds(10000));
//while (true)
//{
// if (client.ContainsKey("StringValueTime"))
// {
// Console.WriteLine("String.键:StringValue,值:{0} {1}", client.Get<string>("StringValueTime"), DateTime.Now);
// Thread.Sleep(1000);
// }
// else
// {
// Console.WriteLine("键:StringValue,值:我已过期 {0}", DateTime.Now);
// Thread.Sleep(1000);
// break;
// }
//} //client.Add<string>("StringValue", " String和Memcached操作方法差不多");
//Console.WriteLine("数据类型为:String.键:StringValue,值:{0}", client.Get<string>("StringValue")); //Student stud = new Student() { id = "1001", name = "李四" };
//client.Add<Student>("StringEntity", stud);
//Student Get_stud = client.Get<Student>("StringEntity");
//Console.WriteLine("数据类型为:String.键:StringEntity,值:{0} {1}", Get_stud.id, Get_stud.name);
#endregion #region Hash
//client.SetEntryInHash("HashID", "Name", "张三");
//client.SetEntryInHash("HashID", "Age", "24");
//client.SetEntryInHash("HashID", "Sex", "男");
//client.SetEntryInHash("HashID", "Address", "上海市XX号XX室"); //List<string> HaskKey = client.GetHashKeys("HashID");
//foreach (string key in HaskKey)
//{
// Console.WriteLine("HashID--Key:{0}", key);
//} //List<string> HaskValue = client.GetHashValues("HashID");
//foreach (string value in HaskValue)
//{
// Console.WriteLine("HashID--Value:{0}", value);
//} //List<string> AllKey = client.GetAllKeys(); //获取所有的key。
//foreach (string Key in AllKey)
//{
// Console.WriteLine("AllKey--Key:{0}", Key);
//}
#endregion #region List
/*
* list是一个链表结构,主要功能是push,pop,获取一个范围的所有的值等,操作中key理解为链表名字。
* Redis的list类型其实就是一个每个子元素都是string类型的双向链表。我们可以通过push,pop操作从链表的头部或者尾部添加删除元素,
* 这样list既可以作为栈,又可以作为队列。Redis list的实现为一个双向链表,即可以支持反向查找和遍历,更方便操作,不过带来了部分额外的内存开销,
* Redis内部的很多实现,包括发送缓冲队列等也都是用的这个数据结构
*/
//client.EnqueueItemOnList("QueueListId", "1.张三"); //入队
//client.EnqueueItemOnList("QueueListId", "2.张四");
//client.EnqueueItemOnList("QueueListId", "3.王五");
//client.EnqueueItemOnList("QueueListId", "4.王麻子");
//int q = client.GetListCount("QueueListId");
//for (int i = 0; i < q; i++)
//{
// Console.WriteLine("QueueListId出队值:{0}", client.DequeueItemFromList("QueueListId")); //出队(队列先进先出)
//} //client.PushItemToList("StackListId", "1.张三"); //入栈
//client.PushItemToList("StackListId", "2.张四");
//client.PushItemToList("StackListId", "3.王五");
//client.PushItemToList("StackListId", "4.王麻子");
//int p = client.GetListCount("StackListId");
//for (int i = 0; i < p; i++)
//{
// Console.WriteLine("StackListId出栈值:{0}", client.PopItemFromList("StackListId")); //出栈(栈先进后出)
//} #endregion #region Set无序集合
/*
它是string类型的无序集合。set是通过hash table实现的,添加,删除和查找,对集合我们可以取并集,交集,差集
*/
//client.AddItemToSet("Set1001", "小A");
//client.AddItemToSet("Set1001", "小B");
//client.AddItemToSet("Set1001", "小C");
//client.AddItemToSet("Set1001", "小D");
//HashSet<string> hastsetA = client.GetAllItemsFromSet("Set1001");
//foreach (string item in hastsetA)
//{
// Console.WriteLine("Set无序集合ValueA:{0}", item); //出来的结果是无须的
//} //client.AddItemToSet("Set1002", "小K");
//client.AddItemToSet("Set1002", "小C");
//client.AddItemToSet("Set1002", "小A");
//client.AddItemToSet("Set1002", "小J");
//HashSet<string> hastsetB = client.GetAllItemsFromSet("Set1002");
//foreach (string item in hastsetB)
//{
// Console.WriteLine("Set无序集合ValueB:{0}", item); //出来的结果是无须的
//} //HashSet<string> hashUnion = client.GetUnionFromSets(new string[] { "Set1001", "Set1002" });
//foreach (string item in hashUnion)
//{
// Console.WriteLine("求Set1001和Set1002的并集:{0}", item); //并集
//} //HashSet<string> hashG = client.GetIntersectFromSets(new string[] { "Set1001", "Set1002" });
//foreach (string item in hashG)
//{
// Console.WriteLine("求Set1001和Set1002的交集:{0}", item); //交集
//} //HashSet<string> hashD = client.GetDifferencesFromSet("Set1001", new string[] { "Set1002" }); //[返回存在于第一个集合,但是不存在于其他集合的数据。差集]
//foreach (string item in hashD)
//{
// Console.WriteLine("求Set1001和Set1002的差集:{0}", item); //差集
//} #endregion #region SetSorted 有序集合
/*
sorted set 是set的一个升级版本,它在set的基础上增加了一个顺序的属性,这一属性在添加修改.元素的时候可以指定,
* 每次指定后,zset(表示有序集合)会自动重新按新的值调整顺序。可以理解为有列的表,一列存 value,一列存顺序。操作中key理解为zset的名字.
*/
client.AddItemToSortedSet("SetSorted1001", "1.刘仔");
client.AddItemToSortedSet("SetSorted1001", "2.星仔");
client.AddItemToSortedSet("SetSorted1001", "3.猪仔");
List<string> listSetSorted = client.GetAllItemsFromSortedSet("SetSorted1001");
foreach (string item in listSetSorted)
{
Console.WriteLine("SetSorted有序集合{0}", item);
}
#endregion
}
c# Redis 使用的更多相关文章
- 使用redis构建可靠分布式锁
关于分布式锁的概念,具体实现方式,直接参阅下面两个帖子,这里就不多介绍了. 分布式锁的多种实现方式 分布式锁总结 对于分布式锁的几种实现方式的优劣,这里再列举下 1. 数据库实现方式 优点:易理解 缺 ...
- Ignite性能测试以及对redis的对比
测试方法 为了对Ignite做一个基本了解,做了一个性能测试,测试方法也比较简单主要是针对client模式,因为这种方法和使用redis的方式特别像.测试方法很简单主要是下面几点: 不作参数优化,默认 ...
- mac osx 安装redis扩展
1 php -v查看php版本 2 brew search php|grep redis 搜索对应的redis ps:如果没有brew 就根据http://brew.sh安装 3 brew ins ...
- Redis/HBase/Tair比较
KV系统对比表 对比维度 Redis Redis Cluster Medis Hbase Tair 访问模式 支持Value大小 理论上不超过1GB(建议不超过1MB) 理论上可配置(默认配置1 ...
- Redis数据库
Redis是k-v型数据库的典范,设计思想及数据结构实现都值得学习. 1.数据类型 value支持五种数据类型:1.字符串(strings)2.字符串列表(lists)3.字符串集合(sets)4.有 ...
- redis 学习笔记(2)
redis-cluster 简介 redis-cluster是一个分布式.容错的redis实现,redis-cluster通过将各个单独的redis实例通过特定的协议连接到一起实现了分布式.集群化的目 ...
- redis 学习笔记(1)
redis持久化 snapshot数据快照(rdb) 这是一种定时将redis内存中的数据写入磁盘文件的一种方案,这样保留这一时刻redis中的数据镜像,用于意外回滚.redis的snapshot的格 ...
- python+uwsgi导致redis无法长链接引起性能下降问题记录
今天在部署python代码到预生产环境时,web站老是出现redis链接未初始化,无法连接到服务的提示,比对了一下开发环境与测试环境代码,完全一致,然后就是查看各种日志,排查了半天也没有查明是什么原因 ...
- nginx+iis+redis+Task.MainForm构建分布式架构 之 (redis存储分布式共享的session及共享session运作流程)
本次要分享的是利用windows+nginx+iis+redis+Task.MainForm组建分布式架构,上一篇分享文章制作是在windows上使用的nginx,一般正式发布的时候是在linux来配 ...
- windows+nginx+iis+redis+Task.MainForm构建分布式架构 之 (nginx+iis构建服务集群)
本次要分享的是利用windows+nginx+iis+redis+Task.MainForm组建分布式架构,由标题就能看出此内容不是一篇分享文章能说完的,所以我打算分几篇分享文章来讲解,一步一步实现分 ...
随机推荐
- Python学习第十六篇——异常处理
在实际中,很多时候时候,我们并不能保证我们所写的程序是完美的.比如我们程序的本意是:用户在输入框内输入数字,并进行后续数学运算,即使我们提醒了用户需要输入数字而不是文本,但是有时会无意或者恶意输入字符 ...
- MySQL数据类型--日期和时间类型
MySQL中的多种时间和格式数据类型 日期和时间类型是为了方便在数据库中存储日期和时间而设计的.MySQL中有多种表示日期和时间的数据类型. 其中,year类型表示时间,date类型表示日期,time ...
- MySQL 使用左连接替换not in
众所周知,左连接和右连接的含义是以哪一张表为准. 左连接就是以左表为准,查出的结果中包含左表所有的记录,如果右表中没有与其对应的记录,那么那一行记录中B表部分的内容就全是NULL. 现在有两个表,一个 ...
- 分布式事务 spring 两阶段提交 tcc
请问分布式事务一致性与raft或paxos协议解决的一致性问题是同一回事吗? - 知乎 https://www.zhihu.com/question/275845393 分布式事务11_TCC 两阶段 ...
- JMeter Exception: java.net.BindException: Address already in use: connect(转)
转自:http://twit88.com/blog/2008/07/28/jmeter-exception-javanetbindexception-address-already-in-use-co ...
- object-fit 属性的用法介绍
这个要在宽,高都是100%的情况下才能提现 object-fit 属性的用法介绍 fill(不保持纵横比缩放图片,使图片完全适应) contain(保持纵横比缩放图片,使图片的长边能完全显示出来) c ...
- 牛客练习赛13F m皇后
题目链接:https://ac.nowcoder.com/acm/contest/70/F 题目大意: 略 分析: 可以分成四步计算冲突:水平方向,垂直方向,左斜线方向,右斜线方向.只要会处理水平方向 ...
- 【转】说说MySQL中的Redo log Undo log都在干啥
阅读目录(Content) 1 undo 1.1 undo是啥 1.2 undo参数 1.3 undo空间管理 2 redo 2.1 redo是啥 2.2 redo 参数 2.3 redo 空间管理 ...
- linux查看端口是否开放
在讨论这个问题前,我们先来了解一下物理端口.逻辑端口.端口号等计算机概念. 端口相关的概念: 在网络技术中,端口(Port)包括逻辑端口和物理端口两种类型.物理端口指的是物理存在的端口,如ADSL M ...
- linux服务器运维
1. grep正则匹配 grep -E "([0-9]{1,3}\.){4}" filepath egrep "([0-9]{1,3}\.){4}" fil ...