javascript
java+cache使用方法_java相关:Spring boot redis cache的key的使用方法
java相關:Spring boot redis cache的key的使用方法
發布于 2020-8-16|
復制鏈接
摘記: 在數據庫查詢中我們往往會使用增加緩存來提高程序的性能,@Cacheable 可以方便的對數據庫查詢方法加緩存。本文主要來探究一下緩存使用的key。搭建項目
數據庫
```sql
mysql> selec ..
在數據庫查詢中我們往往會使用增加緩存來提高程序的性能,@Cacheable 可以方便的對數據庫查詢方法加緩存。本文主要來探究一下緩存使用的key。搭建項目數據庫
```sql
mysql> select * from t_student;
+----+--------+-------------+
| id | name | grade_class |
+----+--------+-------------+
| 1 | Simone | 3-2 |
+----+--------+-------------+
1 row in set (0.01 sec)
```
spring boot 配置
```plain
#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
```
數據訪問層
```java
public interface StudentDao extends CrudRepository {
@Cacheable(value = "Cache")
@Override
Optional findById(Long aLong);
@Cacheable(value = "Cache")
Optional findByName(String name);
}
```
啟動調用數據訪問層方法觀察
```java
@Override
public void run(ApplicationArguments args) throws Exception {
Optional optional = studentDao.findById(1L);
System.out.println(optional);
}
```
當默認使用 @Cacheable(value = "Cache") 的時候查看 redis 中緩存的 key
```plain
127.0.0.1:6379> keys *
1) "Cache::1"
```
可以知道 key是由緩存的名字和參數值生成的,key 的生成和方法的名字無關,如果兩個方法的參數相同了,就會命中同一個緩存,這樣顯然是不行的。使用相同的參數調用 findById 和 findByName 觀察查詢結果
```java
@Override
public void run(ApplicationArguments args) throws Exception {
Optional optional = studentDao.findById(1L);
System.out.println(optional);
Optional optionalByName = studentDao.findByName("1");
System.out.println(optionalByName);
}
//輸出結果
//Optional[Student(id=1, name=Simone, gradeClass=3-2)]
//Optional[Student(id=1, name=Simone, gradeClass=3-2)]
```
從數據庫的數據看 studentDao.findByName("1") 應該是查詢出空的,但是取命中了緩存,所以我們需要給緩存的 key 加上方法的名字。
```java
@Cacheable(value = "Cache", key = "{#root.methodName, #aLong}")
@Override
Optional findById(Long aLong);
@Cacheable(value = "Cache", key = "{#root.methodName, #name}")
Optional findByName(String name);
//Optional[Student(id=1, name=Simone, gradeClass=3-2)]
//Optional.empty
```
Redis 中的 Key 也有了方法的名字
```plain
127.0.0.1:6379> keys *
1) "Cache::findById,1"
2) "Cache::findByName,1"
```
在實際項目中我們肯定不是只有一張表,如果其他表使用緩存的名字也是 Cache,很有可能產生相同的 key,比如我還有一個如下的 dao
```java
public interface TeacherDao extends CrudRepository {
@Cacheable(value = "Cache", key = "{#root.methodName, #aLong}")
@Override
Optional findById(Long aLong);
@Cacheable(value = "Cache", key = "{#root.methodName, #name}")
Optional findByName(String name);
}
```
如果在調用 TeacherDao 中的方法命中了 StudentDao 中的方法會產生 ClassCastException ,這里就兩種方式來解決這個問題。第一種辦法是每個 dao 中都使用不同的緩存名字。第二是給 key 加上類的名字。我 google 了一下,沒有找到使用 Spel 或取到類名的方法(或許有),所以這里通過配置 @Cacheable 的 key參數就不行了。那就只能實現自定義的生成器。
```java
@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, "_");
}
};
}
```
設置 @Cacheable 的 keyGenerator 參數
```java
@Cacheable(value = "Cache", keyGenerator = "customKeyGenerator")
@Override
Optional findById(Long aLong);
@Cacheable(value = "Cache", keyGenerator = "customKeyGenerator")
Optional findByName(String name);
```
查看 Redis 中的 key
```plain
127.0.0.1:6379> keys *
1) "Cache::me.action.dao.StudentDao_findById_1"
2) "Cache::me.action.dao.StudentDao_findByName_1"
```
Key 由緩存名、類名、方法名和參數構成,這樣足夠保險了。在實際開發中可以根據實際情況構造 key 滿足需求。
總結
以上是生活随笔為你收集整理的java+cache使用方法_java相关:Spring boot redis cache的key的使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java 中sun.net.ftp_开发
- 下一篇: 阿富汗现在还有联合国人员吗