Request Scoped Method Caching

Overview

Often there are functional objects whose methods are called frequently over the course of a single request. For a method whose result is expensive to compute and does not change for a set of arguments over the course of a request caching the result can bring significant improvements to performance.

As of uPortal 4.0.3 the @CacheResult annotation provides an easy way to apply request attribute scoped caching to methods.

Requirements

There are two simple requirements for using the annotation:

  1. The annotated method must be defined by an interface
  2. All method parameters included in the key implement hashCode and equals

Features

  • cacheNull - By default null return values are NOT cached, setting this property to true results in caching of null return values
  • cacheException - By default thrown exceptions are NOT cached, setting this property to true results in caching of thrown exceptions which are then re-thrown on subsequent calls
  • keyMask - By default all method arguments are included in the key. Arguments can be excluded from the key by setting a boolean array with the same length as the number of method arguments. Array positions that are false mean that the argument at the same position will not be included

Usage Example

 

@RequestCache(keyMask={false, true})
@Override
public List<String> getFolderNamesForLayoutNode(HttpServletRequest request, String layoutNodeId) {
    //do expensive stuff here 
}