和Ehcache 一樣,如果在classpath 下存在Redis 並且Redis 已經配置好了,此時預設就會使用RedisCacheManager 作為快取提供者,Redis 單機快取使用步驟如下:
建立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>
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
在專案入口類別中開啟緩存,如下
@SpringBootApplication @EnableCaching public class CacheApplication { public static void main(String[] args) { SpringApplication.run(CacheApplication.class, args); } }
第4、5 步驟與SpringBoot淺析快取機制之Ehcache 2.x應用一樣,此處不再做過多的解釋
Book
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"); } }
建立測試類,對Service 中的方法進行測試
@RunWith(SpringRunner.class) @SpringBootTest public 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單機快取如何應用的詳細內容。更多資訊請關注PHP中文網其他相關文章!