SpringCache @Cacheable keyGenerator

本文介绍了如何在Spring Cache中自定义@Cacheable的key,通过AdListCacheKeyGenerator组件,根据ListMixLiveRoomRequestBO的cityId生成唯一缓存键。关键步骤包括实现KeyGenerator接口和在@Cacheable注解中指定keyGenerator。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有时候我们想自定义@Cacheable 的key,由于自定义的key通过方法的参数无法给出,这时候就需要使用keyGenerator了。

通过查看 @Cacheable 的源码可以看出我们自定义一个 KeyGenerator 需要实现一个接口KeyGenerator

	/**
	 * The bean name of the custom {@link org.springframework.cache.interceptor.KeyGenerator}
	 * to use.
	 * <p>Mutually exclusive with the {@link #key} attribute.
	 * @see CacheConfig#keyGenerator
	 */
	String keyGenerator() default "";

首先定义一个 AdListCacheKeyGenerator来实现 KeyGenerator

/**
 * @author 石冬冬(Chris Suk)
 * @since 2022/10/20 4:36 PM
 */
@Component
@Slf4j
public class AdListCacheKeyGenerator implements KeyGenerator{

    @Value("${zhaopin.live-environment:}")
    String env;

    @Override
    public Object generate(Object target, Method method, Object... params) {
        ListMixLiveRoomRequestBO requestBO = (ListMixLiveRoomRequestBO) params[0];
        String cacheKey = new StringBuilder(env).append("_").append(requestBO.getCityId()).toString();
        log.info("[cacheKeyGenerate],cacheKey={}", cacheKey);
        return cacheKey;
    }
}

然后@Cacheable指定该 keyGenerator

@Cacheable(keyGenerator = "adListCacheKeyGenerator")
    public List<AdBO> listValidAd(ListMixLiveRoomRequestBO requestBO) {
        List<AdBO> ads = thirdAdLiveRoomBusiness.listValidAd(requestBO);
        setTestFlag(ads);
        return ads.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(ad -> ad.getId()))), ArrayList::new));
    }
    ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值