Springboot使用Redis中ZSetOperations實(shí)現(xiàn)博客訪問量
1.在application.yml中Redis配置信息
spring: redis: host: 127.0.0.1 port: 6379 password: 12345678
2.在pom.xml中加載依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
3.配置Redis Template
@Configuration public class RedisConfig { @Bean("redisTemplate") public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) { // 1.創(chuàng)建RedisTemplate對(duì)象 RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>(); // 2.加載Redis配置 redisTemplate.setConnectionFactory(lettuceConnectionFactory); // 3.配置key序列化 RedisSerializer<?> stringRedisSerializer = new StringRedisSerializer(); redisTemplate.setKeySerializer(stringRedisSerializer); redisTemplate.setHashKeySerializer(stringRedisSerializer); // 4.配置Value序列化 Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class); ObjectMapper objMapper = new ObjectMapper(); objMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objMapper.activateDefaultTyping(objMapper.getPolymorphicTypeValidator(), ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objMapper); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer); // 5.初始化RedisTemplate redisTemplate.afterPropertiesSet(); return redisTemplate; } @Bean public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForZSet(); } }
4.操作說明
在Spring環(huán)境中,可以@Autowired自動(dòng)注入方式注入操作Redis對(duì)象。比如:RedisTemplate、ZSetOperations。
@Autowired private RedisTemplate redisTemplate; @Autowired private ZSetOperations zSetOperations;
更新瀏覽量(不用去更新到mysql了)
//這個(gè)完全采用redis設(shè)計(jì)就可以 不用查詢數(shù)據(jù)庫 把閱讀量完全加入到緩存中 public void updateReadCount(Integer blogId) { zSetOperations.incrementScore(BLOG_KEY, blogId, 1); //blogMapper.updateReadCount(blogId); 不用在mysql數(shù)據(jù)庫更新了 }
取出榜單TopK(效率提高不用在數(shù)據(jù)庫中查出所有的然后取前20)
public List<Blog> selectTop() { List<Blog> blogList = new ArrayList<>(); Long size = zSetOperations.size(BLOG_KEY); Set<Object> zset; if(size > TopN){ zset = zSetOperations.reverseRange(BLOG_KEY, 0, TopN - 1); }else{ zset = zSetOperations.reverseRange(BLOG_KEY, 0, - 1); } for(Object x:zset){ blogList.add(blogMapper.selectById((int)x)); } return blogList; }
顯示瀏覽量(直接設(shè)定到mysql屬性就好)
Double score = zSetOperations.score(BLOG_KEY, blog.getId()); int readCount = (score != null) ? score.intValue() : 0; blog.setReadCount(readCount);
總結(jié)
如果要效率提高,必須得犧牲空間,本操作還可以進(jìn)一步優(yōu)化!
比如把blog全部存到Redis,再找前TopK時(shí)候就不用去根據(jù)blog ID去mysql數(shù)據(jù)庫搜索,直接可以輸出了
但是如果博客過多的話 占用Redis內(nèi)存太大
到此這篇關(guān)于Springboot 使用Redis中ZSetOperations實(shí)現(xiàn)博客訪問量的文章就介紹到這了,更多相關(guān)Springboot 博客訪問量?jī)?nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot application無法使用$獲取pom變量的問題及解決
這篇文章主要介紹了springboot application無法使用$獲取pom變量的問題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02Java中如何編寫一個(gè)數(shù)的n次方(冪運(yùn)算)?
本文介紹了使用pow函數(shù)和自定義for循環(huán)計(jì)算冪的O(n)時(shí)間復(fù)雜度方法,然后重點(diǎn)講解了快速冪算法的分治思想,以及從二進(jìn)制角度的解釋,包括如何通過位運(yùn)算和循環(huán)迭代實(shí)現(xiàn)高效計(jì)算,給出了Java代碼實(shí)現(xiàn)2024-07-07實(shí)例分析Java中public static void main(String args[])是什么意思
這篇文章主要介紹了實(shí)例分析Java中public static void main(String args[])的意義,詳細(xì)分析了Java主函數(shù)main關(guān)鍵字聲明的具體含義和用法,需要的朋友可以參考下2015-12-12SpringBoot配置主從數(shù)據(jù)庫實(shí)現(xiàn)讀寫分離
現(xiàn)在的 Web 應(yīng)用大都是讀多寫少,本文主要介紹了SpringBoot配置主從數(shù)據(jù)庫實(shí)現(xiàn)讀寫分離,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11Java?SpringMVC實(shí)現(xiàn)自定義攔截器
這篇文章主要為大家詳細(xì)介紹了SpringMVC實(shí)現(xiàn)自定義攔截器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03SpringCloud Gateway 路由配置定位原理分析
本節(jié)主要了解系統(tǒng)中的謂詞與配置的路由信息是如何進(jìn)行初始化關(guān)聯(lián)生成路由對(duì)象的。每個(gè)謂詞工廠中的Config對(duì)象又是如何被解析配置的2021-07-07SpringBoot停止啟動(dòng)時(shí)測(cè)試檢查rabbitmq操作
這篇文章主要介紹了SpringBoot停止啟動(dòng)時(shí)測(cè)試檢查rabbitmq操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09