Redis_简单使用

可基于内存也可持久化的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 桌面管理工具

下载地址:http://redisdesktop.com/download

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应用场景

A.抢XXX赠券、抽奖系统的奖品库存,使用的Redis中的链表

前一天晚上通过定时服务推送奖品库存,使用LPUSH命令将乱序的奖品推入List中,抽奖时则调用LPOP命令,将最左侧奖品弹出队列,提示用户中奖。同时,发送异步消息,让消息去处理中奖纪录并插入关系型数据库中。


好处:
出队操作速度极快,可以满足多人并发抽奖的场景。 
使用了消息队列,避免了数据库并发操作。

 完全避免了关系性数据库的查询插入操作
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

原文地址:https://www.cnblogs.com/ingstyle/p/6655857.html