- 在 pom.xml 文件中添加 Redis 的依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- 在 application.yml 文件中添加 Redis 的配置:
spring:
redis:
host: 127.0.0.1
port: 6379
database: 1
pool:
max-idle: 8
min-idle: 0
max-active: 8
max-wait: -1
- 在 Application.java 類中添加 @EnableCaching 注解,啟用緩存:
@EnableCaching
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- 在 Repository 或 Service 類的方法中添加緩存相關(guān)的注解:
import java.util.List;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class RuleService {
@Transactional
@CacheEvict(cacheNames = {"EnabledCommonRules", "EnabledRulesByPackage"}, allEntries = true)
public void batchEnable(String[] ids) {
repository.batchEnable(ids, CommonUtils.getCurrentUserName());
}
@Transactional
@CacheEvict(cacheNames = {"EnabledCommonRules", "EnabledRulesByPackage"}, allEntries = true)
public void batchDisable(String[] ids) {
repository.batchDisable(ids, CommonUtils.getCurrentUserName());
}
@Cacheable(cacheNames = "EnabledCommonRules")
public List<RuleDto> getEnabledCommonRules() {
return mapper.entitiesToDtos(repository.getEnabledCommonRules());
}
@Cacheable(cacheNames = "EnabledRulesByPackage")
public List<RuleDto> getEnabledRulesByPackage(String packageName) {
return mapper.entitiesToDtos(repository.getEnabledRulesByPackage(packageName));
}
@Caching(evict = {@CacheEvict(cacheNames = "EnabledCommonRules", allEntries = true,
condition = "#p0 != null && #p0.isCommon == 1"),
@CacheEvict(cacheNames = "EnabledRulesByPackage", key = "#p0.packageName", condition = "#p0 != null")})
public RuleDto create(RuleDto dto) {
return super.create(dto);
}
@Caching(evict = {@CacheEvict(cacheNames = "EnabledCommonRules", allEntries = true,
condition = "#p0 != null && #p0.isCommon == 1"),
@CacheEvict(cacheNames = "EnabledRulesByPackage", key = "#p0.packageName", condition = "#p0 != null")})
public RuleDto update(RuleDto dto) {
return super.update(dto);
}
@Caching(evict = {@CacheEvict(cacheNames = "EnabledCommonRules", allEntries = true,
condition = "#p0 != null && #p0.isCommon == 1"),
@CacheEvict(cacheNames = "EnabledRulesByPackage", key = "#p0.packageName", condition = "#p0 != null")})
public RuleDto delete(RuleDto dto) {
return super.delete(dto);
}
@Caching(evict = {@CacheEvict(cacheNames = "EnabledCommonRules", allEntries = true,
condition = "#result != null && #result.isCommon == 1"),
@CacheEvict(cacheNames = "EnabledRulesByPackage", key = "#result.packageName",
condition = "#result != null")})
public RuleDto delete(String id) {
return super.delete(id);
}
@CacheEvict(cacheNames = {"EnabledCommonRules", "EnabledRulesByPackage"}, allEntries = true)
public List<RuleDto> deleteInBatch(List<Rule> list) {
return super.deleteInBatch(list);
}
@CacheEvict(cacheNames = {"EnabledCommonRules", "EnabledRulesByPackage"}, allEntries = true)
public List<RuleDto> deleteInBatch(String[] ids) {
return super.deleteInBatch(ids);
}
}