Cache Overview
​
Caching plays a crucial role in improving the performance and scalability of the system. Caching is a mechanism used to store and reuse frequently accessed data, reducing the need to fetch it from the database or other data sources every time it's requested.
Caching is the part of persistence layer in Hybris. Caching stores the search result, item attribute and item instances. Cache interprets the calls when we query any data, it’s either returned from the cache, or retrieved from the database and written to the cache.
​
Sometimes when the cache reaches the maximum size and can not hold any additional entries then displacement strategy removes the entry from cache. When cache is no longer valid and data is not in sync with database the invalidation strategy invalidate the cache.
​
Key Types of caching :
-
Item Cache: This cache stores instances of model objects (items) retrieved from the database. It helps reduce the load on the database by caching frequently accessed items. The configuration for this cache can be found in the project.properties or local.properties file.
-
Attribute Cache: This cache stores frequently accessed attributes of items, reducing the need to load the entire item from the database. It can be configured at the attribute level, specifying which attributes should be cached.
-
Region Cache: A region cache is used for caching arbitrary data in a specific region. Developers can use this cache for custom caching needs.
-
CMS Site Cache: This cache stores site-specific data to improve the performance of content management features.
Region Cache
Region cache gives the flexibility of dividing the cache in to multiple parts. We can specify the attributes cached for each region. This provides the flexibility that certain objects are cached for longer time while other objects are removed quickly due to limited cache size.
Region cache can be split in to multiple regions called cache regions. Each region can be configured separately and holds the specific data type. We can specify the size and eviction strategy for each region.
​
Region Cache Standard Configuration : The default set up of SAP Commerce Region Cache contains the following settings:
-
Type system region: Type system items storing
-
Entity region: Storing entities of all types except type system ones
-
Query results region: Query results storing
-
Media items region: All media items Storing
The de
​
​
​
​
Region cache eviction strategy: The region cache has the following eviction strategy.
​
-
Least Recently Used (LRU):
-
In this strategy, the cache evicts the least recently used entries first when it needs to make space for new entries.
-
LRU is a common and straightforward eviction strategy, ensuring that the least accessed items are removed first.
-
You can configure this strategy using the memoryStoreEvictionPolicy property with a value of "LRU" in the cache region configuration.
-
-
Least Frequently Used (LFU):
-
In this strategy, the cache evicts the least frequently used entries first when it needs to make space for new entries.
-
LFU takes into account how often each entry is accessed and retains entries that are accessed more frequently.
-
You can configure this strategy using the memoryStoreEvictionPolicy property with a value of "LFU" in the cache region configuration.
-
-
First-In-First-Out (FIFO):
-
FIFO is a straightforward strategy where the cache evicts the oldest entries first when it needs to make space for new entries.
-
It operates based on the order in which entries were added to the cache.
-
You can configure this strategy using the memoryStoreEvictionPolicy property with a value of "FIFO" in the cache region configuration.
-
​
​
​
​
​
​
Region Cache Config Modification
​
By changing values in local.properties: We can change the preconfigured value in the local.properties file and override the existing values.
# Default value is 100000.
regioncache.entityregion.size=100000
# value is FIFO (default).
regioncache.entityregion.evictionpolicy=LRU
​
​
​
​
Adding New Cache Region
​
You can implement your own cache region with a cache implementation different then the default one. An example below presents Spring configuration for your region cache that uses Ehcache implementation:
​
-
<bean name="userCacheRegion" class="de.hybris.platform.regioncache.region.impl.EHCacheRegion">
-
<constructor-arg name="name" value="userCacheRegion" />
-
<constructor-arg name="maxEntries" value="50000" />
-
<constructor-arg name="evictionPolicy" value="LFU" />
-
<property name="handledTypes">
-
<array> <value>1</value>
-
</array>
-
</property>
-
</bean>
​
A cache region has the following properties:
-
name: region name with unique value
-
maxEntries: region size of cache
-
evictionPolicy: LRU, LFU and FIFO
-
handledTypes: List of types which will automatically stored in this region. Use the deployment code of the table you want to store
