SpringBoot淺析緩存機制之Redis單機緩存應用

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
@EnableCaching
public class CacheApplication {
    public static void main(String[] args) {
        SpringApplication.run(CacheApplication.class, args);
    }
}

第 4、5 步與SpringBoot淺析緩存機制之Ehcache 2.x應用一樣,此處不再做過多的解釋

4. 創建 BookDao

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");
    }
}

5. 創建測試類

創建測試類,對 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單機緩存應用的文章就介紹到這瞭,更多相關SpringBoot Redis單機緩存內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: