0

Given these entities and the repository to access their data in 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);
}

I want to add cache for method getTotalPurchaseAmounts(Long customerId), in such way that, when adding some purchase for a customer, only purchasesd of that customer are evicted.

The related dependance entries are:

        <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>

Related configuration:

@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;
    }

}

As spring cache (and ehcache) evicts are limited as per element or all entries, the solution I have developed is creating caches dinamically (one for each customer) so I can evict those.

The extension point I thinks is the best for this is implementing a custom 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;
    }

}

and adding Spring cache to my repository methods:

@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);

}

The only problem I get using this aproach is to get the key evaluated using Spring Expresions.

Is there any way to get that or a different aproach that could work?

1 Answer 1

2

Its an overkill to create a cache for every customer record. Using SpEL you can specify a key for the customer record to evict. Configure ehcache so that there is one customer cache. Then you change your PurchaseDAO methods so that they specify the key to cache or evict. Change your code as follows

@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);

However to answer your question about getting the customerId from the CacheResolver , CacheOperationInvocationContext has a getArgs() method which returns the arguments passed to the method to be cached.

 private Long getCustomerId( CacheOperationInvocationContext<?> context ) {
        Object[] args = context.getArgs();
        Object firstArg = args[0];
        if(firstArg instanceof Long){
           return (Long)firstArg;
        }
        else if(firstArg instanceof Purchase){
            Purchase purchase = (Purchase)firstArg;
            return purchase.getCustomerId();
        }
        return null;
    }
1
  • Thank you Ekem. The thing is that there are going to be more than one record per customer (i.e. @Cacheable public int getPurchasesAmountPerMonth(int month)), and methods with different signature that evict cache, so just getting args[0] does not do the job. You have made me realize that my original question lacks key information. I am going to edit the that to show that idea as the main point of the problem.
    – Basa
    Commented May 15, 2016 at 22:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.