zoukankan      html  css  js  c++  java
  • Redis演示及使用场景

    概述

    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

  • 相关阅读:
    过滤器判断请求参数中是否含有某一字段
    vscode开发vue项目实现pc端自适应_cssrem_rem_reset.scss,pc端媒体查询常用设置
    element_ui的datePicker修改样式
    TCP/IP 卷一 APR、RAPR、ICMP
    TCP/IP 卷一:协议(IP层)
    常见负载均衡策略
    TCP/IP 卷一:协议(链路层)
    TCP/IP 卷一:协议(概述)
    RokcetMQ
    Zookeeper集群
  • 原文地址:https://www.cnblogs.com/jys509/p/4721638.html
Copyright © 2011-2022 走看看