Jump to content
  • Customizing the Ad Hoc Cache Key


    mgeise
    • Features: Ad Hoc, Cache Version: v3.5, v3.1 Product: JasperReports® Server

    In certain scenarios, where JasperReports Server Professional is integrated with your own application, there may be a need to programmatically clear the Ad Hoc cache in JasperReports Server when you recognize that the user has done something to change the set of data they are working with within your application. (i.e. a change in role, etc.). This article covers one possible way that this might be handled - by adding a custom cache key.

    Adding a Custom Cache Key

    Note:This method only would apply to v3.1 or v3.5 of JasperReports Server Professional and since it is not a documented feature is subject to changes in implementation. In many cases, the need for this may be eleviated through the use of Multi-tenancy that was released in v3.5 of JasperReports Server Professional.

    There is a CacheKeyInterceptor interface that can modify the keys used by the Ad Hoc cache. It may be possible to modify the default key interceptor to include the value of a custom key in cache keys, so that a query with the same user id but a different custom key value would be kept separate.

    Having a static method to get its value makes it easy to implement the fix using the following steps:

    Step 1: Subclass DefaultCacheKeyInterceptor

    (com.jaspersoft.commons.datarator.DefaultCacheKeyInterceptor)

    In the implementation of getCacheKeyCalled(), extract the map containing query parameters and add the custom_cache_key value:

    package my.custom.code; import java.util.Map;
    import com.jaspersoft.commons.datarator.jr.JRDataSourceAdapter;
    import com.jaspersoft.commons.datarator.DefaultCacheKeyInterceptor;
    public class CharityIdAddingCacheKeyInterceptor extends DefaultCacheKeyInterceptor {
        public Object getCacheKeyCalled(Object key) {
            // call superclass, cast to map
            Map keyMap = (Map) super.getCacheKeyCalled(key);;
            Map paramMap = (Map) keyMap.get(JRDataSourceAdapter.PARAMS);
            // put charity id in map
            paramMap.put("charity_id", "plug in static call to get custom cache key here");
            return keyMap;
        }
    }
    

    Step 2: Modify the Spring Config to use the new CacheKeyInterceptor

    In WEB-INF/applicationContext-adhoc.xml, find the keyInterceptor property of the dataSetCache bean, which will look something like this:

    <!-- keyInterceptor: a class implementing CacheKeyInterceptor which can modify the cache keys -->
    <property name="keyInterceptor">
        <bean class="com.jaspersoft.commons.datarator.DefaultCacheKeyInterceptor">
    

    Change the class to your new implementation and leave everything else the same:

    <property name="keyInterceptor">
        <bean class="my.custom.code.CharityIdAddingCacheKeyInterceptor">
    

    Step 3: Test

    You can verify that it works by looking at the details of the Ad Hoc cache entries--you should see your new custom cache key parameter.


    User Feedback

    Recommended Comments

    There are no comments to display.



    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

×
×
  • Create New...