下面以Redis集成为例,介绍key的主要用法。
自定义KeyGenerator
@Bean
public KeyGenerator myKeyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object o, Method method, Object... params) {
StringBuilder sb = new StringBuilder();
sb.append(o.getClass().getName());
sb.append(method.getName());
sb.append("(");
for (Object param : params) {
sb.append(param.toString());
sb.append(",");
}
sb.append(")");
return sb.toString();
}
};
}
固定key示例
@Cacheable(value = CacheConsts.MASTER_SYSTEMCONFIG_CACHE_KEY, key = "‘SYSTEMCONFIG_MAP‘")
public Map<String, SystemConfigDto> loadSystemConfigMap() {
变量key示例
@Cacheable(value = CacheConsts.WS_FILTER_TIME_KEY, key = CacheConsts.WS_FILTER_TIME_KEY + "#appId")
public List<WsTimeFilterDto> loadByAppId(String appId) {
@Cacheable(value = CacheConsts.MASTER_CITY_CACHE_KEY, key = CacheConsts.MASTER_CITY_CACHE_KEY + "#exchangeRateEntity.currency")
public List<ExchangeRateEntity> loadExchangeRate(ExchangeRateEntity exchangeRateEntity) {
自定义Key示例@Cacheable(value = CacheConsts.WS_PRICE_RULE_KEY, keyGenerator = "myKeyGenerator")
condition示例
@Cacheable(value = CacheConsts.MASTER_CITY_CACHE_KEY, key = CacheConsts.MASTER_CITY_CACHE_KEY + "#exchangeRateEntity.currency",condition = "#exchangeRateEntity.currency ne ‘CNY‘ ")
public List<ExchangeRateEntity> loadExchangeRate(ExchangeRateEntity exchangeRateEntity) {
原文:http://blog.51cto.com/10705830/2162382