✅P169_缓存-SpringCache-@SpringCache细节设置
2024/2/13大约 1 分钟
缓存-SpringCache-@SpringCache细节设置
表达式语法

自定义
指定缓存数据的存活时间
cfmall-product/src/main/resources/application.properties配置
# 单位:毫秒
spring.cache.redis.time-to-live=3600000指定生成的缓存使用的key
key 属性,接收一个SpEL表达式
@Cacheable(value = {"category"},key = "#root.method.name")
@Cacheable(value = {"category"},key = "'level1Categories'")示例代码 cfmall-product/src/main/java/com/gyz/cfmall/product/service/impl/CategoryServiceImpl.java
/**
 * @Cacheable(value = {"category"}, key = "#root.method.name")
 * 代表当前方法的结果需要缓存。如果缓存中有,方法不用调用。如果缓存中没有,会调用方法,最后将方法的结果放入缓存。
 * @return
 */
@Cacheable(value = {"category"},key = "#root.method.name")
@Override
public List<CategoryEntity> listCategory() {
    System.out.println("listCategory......从数据库查询一级类目");
    List<CategoryEntity> categoryEntityList = this.baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", 0));
    return categoryEntityList;
}访问:http://localhost:8200/,查看缓存key
    @Cacheable(value = {"category"},key = "'level1Categories'")
    @Override
    public List<CategoryEntity> listCategory() {
        System.out.println("listCategory......从数据库查询一级类目");
        List<CategoryEntity> categoryEntityList = this.baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", 0));
        return categoryEntityList;
    }