日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

springboot redis 刷新时间_「SpringBoot实战」SpringCache + Redis实现数据缓存

發布時間:2024/10/5 javascript 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot redis 刷新时间_「SpringBoot实战」SpringCache + Redis实现数据缓存 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

關注我的微信公眾號:后端技術漫談

不定期推送關于后端開發、爬蟲、算法題、數據結構方面的原創技術文章,以及生活中的逸聞趣事。

我目前是一名后端開發工程師。主要關注后端開發,數據安全,網絡爬蟲,物聯網,邊緣計算等方向。

原創博客主要內容

  • Java知識點復習全手冊
  • Leetcode算法題解析
  • 劍指offer算法題解析
  • SpringCloud菜鳥入門實戰系列
  • SpringBoot菜鳥入門實戰系列
  • Python爬蟲相關技術文章
  • 后端開發相關技術文章

前言

本文實現了SpringCache + Redis的集中式緩存,方便大家對學習了解緩存的使用。

本文實現:

  • SpringCache + Redis的組合
  • 通過配置文件實現了自定義key過期時間;key命名方式;value序列化方式

實現本文代碼的前提:

  • 已有一個可以運行的Springboot項目,實現了簡單的CRUD功能

步驟

在Spring Boot中通過@EnableCaching注解自動化配置合適的緩存管理器(CacheManager),Spring Boot根據下面的順序去偵測緩存提供者:

GenericJCache (JSR-107)EhCache 2.xHazelcastInfinispanRedisGuavaSimple

我們所需要做的就是實現一個將緩存數據放在Redis的緩存機制。

  • 添加pom.xml依賴
org.springframework.boot spring-boot-starter-cache org.springframework.boot spring-boot-starter-data-redis

注意:- spring-boot-starter-data-redis和spring-boot-starter-redis的區別:https://blog.csdn.net/weixin_38521381/article/details/79397292

可以看出兩個包并沒有區別,但是當springBoot的版本為1.4.7 以上的時候,spring-boot-starter-redis 就空了。要想引入redis就只能選擇有data的。

  • application.properties中加入redis連接設置(其它詳細設置請查看參考網頁)
# Redisspring.redis.host=localhostspring.redis.port=6379spring.redis.pool.max-idle=8spring.redis.pool.min-idle=0spring.redis.pool.max-active=8spring.redis.pool.max-wait=-1spring.redis.database=0spring.redis.password=xxx
  • 新增KeyGeneratorCacheConfig.java(或者名為CacheConfig)文件

該文件完成三項設置:key過期時間;key命名方式;value序列化方式:JSON便于查看

package com.pricemonitor.pm_backend;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.CacheManager;import org.springframework.cache.annotation.CachingConfigurerSupport;import org.springframework.cache.interceptor.KeyGenerator;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.cache.RedisCacheManager;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;import java.lang.reflect.Method;@Configurationpublic class KeyGeneratorCacheConfig extends CachingConfigurerSupport { private final RedisTemplate redisTemplate; @Autowired public KeyGeneratorCacheConfig(RedisTemplate redisTemplate) { this.redisTemplate = redisTemplate; } @Override public CacheManager cacheManager() { // 設置key的序列化方式為String redisTemplate.setKeySerializer(new StringRedisSerializer()); // 設置value的序列化方式為JSON GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(); redisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer); RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); // 設置默認過期時間為600秒 cacheManager.setDefaultExpiration(600); return cacheManager; } /** * key值為className+methodName+參數值列表 * @return */ @Override public KeyGenerator keyGenerator() { return new KeyGenerator() { @Override public Object generate(Object o, Method method, Object... args) { StringBuilder sb = new StringBuilder(); sb.append(o.getClass().getName()).append("#"); sb.append(method.getName()).append("("); for (Object obj : args) { if(obj != null) { // 在可選參數未給出時時,會出現null,此時需要跳過 sb.append(obj.toString()).append(","); } } sb.append(")"); return sb.toString(); } }; }}
  • 在serviceImpl中加入@CacheConfig并且給給每個方法加入緩存(詳細注解使用請查看參考網頁)
@Service@CacheConfig(cacheNames = "constant")public class ConstantServiceImpl implements ConstantService { @Autowired private ConstantMapper constantMapper; @Cacheable @Override public List alertMessage() { ConstantExample constantExample = new ConstantExample(); ConstantExample.Criteria criteria = constantExample.createCriteria(); criteria.andTypeEqualTo("alert"); return constantMapper.selectByExample(constantExample); } @Cacheable @Override public List noteMessage() { ConstantExample constantExample = new ConstantExample(); ConstantExample.Criteria criteria = constantExample.createCriteria(); criteria.andTypeEqualTo("note"); return constantMapper.selectByExample(constantExample); } @Cacheable @Override public List banner() { ConstantExample constantExample = new ConstantExample(); ConstantExample.Criteria criteria = constantExample.createCriteria(); criteria.andTypeEqualTo("banner"); return constantMapper.selectByExample(constantExample); }}

效果圖

image

注意事項

  • 若直接修改數據庫的表,并沒有提供接口修改的字段,緩存就沒法更新。所以這種字段加緩存需要尤其注意緩存的有效性,最好讓其及時過期?;蛘呓o其實現增刪改接口。
  • 大坑:在可選參數未給出時時,會出現null,此時在生成Key名的時候需要跳過。已在代碼中修改
for (Object obj : args) { if(obj != null) { // 在可選參數未給出時時,會出現null,此時需要跳過 sb.append(obj.toString()).append(","); }}

參考

緩存入門:http://blog.didispace.com/springbootcache1/

Redis集中式緩存:http://blog.didispace.com/springbootcache2/

代碼實現:(經典好用,有小bug):https://zhuanlan.zhihu.com/p/30540686

代碼實現(可參考):https://www.jianshu.com/p/6ba2d2dbf36e

總結

以上是生活随笔為你收集整理的springboot redis 刷新时间_「SpringBoot实战」SpringCache + Redis实现数据缓存的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。