鉴于这些实体和存储库在DDBB中访问其数据:
@Entity
public class Customer {
Long id;
}
@Entity
public class Purchase {
Long customerId;
}
@Repository
public lass PurchaseDAO {
public void insert(Purchase insert);
public void deleteCustomerPurchases(Long customerId);
public long getTotalPurchasesAmount(Long customerId);
public long getTotalPurchasesAmountPerMonth(Long customerId, int month);
}
我想为方法getTotalPurchaseAmounts(Long customerId)添加缓存,这样,当为客户添加一些购买时,只有被购买的客户被驱逐。
相关的依赖条目是:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.2</version>
</dependency>
相关配置:
@EnableCaching
@Configuration
public class CommonConfig {
@Bean
public CacheManager cacheManager() {
EhCacheCacheManager cacheManager = new EhCacheCacheManager();
cacheManager.setCacheManager( ehCacheManager().getObject() );
return cacheManager;
}
@Bean
public EhCacheManagerFactoryBean ehCacheManager() {
EhCacheManagerFactoryBean ehcache = new EhCacheManagerFactoryBean();
ehcache.setConfigLocation( new ClassPathResource( "ehcache.xml" ) );
return ehcache;
}
}
由于弹簧缓存(和ehcache)逐出被限制为元素或所有条目,我开发的解决方案是创建缓存(每个客户一个),所以我可以逐出。
我认为最好的扩展点是实现自定义CacheResolver:
@Component("CustomerPurchasesCacheResolver")
public class CustomerPurchasesCacheResolver implements CacheResolver {
@Autowired
private EhCacheCacheManager cacheManager;
@Override
public Collection<? extends Cache> resolveCaches( CacheOperationInvocationContext<?> context ) {
String cacheName = "customerPurchases_" + getCustomerId( context );
// Add cache to cacheManager if it does not exists
cacheManager.getCacheManager().addCacheIfAbsent( cacheName );
Set<Cache> caches = new HashSet<>();
caches.add( cacheManager.getCache( cacheName ) );
return caches;
}
// Retrieves customerId from cache operation invocation context;
private Long getCustomerId( CacheOperationInvocationContext<?> context ) {
String key = ( (CacheOperation) context.getOperation() ).getKey();
// TODO Evaluate key
// HOW CAN I DO THIS????????????
return null;
}
}
并将Spring缓存添加到我的存储库方法中:
@Repository
public lass PurchaseDAO {
@CacheEvict(cacheResolver="CustomerPurchasesCacheResolver", key="#purchase.customerId")
public void insert(Purchase purchase);
@CacheEvict(cacheResolver="CustomerPurchasesCacheResolver", key="#customerId")
public void deleteCustomerPurchases(Long customerId);
@Cacheable(cacheResolver="CustomerPurchasesCacheResolver")
public long getTotalPurchasesAmount(Long customerId);
@Cacheable(cacheResolver="CustomerPurchasesCacheResolver")
public long getTotalPurchasesAmountPerMonth(Long customerId, int month);
}
我使用这个方法的唯一问题是使用Spring Expresions来评估密钥。
有没有办法让这个或不同的方法有效?
答案 0 :(得分:2)
为每个客户记录创建缓存太过分了。使用SpEL,您可以指定要逐出的客户记录的密钥。配置ehcache以便有一个客户缓存。然后,您更改PurchaseDAO方法,以便它们指定要缓存或逐出的密钥。更改您的代码如下
find / -type l -name 'ctags-exuberant' 2>/dev/null
但是,要回答有关从CacheResolver获取customerId的问题,CacheOperationInvocationContext有一个getArgs()方法,该方法返回传递给要缓存的方法的参数。
@CacheEvict(value = "customerCache" , key="#purchase.customerId")
public void insert(Purchase purchase);
@Cacheable( value = "customerCache" ,key = "#customerId") // you can omit the key if using default key generator as it still uses the method argument
public long getTotalPurchasesAmount(Long customerId);