Elastic Path Commerce Development

Invalidating Caches on a Repository Method

Invalidating Caches on a Repository Method

To invalidate cached values when running update or delete methods, annotate the method with the @CacheRemove annotation.

In general, as caches are limited to the lifetime of a single request, manually evicting objects from the cache is not required.

How Cache Invalidation Works

Request-scoped caches are divided into cache regions. Each region represents the class that instantiated the cache. Cached values are only available to the class that originated the cache. The same applies for invalidation, only the originating class can invalidate the cache.

@CacheRemove requires either an object of the class to be invalidated or an array of class objects. The value to invalidate equals the return type of the corresponding get method.

@CacheRemove(typesToInvalidate = Customer.class)
        public void deleteCustomer(CustomerId customerId) { ... }
        
        @CacheRemove(typesToInvalidate = Customer.class)
        public void deleteAllCustomers() { ... }   

Invalidating by Parameter Equality Matching

To invalidate a cache by parameter equality matching, match the invalidation method parameters. Only cached entries with the same input parameters are removed from the cache.

For example, given the following methods, getCustomerById() and deleteCustomer()

@CacheResult()
public Customer getCustomerById(CustomerId customerId){ ... }
              
@CacheRemove(typesToInvalidate = Customer.class)
public void deleteCustomer(CustomerId customerId){ ... }

If both methods have the same input parameter when called, for example CustomerId(123), then only the values associated with CustomerId(123) is removed from the cache.

Invalidating by Partial Parameter Matching

To invalidate a cache by partial parameter matching, invalidate using a subset of the cached method parameters.

For example, given the following methods, getCustomerByFullName() and deleteCustomer():

@CacheResult()
public Customer getCustomerByFullName(String name, String preName){ ... }
            
@CacheRemove(typesToInvalidate = Customer.class)
public void deleteCustomer(String preName){ ... }

The parameters of the deleteCustomer() method are a subset of getCustomerByFullName()'s parameters. If deleteCustomer("Albert") is called, all Customer objects with the preName Albert are removed from the cache.

Invalidating by Unique Identifiers

To invalidate a cache by its unique identifier, first define a uniqueIdentifier in the cached method.

For example, this method defines customerId as a unique identifier for any cached objects.

@CacheResult(uniqueIdentifier="customerId")
public Customer getCustomerById(CustomerId customerId){ ... }

@CacheRemove(typesToInvalidate = Customer.class, uniqueIdentifier="customerId")
public void deleteCustomer(CustomerId customerId){ ... }

By passing in the unique identifier when invalidating a cache, all Customer objects with the customerId as their identifier are removed from the cache.

Invalidating an Entire Cache Region

If, for some reason, you cannot use any of the above invalidation strategies, you can invalidate a cache by removing all caches in a region (i.e. for a specific domain object). This is the least performant way to invalidate caches and should not be used.

For example, in the following, all caches of type Customer are removed:

@CacheResult()
public Customer getCustomerById(String storeCode,String userId){ ... }
          
@CacheRemove(typesToInvalidate = Customer.class)
public void removeCustomer(String customerName){ ... }