
1、redis单机缓存
1、redis单机缓存
Redis 单机缓存使用步骤如下:
创建项目,添加缓存依赖?
创建 Spring Boot 项目,添加 spring-boot-starter-cache 和 Redis 依赖,代码如下:
<!--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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!--redis单机缓存end-->
缓存配置?
Red is 单机缓存只需要开发者在 application.properties 中进行 Redis 配置及缓存配置即可,代码:
#缓存配置
spring.cache.cache-names=c1,c2
spring.cache.redis.time-to-live=1800s
#Redis配置
spring.redis.database=0
spring.redis.host=127.0.0.1
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
开启缓存?
接下来在项 目入口类中开启缓存,代码如下:
package controller;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
@SpringBootApplication
@EnableCaching
public class RediscacheApplication {
public static void main(String[] args) {
SpringApplication.run(RediscacheApplication.class, args);
}
}
创建 BookDao ?
创建 Book 实体类和 BookService
package Dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.*;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.stereotype.Service;
import javax.crypto.KeyGenerator;
import Bean.Book;
@Service
@CacheConfig(cacheNames = "book_cache")
public class BookDao {
@Autowired
// MyKeyGenerator myKeyGenerator;
// @Cacheable(keyGenerator = "myKeyGenerator")
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");
}
}
package Bean;
import java.io.Serializable;
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;
}
}
创建测试类?
创建测试类, 对 Service 中的方法进行测试,代码如下:
package test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import Dao.BookDao;
import Bean.Book;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CacheApplicationTests {
@Autowired
BookDao bookDao;
@Test
public void contextLoads() {
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);
}
}

一开始执行了两个查询,但是查询方法只打印了一次,因为第二次使用了缓存。接下来执行 了删除方法,删除方法执行完之后再次执行查询, 查询方法又被执行了,因为在删除方法中缓存己 经被删除了 。再接下来执行更新方法,更新方法中不仅更新数据,也更新了缓存,所以在最后的查 询方法中, 查询方法日志没打印,说明该方法没执行,而是使用了缓存中的数据,而缓存中的数据 已经被更新了 。