Performance
Performance
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.
- Get Cart
- Add new item to cart
- Get Cart
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.