欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2022年08月13日 09:31:33   作者:一只小熊貓呀  
在上文中我介紹了Spring Boot使用EhCache 2.x來作為緩存的實現(xiàn),本文接著介紹使用單機版的Redis作為緩存的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

Redis單機緩存

和 Ehcache 一樣,如果在 classpath 下存在 Redis 并且 Redis 已經(jīng)配置好了,此時默認就會使用 RedisCacheManager 作為緩存提供者,Redis 單機緩存使用步驟如下:

1. 創(chuàng)建項目添加緩存依賴

創(chuàng)建 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 單機緩存只需要開發(fā)者在 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. 創(chuàng)建 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. 創(chuàng)建測試類

創(chuàng)建測試類,對 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);
    }
}

執(zhí)行該方法,控制臺打印日志如下:

deleteBookById
getBookById
deleteBookById
getBookById
b3:Book{id=1, name='三國演義', author='羅貫中'}
updateBookById
b4:Book{id=1, name='三國演義2', author='羅貫中'}

為了防止來回測試緩存的影響,這里先執(zhí)行刪除操作(同時也會刪除緩存)。然后執(zhí)行了一次查詢,正常打印,接著又執(zhí)行了一次查詢沒打?。ㄖ苯幼x取的緩存),然后執(zhí)行刪除,接著再執(zhí)行查詢正常打印(刪除操作也刪除了緩存),再接著執(zhí)行更新操作(同時更新了緩存),最后再次查詢,打印更新后的數(shù)據(jù)。

到此這篇關(guān)于SpringBoot淺析緩存機制之Redis單機緩存應用的文章就介紹到這了,更多相關(guān)SpringBoot Redis單機緩存內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論