Redis實(shí)戰(zhàn)之百度首頁新聞熱榜的實(shí)現(xiàn)代碼
目標(biāo)
利用Redis實(shí)現(xiàn)類似百度首頁新聞熱榜功能。
功能
新聞排行榜以熱度為指標(biāo)降序排序,這里假設(shè)熱度就是評(píng)論數(shù)量且統(tǒng)計(jì)的熱度時(shí)間范圍以當(dāng)天為準(zhǔn);根據(jù)新聞的時(shí)效性,這里假設(shè)每15分鐘刷新一次新聞榜單。
分析 Zset數(shù)據(jù)類型:一個(gè)有序集合最多 個(gè)元素,集合元素有序不可重復(fù),每個(gè)元素都會(huì)關(guān)聯(lián)一個(gè)double類型的分?jǐn)?shù)。元素根據(jù)分?jǐn)?shù)從小到大的排序,分?jǐn)?shù)可以重復(fù)。zscore命令可以對(duì)分?jǐn)?shù)實(shí)現(xiàn)增量,且如果該Zset中沒有該元素,則會(huì)創(chuàng)建該條數(shù)據(jù)。可以將模塊名+當(dāng)天的時(shí)間作為Zset的鍵,用戶評(píng)論量作為分?jǐn)?shù),新聞標(biāo)題作為值,每當(dāng)用戶評(píng)論一次新聞,分?jǐn)?shù)則相應(yīng)地加1。每隔15分鐘提取新聞統(tǒng)計(jì)中的前30名(包含第30名)榜單,放入到新聞熱榜的Zset中。
代碼實(shí)現(xiàn)
控制層
package com.shoppingcart.controller; import com.shoppingcart.service.NewsTopServer; import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.List; import java.util.Map; /** * 新聞排行榜 */ @RestController @RequestMapping("/newsTop") public class NewsTopController { @Resource public NewsTopServer newsTopServer; /** * http://localhost:8099/newsTop/zscoreNews?newTitle=《歡樂喜劇人7》全新賽制養(yǎng)成新人&score=434000 * 創(chuàng)建新聞統(tǒng)計(jì)&實(shí)時(shí)統(tǒng)計(jì)新聞熱度 * @param newTitle 新聞標(biāo)題 (根據(jù)業(yè)務(wù)也可以寫成新聞ID) * @param score 熱度增量 * @return 給新聞一個(gè)增量以后,返回新聞的當(dāng)前分?jǐn)?shù)。 */ @GetMapping("/zscoreNews") public Map<String, Object> zscoreNews( @RequestParam(value = "newTitle", required = true) String newTitle, @RequestParam(value = "score", defaultValue = "1") double score ) { Map<String, Object> map = newsTopServer.incrementScore(newTitle, score); return map; } /** * http://localhost:8099/newsTop/findNewByNewTitle?newTitle=《歡樂喜劇人7》全新賽制養(yǎng)成新人 * 查詢某條新聞的熱度 * @param newTitle * @return */ @GetMapping("/findNewByNewTitle") public Map<String, Object> findNewByNewTitle( @RequestParam(value = "newTitle", required = true) String newTitle ) { Map<String, Object> map = newsTopServer.findNewByNewTitle(newTitle); return map; } /** * http://localhost:8099/newsTop/createNewsTop?startPage=0&endPage=29 * 對(duì)統(tǒng)計(jì)的新聞數(shù)據(jù)降序排序,并將[29,0]之間的數(shù)據(jù)放入新聞排行榜。(這個(gè)方法可以設(shè)置成定時(shí)任務(wù)。) * @param startPage 開始下標(biāo) * @param endPage 結(jié)束下標(biāo) * @return */ @GetMapping("/createNewsTop") public Map<String, Object> createNewsTop( @RequestParam(value = "startPage", defaultValue = "0") int startPage, @RequestParam(value = "endPage", defaultValue = "29") int endPage ) { Map<String, Object> map = newsTopServer.createNewsTop(startPage, endPage); return map; } /** * http://localhost:8099/newsTop/newsTop?startPage=20&endPage=29 * 對(duì)統(tǒng)計(jì)的新聞數(shù)據(jù)降序排序,并將[29,0]之間的數(shù)據(jù)放入新聞排行榜。(這個(gè)方法可以設(shè)置成定時(shí)任務(wù)。) * * @param startPage 開始下標(biāo) * @param endPage 結(jié)束下標(biāo) * @return */ @GetMapping("/newsTop") public Map<String, Object> newsTop( @RequestParam(value = "startPage", defaultValue = "0") int startPage, @RequestParam(value = "endPage", defaultValue = "9") int endPage ) { Map<String, Object> map = newsTopServer.newsTop(startPage, endPage); return map; } /** * http://localhost:8099/newsTop/addTestData * 批量增加測試數(shù)據(jù)(新聞統(tǒng)計(jì)) */ @PostMapping("/addTestData") public void addTestData(@RequestBody List<Map<String, Object>> list) { for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i).get("value").toString()); System.out.println(Double.parseDouble(list.get(i).get("score").toString())); zscoreNews(list.get(i).get("value").toString(), Double.parseDouble(list.get(i).get("score").toString())); } } /**新增測試數(shù)據(jù): [ { "score": 2356428.0, "value": "《蒙面唱將猜猜猜》第五季收官" }, { "score": 2335456.0, "value": "《歡樂喜劇人7》全新賽制養(yǎng)成新人" }, { "score": 987655.0, "value": "《星光大道》2020年度總決賽" }, { "score": 954566.0, "value": "網(wǎng)易北京:重構(gòu)夢幻西游項(xiàng)目" }, { "score": 943665.0, "value": "神武驚現(xiàn)靚號(hào):44488888" }, { "score": 876653.0, "value": "小米手機(jī):紅米" }, { "score": 875444.0, "value": "英特爾擴(kuò)大外包" }, { "score": 755656.0, "value": "多益廣州舉辦神武4手游比賽" }, { "score": 687466.0, "value": "亮劍重播超記錄" }, { "score": 567645.0, "value": "春節(jié)快到了" }, { "score": 554342.0, "value": "購票狂潮" }, { "score": 466654.0, "value": "達(dá)摩院旗下?lián)碛?0多位世界級(jí)的科學(xué)家" }, { "score": 456666.0, "value": "NBA MVP候選人" }, { "score": 435654.0, "value": "CBA最佳新秀" }, { "score": 392875.0, "value": "數(shù)字貨幣新時(shí)代" }, { "score": 300454.0, "value": "網(wǎng)易新手游即將發(fā)布" }, { "score": 277654.0, "value": "CBA12強(qiáng)排名:四強(qiáng)格局已定" }, { "score": 265656.0, "value": "用黑科技悄悄改變大眾生活" }, { "score": 234665.0, "value": "玉溪:致力打造全省數(shù)字經(jīng)濟(jì)第一城" }, { "score": 234665.0, "value": "廣西培育消費(fèi)新業(yè)態(tài)新模式" }, { "score": 234656.0, "value": "互聯(lián)網(wǎng)產(chǎn)品是順從用戶?還是教育用戶?" }, { "score": 234564.0, "value": "蔣軍:企業(yè)做強(qiáng),做大跟產(chǎn)品的關(guān)系是什么?" }, { "score": 234564.0, "value": "熱搜第一!微信又有重大更新,這次有點(diǎn)炸" }, { "score": 234555.0, "value": "成功的人,往往都讀這“6”種書" }, { "score": 134566.0, "value": "外地職工留蘇州過年 落戶加15分" }, { "score": 133455.0, "value": "蔣軍:成功創(chuàng)業(yè)的7種思維!創(chuàng)業(yè)者必讀!" }, { "score": 98554.0, "value": "阿里平頭哥:首個(gè)RISC - V版安卓10系統(tǒng)順暢運(yùn)行" }, { "score": 87654.0, "value": "不斷增強(qiáng)人民群眾就醫(yī)獲得感" }, { "score": 54347.0, "value": "《星光大道》年度總冠軍出爐" }, { "score": 43335.0, "value": "流量應(yīng)是榜樣,榜樣應(yīng)成力量" }, { "score": 23555.0, "value": "《山海情》:主旋律可以這樣好看" }, { "score": 23456.0, "value": "2021藝考新動(dòng)向" } ] */ }
業(yè)務(wù)層
package com.shoppingcart.service; import java.util.Map; public interface NewsTopServer { Map<String, Object> incrementScore(String newTitle,double zscore); Map<String, Object> findNewByNewTitle(String newTitle); Map<String, Object> createNewsTop(int startPage, int endPage); Map<String, Object> newsTop(int startPage, int endPage); }
package com.shoppingcart.service.impl; import com.shoppingcart.service.NewsTopServer; import com.shoppingcart.utils.RedisService; import org.springframework.data.redis.core.ZSetOperations; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.*; @Service public class NewsTopServerImpl implements NewsTopServer { @Resource private RedisService redisService; @Override public Map<String, Object> incrementScore(String newTitle, double score) { Map<String, Object> map = new HashMap<>(); //String key= "newsSta:"+DateUtils.dateToString(new Date(),"yyyyMMdd"); String key = "newsSta:" + "20210123"; Double d = redisService.incrementScore(key, newTitle, score); Map<String, Object> m = new HashMap<String, Object>() { { put("key", key); put("newTitle", newTitle); put("score", d); } }; map.put("data", m); map.put("code", 0); return map; } @Override public Map<String, Object> findNewByNewTitle(String newTitle) { //String key= "newsSta:"+DateUtils.dateToString(new Date(),"yyyyMMdd"); String key = "newsSta:" + "20210123"; Double d = redisService.score(key, newTitle); Map<String, Object> map = new HashMap<>(); Map<String, Object> m = new HashMap<String, Object>() { { put("key", key); put("newTitle", newTitle); put("score", d); } }; map.put("data", m); map.put("code", 0); return map; } /** * @param startPage * @param endPage * @return */ @Override public Map<String, Object> createNewsTop(int startPage, int endPage) { Map<String, Object> map = new HashMap<>(); //新聞統(tǒng)計(jì)鍵 //String newsStaKey= "newsSta:"+DateUtils.dateToString(new Date(),"yyyyMMdd"); String newsStaKey = "newsSta:" + "20210123"; //新聞前30排名鍵 //String newsTopKey= "newsSta:"+DateUtils.dateToString(new Date(),"yyyyMMdd"); String newsTopKey = "newsTop:" + "20210123"; //查詢前30的信息(Interface Comparable<T> :該接口對(duì)實(shí)現(xiàn)它的每個(gè)類的對(duì)象強(qiáng)加一個(gè)整體排序。) Set<ZSetOperations.TypedTuple<Object>> set = redisService.reverseRangeWithScores(newsStaKey, startPage, endPage); if (set == null || set.size() == 0) { map.put("data", null); map.put("code", 1); return map; } //刪除舊的新聞排行榜 redisService.del(newsTopKey); //添加新聞排行榜數(shù)據(jù) Long zsetSize = redisService.zsetAdd(newsTopKey, set); Map<String, Object> m = new HashMap<String, Object>() { { put("data", set); put("size", zsetSize); } }; map.put("data", m); map.put("code", 0); return map; } /** * 查看新聞熱榜(TOP30) * * @param startPage * @param endPage * @return */ @Override public Map<String, Object> newsTop(int startPage, int endPage) { //新聞統(tǒng)計(jì)鍵 //String newsStaKey= "newsSta:"+DateUtils.dateToString(new Date(),"yyyyMMdd"); String newsStaKey = "newsSta:" + "20210123"; //新聞前30排名鍵 //String newsTopKey= "newsSta:"+DateUtils.dateToString(new Date(),"yyyyMMdd"); String newsTopKey = "newsTop:" + "20210123"; Set<ZSetOperations.TypedTuple<Object>> set = redisService.reverseRangeWithScores(newsTopKey, startPage, endPage); Map<String, Object> m = new HashMap<String, Object>(); m.put("data", set); m.put("size", set.size()); //新聞排行榜為空,也許現(xiàn)在正在添加數(shù)據(jù),先查詢新聞統(tǒng)計(jì)鍵。 if (set == null || set.size() == 0) { //查詢前30的信息(Interface Comparable<T> :該接口對(duì)實(shí)現(xiàn)它的每個(gè)類的對(duì)象強(qiáng)加一個(gè)整體排序。) Set<ZSetOperations.TypedTuple<Object>> set2 = redisService.reverseRangeWithScores(newsStaKey, startPage, endPage); m.put("data", set); m.put("size", set.size()); } Map<String, Object> map = new HashMap<>(); map.put("data", m); map.put("code", 0); return map; } }
工具類
package com.shoppingcart.utils; import java.text.SimpleDateFormat; import java.util.Date; public class DateUtils { // 日期轉(zhuǎn)字符串,返回指定的格式 public static String dateToString(Date date, String dateFormat) { SimpleDateFormat sdf = new SimpleDateFormat(dateFormat); return sdf.format(date); } }
package com.shoppingcart.utils; import com.alibaba.fastjson.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.connection.RedisZSetCommands; import org.springframework.data.redis.connection.SortParameters; import org.springframework.data.redis.core.DefaultTypedTuple; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.ZSetOperations; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; import org.w3c.dom.ranges.Range; import java.util.*; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; @Service public class RedisService { @Autowired private RedisTemplate<String, Object> redisTemplate; // =============================common============================ /** * 指定緩存失效時(shí)間 * * @param key 鍵 * @param time 時(shí)間(秒) * @return */ public boolean expire(String key, long time) { try { if (time > 0) { redisTemplate.expire(key, time, TimeUnit.SECONDS); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 根據(jù)key 獲取過期時(shí)間 * * @param key 鍵 不能為null * @return 時(shí)間(秒) 返回0代表為永久有效 */ public long getExpire(String key) { return redisTemplate.getExpire(key, TimeUnit.SECONDS); } /** * 判斷key是否存在 * * @param key 鍵 * @return true 存在 false不存在 */ public boolean hasKey(String key) { try { return redisTemplate.hasKey(key); } catch (Exception e) { e.printStackTrace(); return false; } } /** * 刪除緩存 * * @param key 可以傳一個(gè)值 或多個(gè) */ @SuppressWarnings("unchecked") public void del(String... key) { if (key != null && key.length > 0) { if (key.length == 1) { redisTemplate.delete(key[0]); } else { List<String> list = new ArrayList<>(Arrays.asList(key)); redisTemplate.delete(list); } } } /** * 刪除緩存 * * @param keys 可以傳一個(gè)值 或多個(gè) */ @SuppressWarnings("unchecked") public void del(Collection keys) { if (org.apache.commons.collections4.CollectionUtils.isNotEmpty(keys)) { redisTemplate.delete(keys); } } // ============================String============================= /** * 普通緩存獲取 * * @param key 鍵 * @return 值 */ public Object get(String key) { return key == null ? null : redisTemplate.opsForValue().get(key); } /** * 普通緩存放入 * * @param key 鍵 * @param value 值 * @return true成功 false失敗 */ public boolean set(String key, Object value) { try { redisTemplate.opsForValue().set(key, value); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 普通緩存放入并設(shè)置時(shí)間 * * @param key 鍵 * @param value 值 * @param time 時(shí)間(秒) time要大于0 如果time小于等于0 將設(shè)置無限期 * @return true成功 false 失敗 */ public boolean set(String key, Object value, long time) { try { if (time > 0) { redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); } else { set(key, value); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 遞增 * * @param key 鍵 * @param delta 要增加幾(大于0) * @return */ public long incr(String key, long delta) { if (delta < 0) { throw new RuntimeException("遞增因子必須大于0"); } return redisTemplate.opsForValue().increment(key, delta); } /** * 遞減 * * @param key 鍵 * @param delta 要減少幾(小于0) * @return */ public long decr(String key, long delta) { if (delta < 0) { throw new RuntimeException("遞減因子必須大于0"); } return redisTemplate.opsForValue().increment(key, -delta); } // ================================Hash================================= /** * HashGet * * @param key 鍵 不能為null * @param item 項(xiàng) 不能為null * @return 值 */ public Object hget(String key, String item) { return redisTemplate.opsForHash().get(key, item); } /** * 獲取hashKey對(duì)應(yīng)的所有鍵值 * * @param key 鍵 * @return 對(duì)應(yīng)的多個(gè)鍵值 */ public Map<Object, Object> hmget(String key) { Map<Object, Object> entries = redisTemplate.opsForHash().entries(key); return entries; } /** * HashSet * * @param key 鍵 * @param map 對(duì)應(yīng)多個(gè)鍵值 * @return true 成功 false 失敗 */ public boolean hmset(String key, Map<String, Object> map) { try { redisTemplate.opsForHash().putAll(key, map); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * HashSet 并設(shè)置時(shí)間 * * @param key 鍵 * @param map 對(duì)應(yīng)多個(gè)鍵值 * @param time 時(shí)間(秒) * @return true成功 false失敗 */ public boolean hmset(String key, Map<String, Object> map, long time) { try { redisTemplate.opsForHash().putAll(key, map); if (time > 0) { expire(key, time); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 向一張hash表中放入數(shù)據(jù),如果不存在則不添加。 * @param key 鍵 * @param item 項(xiàng) * @param value 值 * @return true 成功 false失敗 */ public boolean hsetnx(String key, String item, Object value) { try { Boolean success = redisTemplate.opsForHash().putIfAbsent(key, item, value); return success; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 向一張hash表中放入數(shù)據(jù),如果存在就覆蓋原來的值。 * * @param key 鍵 * @param item 項(xiàng) * @param value 值 * @return true 成功 false失敗 */ public boolean hset(String key, String item, Object value) { try { redisTemplate.opsForHash().put(key, item, value); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 向一張hash表中放入數(shù)據(jù),如果存在就覆蓋原來的值。 * * @param key 鍵 * @param item 項(xiàng) * @param value 值 * @param time 時(shí)間(秒) 注意:如果已存在的hash表有時(shí)間,這里將會(huì)替換原有的時(shí)間 * @return true 成功 false失敗 */ public boolean hset(String key, String item, Object value, long time) { try { redisTemplate.opsForHash().put(key, item, value); if (time > 0) { expire(key, time); } return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 刪除hash表中的值 * * @param key 鍵 不能為null * @param item 項(xiàng) 可以使多個(gè) 不能為null * 返回被刪除的數(shù)量 */ public Long hdel(String key, Object... item) { return redisTemplate.opsForHash().delete(key, item); } /** * 刪除hash表中的值 * * @param key 鍵 不能為null * @param items 項(xiàng) 可以使多個(gè) 不能為null */ public void hdel(String key, Collection items) { redisTemplate.opsForHash().delete(key, items.toArray()); } /** * 判斷hash表中是否有該項(xiàng)的值 * * @param key 鍵 不能為null * @param item 項(xiàng) 不能為null * @return true 存在 false不存在 */ public boolean hHasKey(String key, String item) { return redisTemplate.opsForHash().hasKey(key, item); } /** * hash數(shù)據(jù)類型:給元素一個(gè)增量 如果不存在,就會(huì)創(chuàng)建一個(gè) 并把新增后的值返回 * * @param key 鍵 * @param item 項(xiàng) * @param delta 要增加幾(大于0) * @return */ public double hincr(String key, String item, double delta) { return redisTemplate.opsForHash().increment(key, item, delta); } // ============================set============================= /** * 根據(jù)key獲取Set中的所有值 * * @param key 鍵 * @return */ public Set<Object> sGet(String key) { try { return redisTemplate.opsForSet().members(key); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 根據(jù)value從一個(gè)set中查詢,是否存在 * * @param key 鍵 * @param value 值 * @return true 存在 false不存在 */ public boolean sHasKey(String key, Object value) { try { return redisTemplate.opsForSet().isMember(key, value); } catch (Exception e) { e.printStackTrace(); return false; } } /** * 將數(shù)據(jù)放入set緩存 * * @param key 鍵 * @param values 值 可以是多個(gè) * @return 成功個(gè)數(shù) */ public long sSet(String key, Object... values) { try { return redisTemplate.opsForSet().add(key, values); } catch (Exception e) { e.printStackTrace(); return 0; } } /** * 將數(shù)據(jù)放入set緩存 * * @param key 鍵 * @param values 值 可以是多個(gè) * @return 成功個(gè)數(shù) */ public long sSet(String key, Collection values) { try { return redisTemplate.opsForSet().add(key, values.toArray()); } catch (Exception e) { e.printStackTrace(); return 0; } } /** * 將set數(shù)據(jù)放入緩存 * * @param key 鍵 * @param time 時(shí)間(秒) * @param values 值 可以是多個(gè) * @return 成功個(gè)數(shù) */ public long sSetAndTime(String key, long time, Object... values) { try { Long count = redisTemplate.opsForSet().add(key, values); if (time > 0) expire(key, time); return count; } catch (Exception e) { e.printStackTrace(); return 0; } } /** * 獲取set緩存的長度 * * @param key 鍵 * @return */ public long sGetSetSize(String key) { try { return redisTemplate.opsForSet().size(key); } catch (Exception e) { e.printStackTrace(); return 0; } } /** * 移除值為value的 * * @param key 鍵 * @param values 值 可以是多個(gè) * @return 移除的個(gè)數(shù) */ public long setRemove(String key, Object... values) { try { Long count = redisTemplate.opsForSet().remove(key, values); return count; } catch (Exception e) { e.printStackTrace(); return 0; } } // ===============================list================================= /** * 獲取list緩存的內(nèi)容 * * @param key 鍵 * @param start 開始 * @param end 結(jié)束 0 到 -1代表所有值 * @return */ public List<Object> lGet(String key, long start, long end) { try { return redisTemplate.opsForList().range(key, start, end); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 獲取list緩存的長度 * * @param key 鍵 * @return */ public long lGetListSize(String key) { try { return redisTemplate.opsForList().size(key); } catch (Exception e) { e.printStackTrace(); return 0; } } /** * 通過索引 獲取list中的值 * * @param key 鍵 * @param index 索引 index>=0時(shí), 0 表頭,1 第二個(gè)元素,依次類推;index<0時(shí),-1,表尾,-2倒數(shù)第二個(gè)元素,依次類推 * @return */ public Object lGetIndex(String key, long index) { try { return redisTemplate.opsForList().index(key, index); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 將list放入緩存 * * @param key 鍵 * @param value 值 * @return */ public boolean lSet(String key, Object value) { try { redisTemplate.opsForList().rightPush(key, value); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 將list放入緩存 * * @param key 鍵 * @param value 值 * @param time 時(shí)間(秒) * @return */ public boolean lSet(String key, Object value, long time) { try { redisTemplate.opsForList().rightPush(key, value); if (time > 0) expire(key, time); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 將list放入緩存 * * @param key 鍵 * @param value 值 * @return */ public boolean lSet(String key, List<Object> value) { try { redisTemplate.opsForList().rightPushAll(key, value); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 將list放入緩存 * * @param key 鍵 * @param value 值 * @param time 時(shí)間(秒) * @return */ public boolean lSet(String key, List<Object> value, long time) { try { redisTemplate.opsForList().rightPushAll(key, value); if (time > 0) expire(key, time); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 根據(jù)索引修改list中的某條數(shù)據(jù) * * @param key 鍵 * @param index 索引 * @param value 值 * @return */ public boolean lUpdateIndex(String key, long index, Object value) { try { redisTemplate.opsForList().set(key, index, value); return true; } catch (Exception e) { e.printStackTrace(); return false; } } /** * 移除N個(gè)值為value * * @param key 鍵 * @param count 移除多少個(gè) * @param value 值 * @return 移除的個(gè)數(shù) */ public long lRemove(String key, long count, Object value) { try { Long remove = redisTemplate.opsForList().remove(key, count, value); return remove; } catch (Exception e) { e.printStackTrace(); return 0; } } // ===============================Zset================================= /** * 給key鍵的value增加value分?jǐn)?shù),沒有則會(huì)創(chuàng)建。 * * @param key 鍵 * @param value 值 * @param score 分?jǐn)?shù) */ public Double incrementScore(String key, String value, double score) { //Boolean add = redisTemplate.boundZSetOps(key).add(value, score); Double add = redisTemplate.boundZSetOps(key).incrementScore(value, score); return add; } /** * 獲得指定Zset元素的分?jǐn)?shù) * * @param key * @param value * @return */ public Double score(String key, String value) { Double score = redisTemplate.boundZSetOps(key).score(value); return score; } /** * 升序查詢key集合內(nèi)[endTop,startTop]如果是負(fù)數(shù)表示倒數(shù) * endTop=-1,startTop=0表示獲取所有數(shù)據(jù)。 * * @param key * @param startPage * @param endPage */ public Set<ZSetOperations.TypedTuple<Object>> rangeWithScores(String key, int startPage, int endPage) { Set<ZSetOperations.TypedTuple<Object>> set = redisTemplate.boundZSetOps(key).rangeWithScores(startPage, endPage); return set; } /** * 降序查詢key集合內(nèi)[endTop,startTop],如果是負(fù)數(shù)表示倒數(shù) * endTop=-1,startTop=0表示獲取所有數(shù)據(jù)。 * * @param key * @param startPage * @param endPage */ public Set<ZSetOperations.TypedTuple<Object>> reverseRangeWithScores(String key, int startPage, int endPage) { Set<ZSetOperations.TypedTuple<Object>> set = redisTemplate.boundZSetOps(key).reverseRangeWithScores(startPage, endPage); return set; } /** * 批量新增數(shù)據(jù) * * @param key * @param set * @return */ public Long zsetAdd(String key, Set set) { Long add = redisTemplate.boundZSetOps(key).add(set); return add; } /** * 刪除指定鍵的指定下標(biāo)范圍數(shù)據(jù) * * @param key * @param startPage * @param endPage */ public Long zsetRemoveRange(String key, int startPage, int endPage) { Long l = redisTemplate.boundZSetOps(key).removeRange(startPage, endPage); return l; } /** * 刪除指定鍵的指定值 * * @param key * @param value */ public Long zsetRemove(String key, String value) { Long remove = redisTemplate.boundZSetOps(key).remove(value); return remove; } }
到此這篇關(guān)于Redis實(shí)戰(zhàn)之百度首頁新聞熱榜的文章就介紹到這了,更多相關(guān)Redis百度首頁新聞熱榜內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于redis狀態(tài)監(jiān)控和性能調(diào)優(yōu)詳解
Redis是一種高級(jí)key-value數(shù)據(jù)庫。它跟memcached類似,不過數(shù)據(jù)可以持久化,而且支持的數(shù)據(jù)類型很豐富。有字符串,鏈表、哈希、集合和有序集合5種。下面這篇文章主要給大家介紹了關(guān)于redis狀態(tài)監(jiān)控和性能調(diào)優(yōu)的相關(guān)資料,需要的朋友可以參考下。2017-09-09Redis和Lua實(shí)現(xiàn)分布式限流器的方法詳解
這篇文章主要給大家介紹了關(guān)于Redis和Lua實(shí)現(xiàn)分布式限流器的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Redis和Lua具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Redis之SDS數(shù)據(jù)結(jié)構(gòu)的使用
本文主要介紹了Redis之SDS數(shù)據(jù)結(jié)構(gòu)的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08淺談Redis如何應(yīng)對(duì)并發(fā)訪問
本文主要介紹了Redis如何應(yīng)對(duì)并發(fā)訪問,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08Redis?哈希Hash底層數(shù)據(jù)結(jié)構(gòu)詳解
這篇文章主要介紹了Redis?哈希Hash底層數(shù)據(jù)結(jié)構(gòu)詳解的相關(guān)資料,需要的朋友可以參考下2022-08-08詳解Redis中地理位置功能Geospatial的應(yīng)用
Geospatial?Indexes?是?Redis?提供的一種數(shù)據(jù)結(jié)構(gòu),用于存儲(chǔ)和查詢地理位置信息,這篇文章就來和大家詳細(xì)講講Geospatial的具體應(yīng)用吧2023-06-06