javascript
java+cache使用方法_java相关:Spring boot redis cache的key的使用方法
java相關(guān):Spring boot redis cache的key的使用方法
發(fā)布于 2020-8-16|
復(fù)制鏈接
摘記: 在數(shù)據(jù)庫(kù)查詢中我們往往會(huì)使用增加緩存來(lái)提高程序的性能,@Cacheable 可以方便的對(duì)數(shù)據(jù)庫(kù)查詢方法加緩存。本文主要來(lái)探究一下緩存使用的key。搭建項(xiàng)目
數(shù)據(jù)庫(kù)
```sql
mysql> selec ..
在數(shù)據(jù)庫(kù)查詢中我們往往會(huì)使用增加緩存來(lái)提高程序的性能,@Cacheable 可以方便的對(duì)數(shù)據(jù)庫(kù)查詢方法加緩存。本文主要來(lái)探究一下緩存使用的key。搭建項(xiàng)目數(shù)據(jù)庫(kù)
```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
```
數(shù)據(jù)訪問(wèn)層
```java
public interface StudentDao extends CrudRepository {
@Cacheable(value = "Cache")
@Override
Optional findById(Long aLong);
@Cacheable(value = "Cache")
Optional findByName(String name);
}
```
啟動(dòng)調(diào)用數(shù)據(jù)訪問(wèn)層方法觀察
```java
@Override
public void run(ApplicationArguments args) throws Exception {
Optional optional = studentDao.findById(1L);
System.out.println(optional);
}
```
當(dāng)默認(rèn)使用 @Cacheable(value = "Cache") 的時(shí)候查看 redis 中緩存的 key
```plain
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é)果
```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);
}
//輸出結(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 加上方法的名字。
```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"
```
在實(shí)際項(xiàng)目中我們肯定不是只有一張表,如果其他表使用緩存的名字也是 Cache,很有可能產(chǎn)生相同的 key,比如我還有一個(gè)如下的 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);
}
```
如果在調(diào)用 TeacherDao 中的方法命中了 StudentDao 中的方法會(huì)產(chǎn)生 ClassCastException ,這里就兩種方式來(lái)解決這個(gè)問(wèn)題。第一種辦法是每個(gè) dao 中都使用不同的緩存名字。第二是給 key 加上類(lèi)的名字。我 google 了一下,沒(méi)有找到使用 Spel 或取到類(lèi)名的方法(或許有),所以這里通過(guò)配置 @Cacheable 的 key參數(shù)就不行了。那就只能實(shí)現(xiàn)自定義的生成器。
```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, "_");
}
};
}
```
設(shè)置 @Cacheable 的 keyGenerator 參數(shù)
```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 由緩存名、類(lèi)名、方法名和參數(shù)構(gòu)成,這樣足夠保險(xiǎn)了。在實(shí)際開(kāi)發(fā)中可以根據(jù)實(shí)際情況構(gòu)造 key 滿足需求。
總結(jié)
以上是生活随笔為你收集整理的java+cache使用方法_java相关:Spring boot redis cache的key的使用方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java 中sun.net.ftp_开发
- 下一篇: 阿富汗现在还有联合国人员吗