zoukankan      html  css  js  c++  java
  • 使用redisList的做同步队列处理数据

    场景:

    在多数据处理的时候,有先有后,比如我们多订单,在同步处理时,想要做到同步处理,先进先出,或者后进先出来处理数据,那么我们可以使用redis的list实现。

    为了能在客户端工具中看到存储在redis中的值,我们使用存储都是字符串方式进行存储。

    测试例子。

    @Resource
        private StringRedisTemplate stringRedisTemplate;
        
        /**
         * 插入一条记录stringRedisTemplate
         * @throws Exception
         */
        @Test
        public void stringredislist_leftpush() throws Exception {
            String redisKey = "stringtest:myredis.list";
            for (int i = 0; i < 10; i++) {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("key" + i, "value" + i);
                stringRedisTemplate.opsForList().leftPush(redisKey, JSON.toJSONString(map));
            }
        }
    
        /**
         * 插入一条记录stringRedisTemplate
         * @throws Exception
         */
        @Test
        public void stringredislist_rightpop() throws Exception {
            String redisKey = "stringtest:myredis.list";
            this.popData("弹出信息", redisKey);
        }
        /**弹出信息*/
        private void popData(String msg, String redisKey) {
            if (stringRedisTemplate.opsForList().size(redisKey) > 0) {
                String objValue = stringRedisTemplate.opsForList().rightPop(redisKey);
                System.out.println(objValue);
                Map<String, Object> map = JSONObject.toJavaObject(JSONObject.parseObject(objValue), Map.class); // object 转换
                System.out.println(map.keySet());
                System.out.println(redisKey + "数据处理,剩余:" + stringRedisTemplate.opsForList().size(redisKey));
                popData(msg, redisKey);
            } else {
                System.out.println(redisKey + "数据处理完成");
                return;
            }
        }

    执行日志打印:

    stringredislist_rightpop....................start....................
    {"key0":"value0"}
    [key0]
    stringtest:myredis.list数据处理,剩余:9
    {"key1":"value1"}
    [key1]
    stringtest:myredis.list数据处理,剩余:8
    {"key2":"value2"}
    [key2]
    stringtest:myredis.list数据处理,剩余:7
    {"key3":"value3"}
    [key3]
    stringtest:myredis.list数据处理,剩余:6
    {"key4":"value4"}
    [key4]
    stringtest:myredis.list数据处理,剩余:5
    {"key5":"value5"}
    [key5]
    stringtest:myredis.list数据处理,剩余:4
    {"key6":"value6"}
    [key6]
    stringtest:myredis.list数据处理,剩余:3
    {"key7":"value7"}
    [key7]
    stringtest:myredis.list数据处理,剩余:2
    {"key8":"value8"}
    [key8]
    stringtest:myredis.list数据处理,剩余:1
    {"key9":"value9"}
    [key9]
    stringtest:myredis.list数据处理,剩余:0
    stringtest:myredis.list数据处理完成
    耗时  0.359 ms
    stringredislist_rightpop....................end....................

    在客户端 redis desktop中看到存储信息。

    如果使用对象存储,那么看到的将是。。看不明白的东西。

  • 相关阅读:
    (转载)直接用SQL语句把DBF导入SQLServer
    (转载)SQLServer存储过程返回值总结
    (转载)MS SQL Server 未公开的加密函数有哪些?
    (转载)SQL语句,纵列转横列
    (转载)直接用SQL语句把DBF导入SQLServer
    (转载)用SQL语句创建Access表
    (转载)根据数据字典表定义的表结构,生成创建表的SQL语句
    (转载)sql语句解决分页问题
    (转载)总结一下SQL语句中引号(')、quotedstr()、('')、format()在SQL语句中的用法
    (转载)异构数据库之间完全可以用SQL语句导数据
  • 原文地址:https://www.cnblogs.com/a393060727/p/14884837.html
Copyright © 2011-2022 走看看