Redis演示及使用场景
来源:互联网 发布:天天飞车礼包 软件 编辑:程序博客网 时间:2024/06/06 03:57
概述
Redis是一个开源的、使用C语言编写的、支持网络交互的、可基于内存也可持久化的Key-Value(字典, Remote Dictionary Server,远程字典服务器)数据库。
客户端:http://redis.io/clients
命令:http://redis.io/commands http://redisdoc.com
.NET开发程序配置
- ServiceStack.Common.dll
- ServiceStack.Interfaces.dll
- ServiceStack.Redis.dll
- ServiceStack.Text.dll
程序配置Redis服务IP和端口
static RedisClient Redis = new RedisClient("192.168.100.118", 6379);
双击运行:redis-server.exe
Redis Desktop Manager 介绍
Redis Desktop Manager(RedisDesktopManager,RDM)是一个快速、简单、支持跨平台的 Redis 桌面管理工具,基于 Qt 5开发(一个跨平台的C++图形用户界面应用程序框架),支持通过 SSH Tunnel 连接。
下载地址:http://redisdesktop.com/download
配置Redis服务地址:
查看可视化keys的值:
C#操作5种基本数据类型
1. 字符串
A: 存储普通字符串,并设置过期时间
int expireTime = 5000;// 5S
存储:client.Add<string>("StringKey","StringValue", DateTime.Now.AddMilliseconds(expireTime));
获取:client.Get<string>("StringKey"), DateTime.Now);
B: 存储类对象
Student stud = new Student() { id = "1000", name = "张三" };
存储:client.Add<Student>("StringEntity", stud);
获取:Student Get_stud = client.Get<Student>("StringEntity");
测试用例输出结果:
2. 哈希
存储: client.SetEntryInHash("HashID", "Name", "张三");
A: 遍历HashID值为HashID的keys
获取:List<string> HaskKey = client.GetHashKeys("HashID");
B:遍历HashID值为HashID的values
获取:List<string> HaskValue = client.GetHashValues("HashID");
C:遍历所有keys
获取:List<string> AllKey = client.GetAllKeys();
测试用例输出结果:
3. 链表
A: 队列
入队:client.EnqueueItemOnList("QueueListId", "1");
出队:long q = client.GetListCount("QueueListId");
client.DequeueItemFromList("QueueListId"));
B: 栈
入栈:client.PushItemToList("StackListId", "1");
出栈:client.PopItemFromList("StackListId")
测试用例输出:
4. 无序集合
存储: client.AddItemToSet("SetA", "1");
获取:HashSet<string> setA = client.GetAllItemsFromSet("SetA");
A:并集
HashSet<string> hashUnion = client.GetUnionFromSets(new string[] { "SetA", "SetB" });
B:交集
HashSet<string> intersectSet = client.GetIntersectFromSets(new string[] { "SetA", "SetB" });
C:差集
HashSet<string> setOfDiffSetAToSetB = client.GetDifferencesFromSet("SetA", new string[] { "SetB" });
测试用例输出:
5. 有序集合
存储:client.AddItemToSortedSet("SetSorted", "A");
输出:List<string> listSetSorted = client.GetAllItemsFromSortedSet("SetSorted");
测试用例输出:
Redis应用场景
只是介绍我本人在使用Redis时用到的场景,仅个人观点。
A.抢XXX赠券、抽奖系统的奖品库存,使用的Redis中的链表
前一天晚上通过定时服务推送奖品库存,使用LPUSH命令将乱序的奖品推入List中,抽奖时则调用LPOP命令,将最左侧奖品弹出队列,提示用户中奖。同时,发送异步消息,让消息去处理中奖纪录并插入关系型数据库中。
好处:
出队操作速度极快,可以满足多人并发抽奖的场景。
使用了消息队列,避免了数据库并发操作。
B.某活动累计充值xxx元,奖励xxx。使用Redis中的string/hash(哈希)结构
用户每次充值,都发送一个充值MQ事件(使用RabbitMQ),另一个程序,消费充值MQ事件,将事件中的用户ID、充值金额分别存到Redis(string/hash)里面。
以后,就可以直接汇总用户总充值金额给满足条件的客户赠送奖品。
好处:
完全避免了关系性数据库的查询插入操作
Redis的查询速度非常快,提升了用户体验
扩展阅读
1. redis持久化RDB和AOF http://my.oschina.net/davehe/blog/174662
2. Redis作者谈Redis应用场景 http://blog.nosqlfan.com/html/2235.html
3. Redis使用总结之与Memcached异同 http://www.cnblogs.com/ceecy/p/3279407.html
4. Redis内存使用优化与存储 http://www.infoq.com/cn/articles/tq-redis-memory-usage-optimization-storage
5. Redis学习手册(目录) http://www.cnblogs.com/stephen-liu74/archive/2012/04/16/2370212.html
演示代码下载:http://download.csdn.net/detail/jys1216/8991915
- Redis演示及使用场景
- Redis数据类型及使用场景
- Redis数据类型及使用场景
- Redis实战经验及使用场景
- Redis实战经验及使用场景
- redis的类型、原理及使用场景
- redis的类型、原理及使用场景
- Redis集群方案及使用场景
- redis的类型、原理及使用场景
- Redis和MySQL数据同步及Redis使用场景
- Redis:九、redis使用场景
- Redis:九、redis使用场景
- redis的五种数据结构及使用场景
- Redis的六种特性及使用场景
- redis学习日志一(简介,特点及使用场景)
- redis支持的数据类型、操作指令及使用场景
- Redis不同数据类型命令使用及应用场景
- mysql,redis,mongodb的使用场景及区别
- 安装 Intel RST (Intel Rapid Storage Technology) 磁盘阵列驱动时,提示 “此产品有一个挂起的重新启动” 的解决方法
- 快速实现RecycleView的网格和瀑布流布局添加头部(尾部原理一样,故略之)
- java中static关键字的用法详解
- Java中this关键字用法详解
- LeetCode 题解(158): Gray Code
- Redis演示及使用场景
- Tracking metrics for both multiple targets and single target
- 客户信息一览
- NServiceBus
- Ubuntu启动盘制作(U盘)
- .NET 环境中使用RabbitMQ
- NET下RabbitMQ实践[配置篇]
- NET下RabbitMQ实践[示例篇]
- 黑马程序员-Java基础:继承和多态