spring-cache
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
caffeine
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
spring:
cache:
type: caffeine
cache-names: ‘ruleCache‘
caffeine:
spec: initialCapacity=1000, maximumSize=18000, expireAfterAccess=20D
package com.cfuture.dataqualitycontrol.service.impl;
import com.cfuture.dataqualitycontrol.domain.po.Rule;
import com.cfuture.dataqualitycontrol.service.RuleService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
/**
* @author yangjx
* @date 2021/1/21 14:04
* @description
*/
@Service
public class RuleServiceImpl implements RuleService {
@Override
@CachePut(value = "ruleCache", key = "#rule.id")
public Rule addRule(Rule rule) {
return rule; // 必须返回被缓存对象
}
@Override
@Cacheable(value = "ruleCache", key = "#id")
public Rule getRule(Long id) {
Rule rule = Rule.builder().expression(String.valueOf(id)).message(String.valueOf(System.currentTimeMillis())).build();
rule.setId(id);
return rule; // 必须返回被缓存对象
}
@Override
@CacheEvict(value = "ruleCache", key = "#id")
public boolean deleteRule(Long id) {
return true;
}
@Override
@CachePut(value = "ruleCache", key = "#rule.id")
public Rule updateRule(Rule rule) {
return rule; // 必须返回被缓存对象
}
}
原文:https://www.cnblogs.com/JaxYoun/p/14313070.html