Spring boot redis cache的key的使用方法
在數(shù)據(jù)庫(kù)查詢中我們往往會(huì)使用增加緩存來(lái)提高程序的性能,@Cacheable 可以方便的對(duì)數(shù)據(jù)庫(kù)查詢方法加緩存。本文主要來(lái)探究一下緩存使用的key。
搭建項(xiàng)目
數(shù)據(jù)庫(kù)
mysql> select * from t_student; +----+--------+-------------+ | id | name | grade_class | +----+--------+-------------+ | 1 | Simone | 3-2 | +----+--------+-------------+ 1 row in set (0.01 sec)
spring boot 配置
#jpa spring.jpa.hibernate.ddl-auto=none spring.datasource.url=jdbc:mysql://127.0.0.1:3306/pratice spring.datasource.username=root spring.datasource.password=123456 #redis spring.redis.host=localhost spring.redis.lettuce.pool.maxActive=5 spring.redis.lettuce.pool.maxIdle=5 #cache spring.cache.cache-names=Cache spring.cache.redis.time-to-live=300000
數(shù)據(jù)訪問(wèn)層
public interface StudentDao extends CrudRepository<Student, Long> { @Cacheable(value = "Cache") @Override Optional<Student> findById(Long aLong); @Cacheable(value = "Cache") Optional<Student> findByName(String name); }
啟動(dòng)調(diào)用數(shù)據(jù)訪問(wèn)層方法觀察
@Override public void run(ApplicationArguments args) throws Exception { Optional<Student> optional = studentDao.findById(1L); System.out.println(optional); }
當(dāng)默認(rèn)使用 @Cacheable(value = "Cache") 的時(shí)候查看 redis 中緩存的 key
127.0.0.1:6379> keys * 1) "Cache::1"
可以知道 key是由緩存的名字和參數(shù)值生成的,key 的生成和方法的名字無(wú)關(guān),如果兩個(gè)方法的參數(shù)相同了,就會(huì)命中同一個(gè)緩存,這樣顯然是不行的。使用相同的參數(shù)調(diào)用 findById 和 findByName 觀察查詢結(jié)果
@Override public void run(ApplicationArguments args) throws Exception { Optional<Student> optional = studentDao.findById(1L); System.out.println(optional); Optional<Student> optionalByName = studentDao.findByName("1"); System.out.println(optionalByName); } //輸出結(jié)果 //Optional[Student(id=1, name=Simone, gradeClass=3-2)] //Optional[Student(id=1, name=Simone, gradeClass=3-2)]
從數(shù)據(jù)庫(kù)的數(shù)據(jù)看 studentDao.findByName("1") 應(yīng)該是查詢出空的,但是取命中了緩存,所以我們需要給緩存的 key 加上方法的名字。
@Cacheable(value = "Cache", key = "{#root.methodName, #aLong}") @Override Optional<Student> findById(Long aLong); @Cacheable(value = "Cache", key = "{#root.methodName, #name}") Optional<Student> findByName(String name); //Optional[Student(id=1, name=Simone, gradeClass=3-2)] //Optional.empty
Redis 中的 Key 也有了方法的名字
127.0.0.1:6379> keys * 1) "Cache::findById,1" 2) "Cache::findByName,1"
在實(shí)際項(xiàng)目中我們肯定不是只有一張表,如果其他表使用緩存的名字也是 Cache,很有可能產(chǎn)生相同的 key,比如我還有一個(gè)如下的 dao
public interface TeacherDao extends CrudRepository<Teacher, Long> { @Cacheable(value = "Cache", key = "{#root.methodName, #aLong}") @Override Optional<Teacher> findById(Long aLong); @Cacheable(value = "Cache", key = "{#root.methodName, #name}") Optional<Teacher> findByName(String name); }
如果在調(diào)用 TeacherDao 中的方法命中了 StudentDao 中的方法會(huì)產(chǎn)生 ClassCastException ,這里就兩種方式來(lái)解決這個(gè)問(wèn)題。第一種辦法是每個(gè) dao 中都使用不同的緩存名字。第二是給 key 加上類的名字。
我 google 了一下,沒(méi)有找到使用 Spel 或取到類名的方法(或許有),所以這里通過(guò)配置 @Cacheable 的 key參數(shù)就不行了。那就只能實(shí)現(xiàn)自定義的生成器。
@Bean("customKeyGenerator") public KeyGenerator keyGenerator() { return new KeyGenerator() { @Override public Object generate(Object o, Method method, Object... objects) { return method.getDeclaringClass().getName() + "_" + method.getName() + "_" + StringUtils.arrayToDelimitedString(objects, "_"); } }; }
設(shè)置 @Cacheable 的 keyGenerator 參數(shù)
@Cacheable(value = "Cache", keyGenerator = "customKeyGenerator") @Override Optional<Student> findById(Long aLong); @Cacheable(value = "Cache", keyGenerator = "customKeyGenerator") Optional<Student> findByName(String name);
查看 Redis 中的 key
127.0.0.1:6379> keys * 1) "Cache::me.action.dao.StudentDao_findById_1" 2) "Cache::me.action.dao.StudentDao_findByName_1"
Key 由緩存名、類名、方法名和參數(shù)構(gòu)成,這樣足夠保險(xiǎn)了。在實(shí)際開發(fā)中可以根據(jù)實(shí)際情況構(gòu)造 key 滿足需求。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot實(shí)現(xiàn)EMQ設(shè)備的上下線告警
EMQX?的上下線系統(tǒng)消息通知功能在客戶端連接成功或者客戶端斷開連接,需要實(shí)現(xiàn)設(shè)備的上下線狀態(tài)監(jiān)控,所以本文給大家介紹了如何通過(guò)SpringBoot實(shí)現(xiàn)EMQ設(shè)備的上下線告警,文中有詳細(xì)的代碼示例,需要的朋友可以參考下2023-10-10mybatis自定義類型處理器TypehHandler示例詳解
我們?cè)趯憁apper映射器的配置文件時(shí),不經(jīng)意間已經(jīng)用到類型轉(zhuǎn)換,不過(guò)是mybatis幫我們完成的,下面這篇文章主要給大家介紹了關(guān)于mybatis自定義類型處理器TypehHandler的相關(guān)資料,需要的朋友可以參考下2018-09-09基于SpringBoot?使用?Flink?收發(fā)Kafka消息的示例詳解
這篇文章主要介紹了基于SpringBoot?使用?Flink?收發(fā)Kafka消息,本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-01-01關(guān)于logback.xml和logback-spring.xml的區(qū)別及說(shuō)明
這篇文章主要介紹了關(guān)于logback.xml和logback-spring.xml的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06java:try...catch跳過(guò)異常繼續(xù)處理循環(huán)問(wèn)題
這篇文章主要介紹了java:try...catch跳過(guò)異常繼續(xù)處理循環(huán)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10SpringBoot+Redis實(shí)現(xiàn)查找附近用戶的示例代碼
SpringDataRedis提供了十分簡(jiǎn)單的地理位置定位的功能,本文主要介紹了SpringBoot+Redis實(shí)現(xiàn)查找附近用戶的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02springboot加載命令行參數(shù)ApplicationArguments的實(shí)現(xiàn)
本文主要介紹了springboot加載命令行參數(shù)ApplicationArguments的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04高級(jí)數(shù)據(jù)結(jié)構(gòu)及應(yīng)用之使用bitmap進(jìn)行字符串去重的方法實(shí)例
今天小編就為大家分享一篇關(guān)于高級(jí)數(shù)據(jù)結(jié)構(gòu)及應(yīng)用之使用bitmap進(jìn)行字符串去重的方法實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02