Announcement: You can find the guides for Commerce 7.5 and later on the new Elastic Path Documentation site. This Developer Center contains the guides for Commerce 6.13.0 through 7.4.1.Visit new site

This version of Elastic Path Commerce is no longer supported or maintained. To upgrade to the latest version, contact your Elastic Path representative.



Domain Object Caching

What is a Domain Object?

Domain objects are classes that represent a unique set of data. Typically in Cortex, domain objects are carts, shoppers, products, and so on. Domain objects are used in nearly all Cortex operations: getting products, logging in customers, adding to cart, and so on.

Benefits of Caching Domain Objects

Cortex can call into your backend system multiple times to retrieve domain objects during a single http request. Domain caching can greatly improve performance, as Cortex won't need to repeatedly retrieve the same domain object from your back end system. For efficiency, Cortex maintains a separate cache of domain objects for each request.

Implementing Domain Object Caching

To implement domain object caching, inject the RequestCacheProvider into your repository class. This provides a single get() method that returns an Optional<ConcurrentMap>. This will resolve absent if you are attempting to access the cache outside of the ResourceOperationContext context. In this case, caching is not available because we cannot grantee the ID to be consistent for the request. The default cache implementation stores the ConcurrentMap using pass by reference so no copies are made.

When implementing a domain cache, you must handle invalidation of the domain objects within a single request. For example, a resource performing the following operations will result in incorrect results if invalidation is not performed.
  1. Get Cart
  2. Add new item to cart
  3. Get Cart
If step 1 caches the response, step 3 will return the cached value if step 2 did not invalidate or update the cache.

Providing a New Request Cache implementation

The default cache is implemented using guava; however, you are free to provide your own implementation. You need to implement the CacheProvider interface to replace the default cache with yours removed ep-guava-cache-impl from the webapp and add your bundle. The default cache uses ResourceOperationContext to generate unique id's you are free to use something else, the only requirement is that the same cache entry is returned irrespective of the calling thread.