Flexible queries are already cached in hybris commerce which provide good performance in most of projects. Generally, hybris regions cache performs well when system has more read of data than writes. Frequent changes to data (products, images, master data etc.) will result in invalidation of cached entities, types and flexible queries on all nodes forcing system to fetch data from database as cache has been invalidated. Projects having very frequent changes in database can suffer from high performance degradation due to frequent invalidations.
In this post we will be discussing an option to stop invalidation of query for specified amount of time irrespective of change in items in database, this feature is called TTL – Tme To Live. The value of TTL is defined in session context which enforces query to live at least for this duration. Implementation of TTL can significantly improve performance in applications which have frequent changes in database.
Below are steps to executed to implement TTL in a project:
- Create an aspect class as below – this class is picking configurable properties: a flag and duration of TTL (in seconds) from configuration file and sets in session context:
- Define spring bean:
- Add AOP configuration for specific methods to enable TTL caching:
Please note this feature should be tuned as per your use case. Applications with less writes than reads can have adverse affect on performance. Downside of this configuration is that cached queries will not pick changes immediately on storefront for the defined time so there need to be ballance between performace gain vs data accuracy. Overall this is a very good option to finetune the performance of hybris applications.
Try it and enjoy improved performance!