Wednesday, 13 December 2017

Hybris Platform Cache

Hybris Cache 
  • part of Hybris persistence layer. 
  • stores in memory:
    • search results
    • item attributes
    • item instances

When Data Is Cached
Every time the API is accessed, the cache intercepts calls and handles caching implicitly. 
  • Caching item attributes
  • Caching FlexibleSeach results

When Data Is Removed from Cache

Managing Hybris Cache
HAC > Monitoring > Cache

  • Clear cache
  • Monitor Cache Statistics

How Data Is Cached
- Hybris Region Cache
- can be split into multiple partitions called cache regions

Cache region
- can hold its own set of handled types
- can configure the size of each region and its eviction strategy

Supported eviction strategies include:

By default, the Hybris Region Cache provides the following cache regions:
  • ehCacheEntityCacheRegion
  • Unlimited cache for type system region (No eviction strategy)
  • LRUCacheRegion

Standard Configuration
  • Type system region: For storing entities of type system items
    • Size: Unlimited
    • No eviction strategy
  • Entity region: For storing entities of all types except type system ones
    • Eviction strategy: FIFO
  • Query results region: For storing all query results
    • Eviction strategy: FIFO
  • Media items region: For storing all media items
    • Eviction strategy: LRU

Configuring Region Cache
By default, the Hybris Region Cache is configured in:
  • advanced.properties
  • core-cache.xml
advanced.properties

core-cache.xml

To modify default Region settings:
  • Change values in local.properties
  • Override the cache bean in <extn.>-spring.xml file using an alias.

Adding New Cache Region
<extn.>-spring.xml
<bean name="productCacheRegion" 
class="de.hybris.platform.regioncache.region.impl.EHCacheRegion">
<constructor-arg name="name" value="productCacheRegion" />
<constructor-arg name="maxEntries" value="50000" />
<constructor-arg name="evictionPolicy" value="LFU" />
<property name="handledTypes">
<array>
<value>1</value>
</array>
</property>
</bean>
name: unique region name
maxEntries: cache region size
evictionPolicy: LRU, LFU, FIFO
handledTypes: list of types stored in this region. Use deployment code to set up types

Cache regions are Spring beans holding objects of a specified type. Each region has its own set of handled types. It is also possible to have a region without a defined type of objects. However, such region is not taken into account by a controller and you need to implement a region resolver to be able to use it. 

To define a region type:
  • Provide a deployment code list from items.xml file if you want that a region stores entities for the provided deployment code.
  • Choose a special defined type called ALL_TYPES. It denotes that a region stores all unconfigured types.
To configure a cache region to handle query results, use:
  • QUERY_CACHE: a region stores query results.
  • NO_QUERY: Used together with ALL_TYPES to denote that a region stores all unconfigured types except query results.

Hybris Cache API
The Hybris Cache API is backwards compatible
E.g.: single item invalidation is done by:
de.hybris.platform.util.Utilities.invalidateCache(primaryKey);

Access Statistics for a Cache Region
@Autowired
CacheController controller;
...
Collection<CacheRegion> regions = controller.getRegions();
for (CacheRegion region : regions) {
CacheStatistics stats = region.getCacheRegionStatistics();
...
}

Clearing Cache
  • Clearing all cache regions: controller.clearCache();
  • Clearing a single cache region: region.clearCache();

Customizing Region Cache
  • CacheController: Contains methods coordinating cache operations on multiple regions.
  • CacheConfiguration: Provides configuration to the controller and it holds:
    • CacheRegion: Defines the interface of the cache region.
      • DefaultCacheRegion: Simple FIFO cache region, which is the default for entity and query results regions
      • EHCCacheRegion: Cache based on Ehcache
      • UnlimitedCacheRegion: Cache region for type system
    • CacheRegionResolver: Provides regions to the cache controller. There can be more than one region resolver in use.When reading from a DB or adding to the DB, the cache controller sequentially asks region resolvers for a given key. The first region that holds cached data is used. In case of invalidation, the cache controller asks all region resolvers and the invalidated value for the given key is removed from all returned regions.
    • InvalidationFilter: The system can have more than one filter registered. Registered invalidation filters are processed one by one to check if all of them enable invalidation. If at least one filter disallows invalidation, data stays in cache until is evicted. Invalidation filters are checked every time the invalidation is fired.
  • CacheRegionProvider: Keeps regions lists and provides regions list access methods.
  • CacheStatistics: Keeps statistics for a single cache region.
    
License Restrictions on Cache Size
Depending on your license you have a certain limit of total cache size for all regions. After extending the cache sizes without additional license, the Hybris Platform will not start.

Disabling the Hybris Region Cache
local.propertiescache.legacymode=true

Hybris Region Cache is the default cache.

No comments:

Post a Comment