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博客文章来源:https://www.toymoban.com/news/detail-540826.html
(108条消息) springboot+redis+mysql+quartz-通过Java操作jedis的scan命令获取缓存数据定时更新数据库_xin麒的博客-CSDN博客文章来源地址https://www.toymoban.com/news/detail-540826.html
到了这里,关于springboot+redis+mysql+quartz-通过Java操作jedis定时使用lua脚本获取缓存数据并更新数据库的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!