缓存的引入是现在大部分系统所必须考虑的
综合所述:我们需要构建 L1 Caffeine JVM 级别缓存 , L2 Redis 缓存。
目前大部分应用缓存都是基于 Spring Cache 实现,基于注解(annotation)的缓存(cache)技术,存在的问题如下:
<dependency>
<groupId>com.pig4cloud.plugin</groupId>
<artifactId>multilevel-cache-spring-boot-starter</artifactId>
<version>0.0.1</version>
</dependency>
@EnableCaching
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
@Cacheable(value = "get",key = "#key")
@GetMapping("/get")
public String get(String key){
return "success";
}
为保证性能 redis 在 127.0.0.1 环路安装
Benchmark | Mode | Cnt | Score | Units |
---|---|---|---|---|
多级实现 | thrpt | 2 | 2716.074 | ops/s |
默认 redis | thrpt | 2 | 1373.476 | ops/s |
public class RedisCaffeineCacheManager implements CacheManager {
@Override
public Cache getCache(String name) {
Cache cache = cacheMap.get(name);
if (cache != null) {
return cache;
}
cache = new RedisCaffeineCache(name, stringKeyRedisTemplate, caffeineCache(), cacheConfigProperties);
Cache oldCache = cacheMap.putIfAbsent(name, cache);
log.debug("create cache instance, the cache name is : {}", name);
return oldCache == null ? cache : oldCache;
}
}
public class RedisCaffeineCache extends AbstractValueAdaptingCache {
protected Object lookup(Object key) {
Object cacheKey = getKey(key);
// 1. 先调用 caffeine 查询是否存在指定的值
Object value = caffeineCache.getIfPresent(key);
if (value != null) {
log.debug("get cache from caffeine, the key is : {}", cacheKey);
return value;
}
// 2. 调用 redis 查询在指定的值
value = stringKeyRedisTemplate.opsForValue().get(cacheKey);
if (value != null) {
log.debug("get cache from redis and put in caffeine, the key is : {}", cacheKey);
caffeineCache.put(key, value);
}
return value;
}
}
public class RedisCaffeineCache extends AbstractValueAdaptingCache {
@Override
public void put(Object key, Object value) {
push(new CacheMessage(this.name, key));
}
@Override
public ValueWrapper putIfAbsent(Object key, Object value) {
push(new CacheMessage(this.name, key));
}
@Override
public void evict(Object key) {
push(new CacheMessage(this.name, key));
}
@Override
public void clear() {
push(new CacheMessage(this.name, null));
}
private void push(CacheMessage message) {
stringKeyRedisTemplate.convertAndSend(topic, message);
}
}
public class CacheMessageListener implements MessageListener {
private final RedisTemplate<Object, Object> redisTemplate;
private final RedisCaffeineCacheManager redisCaffeineCacheManager;
@Override
public void onMessage(Message message, byte[] pattern) {
CacheMessage cacheMessage = (CacheMessage) redisTemplate.getValueSerializer().deserialize(message.getBody());
cacheMessage.getCacheName(), cacheMessage.getKey());
redisCaffeineCacheManager.clearLocal(cacheMessage.getCacheName(), cacheMessage.getKey());
}
}
[https://github.com/pig-mesh/multilevel-cache-spring-boot-starter
](https://github.com/pig-mesh/multilevel-cache-spring-boot-starter)
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。