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));
    }
    ```
### 使用 `@Cacheable` 注解时自定义 `KeyGenerator` 为了实现更灵活的键生成策略,在使用 `@Cacheable` 注解时可以指定一个自定义的 `KeyGenerator` 来替代默认的行为。这允许开发者根据业务逻辑创建更加复杂和精确的缓存键。 #### 创建自定义 `KeyGenerator` 首先,需要定义一个新的类并让它实现了 `org.springframework.cache.interceptor.KeyGenerator` 接口: ```java import org.springframework.cache.interpreter.DefaultParameterNameDiscoverer; import org.springframework.cache.interceptor.KeyGenerator; public class CustomKeyGenerator implements KeyGenerator { private final DefaultParameterNameDiscoverer discoverer = new DefaultParameterNameDiscoverer(); @Override public Object generate(Object target, java.lang.reflect.Method method, Object... params) { StringBuilder sb = new StringBuilder(); String[] paramNames = discoverer.getParameterNames(method); if (paramNames != null && paramNames.length > 0) { for (int i = 0; i < paramNames.length; ++i) { sb.append(paramNames[i]).append("=").append(params[i]); if (i < paramNames.length - 1) { sb.append("_"); } } } return sb.toString(); } } ``` 此代码片段展示了如何构建一个简单的字符串形式的键名,其中包含了方法参数名称与其对应的值[^1]。 #### 配置 Bean 接着要在 Spring 上下文中注册该 `CustomKeyGenerator` 实例作为 bean: ```xml <bean id="customKeyGenerator" class="com.example.CustomKeyGenerator"/> ``` 对于 Java Config 用户,则可以在配置类中添加如下内容: ```java @Configuration public class AppConfig { @Bean public KeyGenerator customKeyGenerator() { return new CustomKeyGenerator(); } } ``` #### 应用于 `@Cacheable` 最后一步是在目标服务层的方法上应用 `@Cacheable` 并指明要使用的 `keyGenerator` 属性: ```java @Service public class MyService { @Autowired private UserRepository userRepository; @Cacheable(value = "users", keyGenerator = "customKeyGenerator") public User findUser(String name, Integer age) { // ... some logic here ... return userRepository.findByNameAndAge(name, age); } } ``` 上述例子表明了当调用 `findUser` 方法时将会利用之前定义好的 `CustomKeyGenerator` 来计算唯一的缓存键[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值