redis单机缓存和 ehcache 一样,如果在 classpath 下存在 redis 并且 redis 已经配置好了,此时默认就会使用 rediscachemanager 作为缓存提供者,redis 单机缓存使用步骤如下:
1. 创建项目添加缓存依赖创建 spring boot 项目,添加 spring-boot-starter-cache 和 redis 依赖
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-cache</artifactid></dependency><dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid></dependency><dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-redis</artifactid> <exclusions> <exclusion> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-data-redis</artifactid> </exclusion> </exclusions></dependency><dependency> <groupid>io.lettuce</groupid> <artifactid>lettuce-core</artifactid></dependency><dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope></dependency>
2. 缓存配置redis 单机缓存只需要开发者在 application.properties 中进行 redis 配置及缓存配置即可,代码如下
# 缓存配置
# 配置缓存名称,redis中的key都有一个前缀,默认前缀是“缓存名::”
spring.cache.cache-names=c1,c2
# 配置缓存有效期,即redis中的key过期时间
spring.cache.redis.time-to-live=1800s
# redis 配置
spring.redis.database=0
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=123456
spring.redis.jedis.pool.max-active=8
spring.redis.jedis.pool.max-idle=8
spring.redis.jedis.pool.max-wait=-1ms
spring.redis.jedis.pool.min-idle=0
3. 开启缓存在项目入口类中开启缓存,如下
@springbootapplication@enablecachingpublic class cacheapplication { public static void main(string[] args) { springapplication.run(cacheapplication.class, args); }}
第 4、5 步与springboot浅析缓存机制之ehcache 2.x应用一样,此处不再做过多的解释
4. 创建 bookdaobook
public class book implements serializable { private integer id; private string name; private string author; @override public string tostring() { return "book{" + "id=" + id + ", name='" + name + '\'' + ", author='" + author + '\'' + '}'; } public integer getid() { return id; } public void setid(integer id) { this.id = id; } public string getname() { return name; } public void setname(string name) { this.name = name; } public string getauthor() { return author; } public void setauthor(string author) { this.author = author; }}
bookdao
@repository@cacheconfig(cachenames = "book_cache")public class bookdao { @cacheable public book getbookbyid(integer id) { system.out.println("getbookbyid"); book book = new book(); book.setid(id); book.setname("三国演义"); book.setauthor("罗贯中"); return book; } @cacheput(key = "#book.id") public book updatebookbyid(book book) { system.out.println("updatebookbyid"); book.setname("三国演义2"); return book; } @cacheevict(key = "#id") public void deletebookbyid(integer id) { system.out.println("deletebookbyid"); }}
5. 创建测试类创建测试类,对 service 中的方法进行测试
@runwith(springrunner.class)@springboottestpublic class cacheapplicationtests { @autowired bookdao bookdao; @test public void contextloads() { bookdao.deletebookbyid(1); bookdao.getbookbyid(1); bookdao.getbookbyid(1); bookdao.deletebookbyid(1); book b3 = bookdao.getbookbyid(1); system.out.println("b3:"+b3); book b = new book(); b.setname("三国演义"); b.setauthor("罗贯中"); b.setid(1); bookdao.updatebookbyid(b); book b4 = bookdao.getbookbyid(1); system.out.println("b4:"+b4); }}
执行该方法,控制台打印日志如下:
deletebookbyid
getbookbyid
deletebookbyid
getbookbyid
b3:book{id=1, name='三国演义', author='罗贯中'}
updatebookbyid
b4:book{id=1, name='三国演义2', author='罗贯中'}
为了避免缓存对来回测试的影响,我们首先执行删除操作(这同时也会删除缓存)。然后执行了一次查询,正常打印,接着又执行了一次查询没打印(直接读取的缓存),然后执行删除,接着再执行查询正常打印(删除操作也删除了缓存),再接着执行更新操作(同时更新了缓存),最后再次查询,打印更新后的数据。
以上就是springboot缓存机制之redis单机缓存如何应用的详细内容。
