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

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

一、重点

代码讲解:7.1点赞功能-定时持久化到数据库-Java整合lua_哔哩哔哩_bilibili

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

代码:

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

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

数据库表的设计:
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博客

lua脚本:

local prefix = KEYS[1]; 
local redisKeys = redis.call('keys',prefix ..'*');

if(not redisKeys) 
    then    
    	return (nil);
end;

local maps = {};

for i, v in pairs(redisKeys) do
    local blogId = string.sub(v,string.len(prefix) + 1,string.len(v));
    local zset = redis.call('zrange',v,'0','-1','withscores');
    table.insert(maps,blogId);
    table.insert(maps,zset);    
end;

return maps;

lua脚本就另外开一篇文章来讲:

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

@Override
public void updateAllLikeListToDatabaseByLua() {
    String prefix = "BLOG_LIKED_KEY";

    Map<Long, Map<String, String>> maps = getMapsByLuaUseJedis(prefix);
    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);
    }

}
    public Map<Long, Map<String, String>> getMapsByLuaUseJedis(String prefix) {
        Map<Long, Map<String, String>> maps = null;
        String script = "local prefix = KEYS[1]; \n" +
                "local redisKeys = redis.call('keys',prefix ..'*');\n" +
                "\n" +
                "if(not redisKeys) \n" +
                "    then    \n" +
                "    \treturn (nil);\n" +
                "end;\n" +
                "\n" +
                "local maps = {};\n" +
                "\n" +
                "for i, v in pairs(redisKeys) do\n" +
                "    local blogId = string.sub(v,string.len(prefix) + 1,string.len(v));\n" +
                "    local zset = redis.call('zrange',v,'0','-1','withscores');\n" +
                "    table.insert(maps,blogId);\n" +
                "    table.insert(maps,zset);    \n" +
                "end;\n" +
                "\n" +
                "return maps;";
        Jedis jedis = null;
        try {
            jedis =  jedisPool.getResource();
            List result = (List) jedis.eval(script, Arrays.asList(prefix), new ArrayList<>());//https://blog.csdn.net/EnjoyFight/article/details/127808971
            System.out.println("result is " + result);//result is [1, [1, 1688218686914]]
/*        log.debug("result is ",result);//不知道为什么不可以log打印
        log.debug("result is null?{}",result==null);
        String s = JSONUtil.toJsonStr(result);
        log.debug("s is ",s);
        log.debug("s is null? {}",s==null);
        log.debug("s .len is  {}",s.length());*/


            maps = parseLuaResultToMaps(result);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (jedis != null) jedis.close();
        }
        return maps;

    }

基本内容都在视频讲了的。

其他类似的文章:

(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博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值