【開源項目系列】如何基於 Spring Cache 實現多級緩存(同時整合本地緩存 Ehcache 和分佈式緩存 Redis)

一、緩存

當系統的併發量上來了,如果我們頻繁地去訪問數據庫,那麼會使數據庫的壓力不斷增大,在高峯時甚至可以出現數據庫崩潰的現象。所以一般我們會使用緩存來解決這個數據庫併發訪問問題,用戶訪問進來,會先從緩存裏查詢,如果存在則返回,如果不存在再從數據庫裏查詢,最後添加到緩存裏,然後返回給用戶,當然了,接下來又能使用緩存來提供查詢功能。

而緩存,一般我們可以分爲本地緩存和分佈式緩存。

常用的本地緩存有 ehcache、guava cache,而我們一般都是使用 ehcache,畢竟他是純 Java 的,出現問題我們還可以根據源碼解決,並且還能自己進行二次開發來擴展功能。

常用的分佈式緩存當然就是 Redis 了,Redis 是基於內存和單線程的,執行效率非常的高。

二、Spring Cache

相信如果要整合緩存到項目中,大家都會使用到 Spring Cache,它不但整合了多種緩存框架(ehcache、jcache等等),還可以基於註解來使用,是相當的方便。

緩存框架的整合在 spring-context-support 中:
在這裏插入圖片描述
緩存註解在 spring-context 中:
在這裏插入圖片描述
當然了,在 Spring 的 context 中沒有整合 Redis,但是我們可以在 spring-data-redis 中找到。
在這裏插入圖片描述
但是我們都知道,不管是在 Spring 項目 還是 Spring Boot 中,我們都只能整合一種緩存,不能同時整合多種緩存。

在 Spring Boot 中,我們一般是利用 spring.cache.type 來指定使用哪種緩存,然後填寫相關配置信息來完成自動配置。
在這裏插入圖片描述
CacheType 的源碼:我們可以看到,Spring 是支持非常多種緩存框架的。

package org.springframework.boot.autoconfigure.cache;

public enum CacheType {
    GENERIC,
    JCACHE,
    EHCACHE,
    HAZELCAST,
    INFINISPAN,
    COUCHBASE,
    REDIS,
    CAFFEINE,
    SIMPLE,
    NONE;

    private CacheType() {
    }
}

那麼如果我們就是有這麼一種需求,要整合兩種緩存框架:例如一個本地緩存 Ehcache,一個分佈式緩存 Redis,

那能整麼?

能是能,但是 Spring 可不提供這種多級緩存,而是需要你自己動手來整了。

三、h2cache-spring-boot-starter

1、什麼是 h2cache-spring-boot-starter?

在微服務中,每個服務都是無狀態的,服務之間需要經過 HTTP 或者 RPC 來進行通信。而每個服務都擁有自己對應的數據庫,所以說如果服務A 需要獲取服務B 的某個表的數據,那麼就需要一次 HTTP 或 RPC 通信,那如果高峯期每秒需要調用100次,那豈不是需要100次 HTTP 或 RPC 通信,這是相當耗費接口性能的。

那怎麼解決呢?

本地緩存那是肯定不是的,因爲一般不同服務都是部署在不同的機器上面的,所以此時我們需要的是分佈式緩存,例如 Redis;但是,訪問量高的的服務當然還是需要本地緩存了。所以最後,我們不但需要本地緩存,還需要分佈式緩存,但是 Spring Boot 卻不能提供這種多級緩存的功能,所以需要我們自己來整合。

不用怕,我已經自己整了一個 Spring Boot Starter了,就是 h2cache-spring-boot-starter,我們只需要在配置文件配置上對應的信息,就可以啓用這個多級緩存的功能了。

2、開始使用

添加依賴:

大家正常引入下面依賴即可,因爲我已經將此項目發佈到 Maven 中央倉庫了~

<denpency>
    <groupId>com.github.howinfun</groupId>
    <artifactId>h2cache-spring-boot-starter</artifactId>
    <version>0.0.1</version>
</denpency>

在 Spring Boot properties 啓用服務,並且加上對應的配置:

開啓多級緩存服務:

# Enable L2 cache or not
h2cache.enabled=true

配置 Ehcache:

# Ehcache Config
## the path of ehcache.xml (We can put it directly under Resources) 
h2cache.ehcache.filePath=ehcache.xml
#Set whether the EhCache CacheManager should be shared (as a singleton at the ClassLoader level) or independent (typically local within the application).Default is "false", creating an independent local instance.
h2cache.ehcache.shared=true

配置 Redis:主要包括默認的緩存配置和自定義緩存配置

要注意一點的是:h2cache-spring-boot-starter 同時引入了 LettuceJedis 客戶端,而 Spring Boot 默認使用 Lettuce 客戶端,所以如果我們需要使用 Jedis 客戶端,需要將 Lettuce 依賴去除掉。

# Redis Config
## default Config (expire)
h2cache.redis.default-config.ttl=200
### Disable caching {@literal null} values.Default is "false"
h2cache.redis.default-config.disable-null-values=true
### Disable using cache key prefixes.Default is "true"
h2cache.redis.default-config.use-prefix=true

## Custom Config list
### cacheName -> @CacheConfig#cacheNames @Cacheable#cacheNames and other comments, etc   
h2cache.redis.config-list[0].cache-name=userCache
h2cache.redis.config-list[0].ttl=60
h2cache.redis.config-list[0].use-prefix=true
h2cache.redis.config-list[0].disable-null-values=true

