Springboot使用Redis中ZSetOperations實現(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對象
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自動注入方式注入操作Redis對象。比如:RedisTemplate、ZSetOperations。
@Autowired private RedisTemplate redisTemplate; @Autowired private ZSetOperations zSetOperations;
更新瀏覽量(不用去更新到mysql了)
//這個完全采用redis設計就可以 不用查詢數(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;
}
顯示瀏覽量(直接設定到mysql屬性就好)
Double score = zSetOperations.score(BLOG_KEY, blog.getId());
int readCount = (score != null) ? score.intValue() : 0;
blog.setReadCount(readCount);
總結
如果要效率提高,必須得犧牲空間,本操作還可以進一步優(yōu)化!
比如把blog全部存到Redis,再找前TopK時候就不用去根據(jù)blog ID去mysql數(shù)據(jù)庫搜索,直接可以輸出了
但是如果博客過多的話 占用Redis內(nèi)存太大
到此這篇關于Springboot 使用Redis中ZSetOperations實現(xiàn)博客訪問量的文章就介紹到這了,更多相關Springboot 博客訪問量內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot application無法使用$獲取pom變量的問題及解決
這篇文章主要介紹了springboot application無法使用$獲取pom變量的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
實例分析Java中public static void main(String args[])是什么意思
這篇文章主要介紹了實例分析Java中public static void main(String args[])的意義,詳細分析了Java主函數(shù)main關鍵字聲明的具體含義和用法,需要的朋友可以參考下2015-12-12
SpringBoot配置主從數(shù)據(jù)庫實現(xiàn)讀寫分離
現(xiàn)在的 Web 應用大都是讀多寫少,本文主要介紹了SpringBoot配置主從數(shù)據(jù)庫實現(xiàn)讀寫分離,具有一定的參考價值,感興趣的可以了解一下2023-11-11
SpringCloud Gateway 路由配置定位原理分析
本節(jié)主要了解系統(tǒng)中的謂詞與配置的路由信息是如何進行初始化關聯(lián)生成路由對象的。每個謂詞工廠中的Config對象又是如何被解析配置的2021-07-07

