当前位置: 移动技术网 > IT编程>开发语言>Java > Spring boot redis cache的key的使用方法

Spring boot redis cache的key的使用方法

2019年07月19日  | 移动技术网IT编程  | 我要评论

在数据库查询中我们往往会使用增加缓存来提高程序的性能,@cacheable 可以方便的对数据库查询方法加缓存。本文主要来探究一下缓存使用的key。

搭建项目

数据库

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

数据访问层

public interface studentdao extends crudrepository<student, long> {

  @cacheable(value = "cache")
  @override
  optional<student> findbyid(long along);

  @cacheable(value = "cache")
  optional<student> findbyname(string name);
}

启动调用数据访问层方法观察

@override
public void run(applicationarguments args) throws exception {
 optional<student> optional = studentdao.findbyid(1l);
 system.out.println(optional);
}

当默认使用 @cacheable(value = "cache") 的时候查看 redis 中缓存的 key

127.0.0.1:6379> keys *
1) "cache::1"

可以知道 key是由缓存的名字和参数值生成的,key 的生成和方法的名字无关,如果两个方法的参数相同了,就会命中同一个缓存,这样显然是不行的。使用相同的参数调用 findbyid 和 findbyname 观察查询结果

@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);
}
//输出结果
//optional[student(id=1, name=simone, gradeclass=3-2)]
//optional[student(id=1, name=simone, gradeclass=3-2)]

从数据库的数据看 studentdao.findbyname("1") 应该是查询出空的,但是取命中了缓存,所以我们需要给缓存的 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"

在实际项目中我们肯定不是只有一张表,如果其他表使用缓存的名字也是 cache,很有可能产生相同的 key,比如我还有一个如下的 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);
}

如果在调用 teacherdao 中的方法命中了 studentdao 中的方法会产生 classcastexception ,这里就两种方式来解决这个问题。第一种办法是每个 dao 中都使用不同的缓存名字。第二是给 key 加上类的名字。

我 google 了一下,没有找到使用 spel 或取到类名的方法(或许有),所以这里通过配置 @cacheable 的 key参数就不行了。那就只能实现自定义的生成器。

@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 参数

@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 由缓存名、类名、方法名和参数构成,这样足够保险了。在实际开发中可以根据实际情况构造 key 满足需求。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网