h2cache.redis.config-list[1].cache-name=bookCache
h2cache.redis.config-list[1].ttl=60
h2cache.redis.config-list[1].use-prefix=true

#Redis
spring.redis.host=10.111.0.111
spring.redis.password=
spring.redis.port=6379
spring.redis.database=15
# 連接池最大連接數(使用負值表示沒有限制)
spring.redis.jedis.pool.max-active=8
# 連接池中的最小空閒連接
spring.redis.jedis.pool.min-idle=0
# 連接池中的最大空閒連接
spring.redis.jedis.pool.max-idle=8
# 連接池最大阻塞等待時間(使用負值表示沒有限制)
spring.redis.jedis.pool.max-wait=30

如何使用緩存註解

我們只要像之前一樣使用 Spring Cache 的註解即可。

for example:

代碼裏的持久層,我使用的是: mybatis-plus.

package com.hyf.testDemo.redis;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Repository;

/**
 * @author Howinfun
 * @desc
 * @date 2020/3/25
 */
@Repository
// Global cache config,We usually set the cacheName               
@CacheConfig(cacheNames = {"userCache"})
public interface UserMapper extends BaseMapper<User> {

    /**
    * put the data to cache(Ehcache & Redis)
    * @param id
    * @return 
    */
    @Cacheable(key = "#id",unless = "#result == null")
    User selectById(Long id);

    /**
    * put the data to cache After method execution
    * @param user
    * @return 
    */
    @CachePut(key = "#user.id", condition = "#user.name != null and #user.name != ''")
    default User insert0(User user) {
        
        this.insert(user);
        return user;
    }

    /**
    * evict the data from cache
    * @param id
    * @return 
    */
    @CacheEvict(key = "#id")
    int deleteById(Long id);

    /**
    * Using cache annotations in combination
    * @param user
    * @return 
    */
    @Caching(
            evict = {@CacheEvict(key = "#user.id", beforeInvocation = true)},
            put = {@CachePut(key = "#user.id")}
    )
    default User updateUser0(User user){
        
        this.updateById(user);
        return user;
    }
}

測試一下:

查詢:我們可以看到,在數據庫查詢到結果後,會將數據添加到 EhcacheRedis 緩存中;接着之後的查詢都將會先從 Ehcache 或者 Redis 裏查詢。

2020-04-03 09:55:09.691  INFO 5920 --- [nio-8080-exec-7] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-04-03 09:55:10.044  INFO 5920 --- [nio-8080-exec-7] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2020-04-03 09:55:10.051 DEBUG 5920 --- [nio-8080-exec-7] c.h.t.redis.BookMapper2.selectById       : ==>  Preparing: SELECT id,create_time,update_time,read_frequency,version,book_name FROM book WHERE id=? 
2020-04-03 09:55:10.068 DEBUG 5920 --- [nio-8080-exec-7] c.h.t.redis.BookMapper2.selectById       : ==> Parameters: 51(Long)
2020-04-03 09:55:10.107 DEBUG 5920 --- [nio-8080-exec-7] c.h.t.redis.BookMapper2.selectById       : <==      Total: 1
2020-04-03 09:55:10.113  INFO 5920 --- [nio-8080-exec-7] c.hyf.cache.cachetemplate.H2CacheCache   : insert into ehcache,key:51,value:Book2(id=51, bookName=微服務架構, readFrequency=1, createTime=2020-03-20T16:10:13, updateTime=2020-03-27T09:14:44, version=1)
2020-04-03 09:55:10.118  INFO 5920 --- [nio-8080-exec-7] c.hyf.cache.cachetemplate.H2CacheCache   : insert into redis,key:51,value:Book2(id=51, bookName=微服務架構, readFrequency=1, createTime=2020-03-20T16:10:13, updateTime=2020-03-27T09:14:44, version=1)

2020-04-03 09:55:31.864  INFO 5920 --- [nio-8080-exec-2] c.hyf.cache.cachetemplate.H2CacheCache   : select from ehcache,key:51

刪除:刪除數據庫中的數據後,也會刪除 EhcacheRedis 中對應的緩存數據。

2020-04-03 10:05:18.704 DEBUG 5920 --- [nio-8080-exec-3] c.h.t.redis.BookMapper2.deleteById       : ==>  Preparing: DELETE FROM book WHERE id=? 
2020-04-03 10:05:18.704 DEBUG 5920 --- [nio-8080-exec-3] c.h.t.redis.BookMapper2.deleteById       : ==> Parameters: 51(Long)
2020-04-03 10:05:18.731 DEBUG 5920 --- [nio-8080-exec-3] c.h.t.redis.BookMapper2.deleteById       : <==    Updates: 1
2020-04-03 10:05:18.732  INFO 5920 --- [nio-8080-exec-3] c.hyf.cache.cachetemplate.H2CacheCache   : delete from ehcache,key:51
2020-04-03 10:05:18.844  INFO 5920 --- [nio-8080-exec-3] c.hyf.cache.cachetemplate.H2CacheCache   : delete from redis,key:51

其他的就不用演示了...

四、最後

當然啦,這個 starter 還是比較簡單的,如果大家感興趣,可以去看看源碼是如何基於 Spring Cache 實現多級緩存的~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章