Caching in Hybris OCC Layer

The basic definition of caching that you can find on google is:

Caching is the process of storing copies of files in a cache, or temporary storage location, so that they can be accessed more quickly. Technically, a cache is any temporary storage location for copies of files or data, but the term is often used in reference to Internet technologies.

https://www.cloudflare.com/learning/cdn/what-is-caching/

Extending the same Content the OCC layer provide the concept of caching to enable the fast access of the API. A Cache is actually a layer sits between one or more Web servers & Client. It watches the request coming and save the response such are json response in Hybris webservices. When another request comes in and request for the same data then the cache mechanism will return the data from cache instead from server. Caching could be Client-side as well as server-side.

You will see the @CacheControl annotation on request mapping in controller. This annotation is used to generate Cache-Control Header in response. This annotation is defined in webservicescommons extension, which helps in enabling client-side caching for a particular method or the entire controller.

The parameter in this extension contains directive and max age. maxAge define the age of data that stays in cache.

An important point to consider is this annotation works only if we add CacheControlHandlerInterceptor in mvc interceptors.

<mvc:interceptors>         
<bean class="de.hybris.platform.webservicescommons.interceptors.CacheControlHandlerInterceptor"/>     
</mvc:interceptors>

Server side caching can be enabled using <cache:annotation-driven> element. This element will also allow us to define the cache key generator & cache manager which will be used for caching.

The spring configuration file used to manage the server-side caching is :

ycommercewebservices/web/webroot/WEB-INF/config/cache-config-spring.xml

The Ehcache is used for managing server-side caching. Ehcache is an open-source, standards-based cache used to improve performance, offload the database, and simplify the scalability. The details of EhCache is managed using ycommercewebservices/web/webroot/WEB-INF/ehcache.xml

The above @Cacheable annotation is used to cache the method. This annotation may contains parameter value and key. Value indicates the cache name & key includes the cache key generator which will be called with mentioned parameter to generate the cached key.

The default key generator is configured in commercewebservicescommons-spring.xml file of the ycommercewebservices extension. We can a bean with name commerceCacheKeyGenerator which is called to generate the key and store the value. The key generator takes some additional attributes like: <base site,> <language><user><currency> while generating the cache keys

Leave a Reply

Your email address will not be published. Required fields are marked *