Sunday, November 6, 2011

Liferay caching

Liferay is caching on a few places. Good one to start at is taking a look at PortalContextLoaderListener where all the key classes that take care of pooling, caching or buffering get revealed.

Entity caching and Finder caching :

These are just java collections that are mapping an instance of an Object representing an Entity to a primary key. Unless you access database by other means than via Persistence layer generated by Service Builder, you can rely on these types of cache. The finder cache is using FinderPaths, you can take a look at how it is done :

fetchByContactId(long contactId, boolean retrieveFromCache) {
  Object[] finderArgs = new Object[] { contactId };

  Object result = null;

  if(retrieveFromCache) {
   result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY, finderArgs, this);

To disable entity and finder caching of User entity for instance, you set these properties to false in :

Hibernate caching :

Liferay is using Ehcache as a caching provider for Hibernate. If you are using Service Builder, you can choose whether to cache entity or not by attribute cache-enabled in portlet-hbm.xml

   <entity name="Order" remote-service="false" cache-enabled="false">

and hibernate entity mapping will be generated accordingly :

   <cache usage="read-write" />

Permission caching :

Liferay has improved permissioning system and its performance so much two years ago, that it doesn't seem to be an issue for now. Take a look at PermissionCacheUtil if you need to know more.

ThreadLocal caching :

Liferay is using ThreadLocalCache via AOP. Take a look at ThreadLocalCacheAdvice and corresponding @ThreadLocalCachable annotation.

No comments: