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中看到存储信息。

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

  • 相关阅读:
    [Functional Programming] liftA2 and converge
    [Javascript] Convert a forEach method to generator
    [React Native] Up & Running with React Native & TypeScript
    [React] Create a Query Parameter Modal Route with React Router
    [ES2019] Represent Collision-free String Constants as Symbols in JavaScript
    形形色色的软件生命周期模型(4)——MSF、实用型
    整型数组处理算法(九)给定任意一个正整数,求比这个数大且最小的“不重复数”(性能优化)[2014百度笔试题]
    Easyui获取数据库date数据的显示
    [置顶] 如何更改CSDN博客高亮代码皮肤的样式,使博客看起来更有范(推荐)
    try-catch-finally 引发的奇怪问题
  • 原文地址:https://www.cnblogs.com/a393060727/p/14884837.html
Copyright © 2011-2022 走看看