springboot+redis+mysql+quartz-通过Java操作jedis使用pipeline获取缓存数据定时更新数据库

news/2024/7/3 17:48:29

一、重点

代码讲解:6-点赞功能-定时持久化到数据库-pipeline+lua-优化pipeline_哔哩哔哩_bilibili

https://www.bilibili.com/video/BV1yP411C7dr

代码:

blogLike_schedule/like06 · xin麒/XinQiUtilsOrDemo - 码云 - 开源中国 (gitee.com)

https://gitee.com/flowers-bloom-is-the-sea/XinQiUtilsOrDemo/tree/master/blogLike_schedule/like06

数据库表的设计:
blogLike_schedule · xin麒/XinQiUtilsOrDemo - 码云 - 开源中国 (gitee.com)

https://gitee.com/flowers-bloom-is-the-sea/XinQiUtilsOrDemo/tree/master/blogLike_schedule

架构图:

在这里插入图片描述

最开始的代码版本:

(108条消息) springboot+redis+mysql+quartz-通过Java操作redis的KEYS*命令获取缓存数据定时更新数据库_xin麒的博客-CSDN博客

二、核心代码:

定时板块就看这篇吧:

    @Override
    public void updateAllLikeListToDatabaseByPipeline1() {
        String prefix = "BLOG_LIKED_KEY";
//        Set<String> keys = getAllRedisKeyByKeys(prefix);//这个不行!
        Set<String> keys = getAllRedisKeyByScanThroughMatch(prefix);//通过scan拿到以BLOG_LIKED_KEY开头的所有key
        if (keys == null || keys.size() == 0) return;
        Map<Long,Map<String,String>> maps = getIdWithTimeListsByPipelineByJedis(keys);
        if (maps == null || maps.size() == 0) return;

        for (Map.Entry<Long, Map<String, String>> entry : maps.entrySet()) {
            Long blogId = entry.getKey();
            Map<String, String> likeList = entry.getValue();
            updateLikeListByBlogId(blogId,likeList);
        }

    }

这里还是算半Pipeline方法,因为zset的键是通过scan扫描获取到的,这里的scan扫描的方法应该是可以放到管道里面的。但是呢因为Pipeline不适合做太强逻辑相关的命令,所以这个应该也算可以的了。为什么?这里可以去看看这篇文章:

(108条消息) redis事务-pipeline-lua三者区别简单概括_xin麒的博客-CSDN博客

先通过scan方法扫出

public Set<String> getAllRedisKeyByScanThroughMatch(String prefix) {//找不到stringRedisTemplate对Zset里键值对扫描的资料
    Jedis jedis = null;
    Set<String> blogLikeList = new HashSet<>();
    ScanParams scanParams = new ScanParams();
    try {
        jedis = jedisPool.getResource();
        String cursor = "0";


        do {
            // 扫描并获取一部分key
            ScanResult<String> result = jedis.scan(cursor, scanParams.match(prefix.concat("*")));
            // 记录cursor
            cursor = result.getCursor();
            List<String> list = result.getResult();
            if (list == null || list.isEmpty()) {
                break;
            }

            // 遍历
            for (String key : list) {
                log.debug("key is {}", key);//这里可以看到有哪些key
                blogLikeList.add(key);
            }

        } while (!cursor.equals("0"));
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (jedis != null) jedis.close();
    }
    return blogLikeList;
}

