一、重點(diǎn)
代碼講解:6-點(diǎn)贊功能-定時(shí)持久化到數(shù)據(jù)庫(kù)-pipeline+lua-優(yōu)化pipeline_嗶哩嗶哩_bilibili
https://www.bilibili.com/video/BV1yP411C7dr
代碼:
blogLike_schedule/like06 · xin麒/XinQiUtilsOrDemo - 碼云 - 開源中國(guó) (gitee.com)
https://gitee.com/flowers-bloom-is-the-sea/XinQiUtilsOrDemo/tree/master/blogLike_schedule/like06
數(shù)據(jù)庫(kù)表的設(shè)計(jì):
blogLike_schedule · xin麒/XinQiUtilsOrDemo - 碼云 - 開源中國(guó) (gitee.com)
https://gitee.com/flowers-bloom-is-the-sea/XinQiUtilsOrDemo/tree/master/blogLike_schedule
架構(gòu)圖:
最開始的代碼版本:
(108條消息) springboot+redis+mysql+quartz-通過Java操作redis的KEYS*命令獲取緩存數(shù)據(jù)定時(shí)更新數(shù)據(jù)庫(kù)_xin麒的博客-CSDN博客
二、核心代碼:
定時(shí)板塊就看這篇吧:
@Override
public void updateAllLikeListToDatabaseByPipeline1() {
String prefix = "BLOG_LIKED_KEY";
// Set<String> keys = getAllRedisKeyByKeys(prefix);//這個(gè)不行!
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方法,因?yàn)閦set的鍵是通過scan掃描獲取到的,這里的scan掃描的方法應(yīng)該是可以放到管道里面的。但是呢因?yàn)镻ipeline不適合做太強(qiáng)邏輯相關(guān)的命令,所以這個(gè)應(yīng)該也算可以的了。為什么?這里可以去看看這篇文章:
(108條消息) redis事務(wù)-pipeline-lua三者區(qū)別簡(jiǎn)單概括_xin麒的博客-CSDN博客
先通過scan方法掃出
public Set<String> getAllRedisKeyByScanThroughMatch(String prefix) {//找不到stringRedisTemplate對(duì)Zset里鍵值對(duì)掃描的資料
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;
}
然后再通過這個(gè)方法來(lái)使用pipeline技術(shù):
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;
}
三、其他
現(xiàn)在是2023-07-01,不得不吐槽stringRedisTemplate
和RedisTemplate
,如果要來(lái)實(shí)現(xiàn)使用pipeline技術(shù)將緩存里面的zset數(shù)據(jù)獲取得到,好像只能用jedis
了,其他的都很難找得到參考資料,全網(wǎng)搜不到,官網(wǎng)也很少資料(可能沒細(xì)看),笑死,所以后面就用jedis
來(lái)實(shí)現(xiàn)了。
其他類似的文章:
(108條消息) springboot+redis+mysql+quartz-通過Java操作redis的KEYS*命令獲取緩存數(shù)據(jù)定時(shí)更新數(shù)據(jù)庫(kù)_xin麒的博客-CSDN博客
(108條消息) springboot+redis+mysql+quartz-通過Java操作jedis定時(shí)使用lua腳本獲取緩存數(shù)據(jù)并更新數(shù)據(jù)庫(kù)_xin麒的博客-CSDN博客
(108條消息) lua腳本獲取table類型-Java使用lua腳本操作redis獲取zset元素的集合_xin麒的博客-CSDN博客
(108條消息) springboot+redis+mysql+quartz-通過Java操作jedis使用pipeline獲取緩存數(shù)據(jù)定時(shí)更新數(shù)據(jù)庫(kù)_xin麒的博客-CSDN博客文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-559616.html
(108條消息) springboot+redis+mysql+quartz-通過Java操作jedis的scan命令獲取緩存數(shù)據(jù)定時(shí)更新數(shù)據(jù)庫(kù)_xin麒的博客-CSDN博客文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-559616.html
到了這里,關(guān)于springboot+redis+mysql+quartz-通過Java操作jedis使用pipeline獲取緩存數(shù)據(jù)定時(shí)更新數(shù)據(jù)庫(kù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!