然后再通过这个方法来使用pipeline技术:

    public Map<Long,Map<String,String>> getIdWithTimeListsByPipelineByJedis(Collection<String> collection) {

        String prefix = "BLOG_LIKED_KEY";
        Jedis jedis = null;
        Map<Long,Map<String,String>> maps = null;
        try {
            jedis = jedisPool.getResource();
            Pipeline pipe = jedis.pipelined();
            ArrayList<Long> blogKeyIds = new ArrayList<>(collection.size());
            for (String key : collection) {
                pipe.zrangeWithScores(key, 0, -1);
                blogKeyIds.add(Long.parseLong(key.substring(prefix.length(),key.length())));
            }
            List<Object> response = pipe.syncAndReturnAll();
            maps = pipeLineResponseTransformedToMap(response,blogKeyIds);
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
        log.debug("maps is {}",maps);
        return maps;
    }

三、其他

现在是2023-07-01,不得不吐槽stringRedisTemplateRedisTemplate,如果要来实现使用pipeline技术将缓存里面的zset数据获取得到,好像只能用jedis了,其他的都很难找得到参考资料,全网搜不到,官网也很少资料(可能没细看),笑死,所以后面就用jedis来实现了。

其他类似的文章:

(108条消息) springboot+redis+mysql+quartz-通过Java操作redis的KEYS*命令获取缓存数据定时更新数据库_xin麒的博客-CSDN博客

(108条消息) springboot+redis+mysql+quartz-通过Java操作jedis定时使用lua脚本获取缓存数据并更新数据库_xin麒的博客-CSDN博客

(108条消息) lua脚本获取table类型-Java使用lua脚本操作redis获取zset元素的集合_xin麒的博客-CSDN博客

(108条消息) springboot+redis+mysql+quartz-通过Java操作jedis使用pipeline获取缓存数据定时更新数据库_xin麒的博客-CSDN博客

(108条消息) springboot+redis+mysql+quartz-通过Java操作jedis的scan命令获取缓存数据定时更新数据库_xin麒的博客-CSDN博客


http://www.niftyadmin.cn/n/3652184.html

相关文章

Unix程序设计:实现wc命令

上次实现了下cp命令&#xff0c;这次实现下wc命令&#xff0c;比较简单。只是为了学习&#xff0c;没有太多技术含量&#xff0c;权当了解。我们知道wc命令就是统计输入的字符多少个字符、单词、行数&#xff0c;这么些功能&#xff0c;我们知道一般一行就是一个回车来算的&…

使用PHP往Windows系统中添加用户

再csdn论坛上看到类似问题&#xff0c;所以有了一下回答&#xff0c;一个简单的思路。http://community.csdn.net/Expert/topic/4190/4190360.xml?temp.7591669可以实现, 方法有二。一、再Web中添加用户因为添加用户&#xff0c;所以你运行PHP程序的用户必须是管理员权限(Admi…

Linux系统网卡配置

Linux系统网卡配置 点击编辑&#xff0c;虚拟网络编辑器

轻型数据库SQLite结合PHP的开发

轻型数据库SQLite结合PHP的开发SQLite是一款轻型的数据库&#xff0c;它的设计目标是嵌入式的&#xff0c;而且目前已经在很多嵌入式产品中使用了它&#xff0c;它占用资源非常的低&#xff0c;在嵌入式设备中&#xff0c;可能只需要几百K的内存就够了。它能够支持Windows/Linu…

Linux基础命令(1)

内容概要1、什么是操作系统内核2、Shell3、Linux命令类型4、Linux的命令格式5、获取命令的帮助方法6、常用命令相对路径的表现形式ls的常用选项1、什么是操作系统内核 操作系统内核就是会调用计算机中的硬件资源&#xff0c;操作系统会把二进制数封装成各种各样的系统调用&…

使用JavaScript写的操作系统和输入法程序

JavaScript是一门客户端的脚本语言&#xff0c;但是你千万不要认为它功能弱哦&#xff0c;因为你看了下面的例子之后就明白了。一. 用JavaScript写的操作系统呵呵,那天同事开玩笑说,要是有个用JS写的操作系统就好了. 我们都笑他异想天开&#xff0c;想不到的是&#xff0c;竟然…

Unix程序设计:实现cp命令

最近苦读《Unix系统编程》便写了一些实例&#xff0c;逐步增加自己Unix程序设计的能力。首先来实现一个Unix下常用命令&#xff1a;cp先看代码&#xff1a;#include #include #include #define BUFSIZE 512#define PERM 0755/* copy file function */int copyfile(const char…

PHP中模板分页的处理

PHP普通开发中php代码和html代码夹杂的情况中处理分页是比较简单的&#xff0c;也可以构建成函数的形式。最近开发中使用 Pear::DB Smarty 的结构&#xff0c;于是考虑如果对模板进行分页&#xff0c;因为不能直接操作页面&#xff0c;所以就考虑生成分页字符串的形式。因为是…