In 2.3.19, sharing cache storage

Jaime Garza <[email protected]> Wed, 23 Jul 2014 08:51:52 -0700 (PDT)
Newsgroups gmane.comp.web.freemarker.devel
Message-ID <7e0ffc1e-b820-4a12-9030-fe3831effa08@default>
Hi Daniel,

I am trying to share an instance of MruCacheStorage across threads. I keep it around across multiple threads and when creating my Configuration, I simply do a Configuration#setCacheStorage(). This internally calls Configuration#createTemplateCache, which calls cache.setConfiguration(this), which calls TemplateCache#clear(), which eventually clears the storage.

My use case is that I know that a set of around 200 threads will be accessing the same templates over and over (executing the same template(s) on a different root namespace) concurrently. I want the loading of templates to happen as infrequently as possible. Also, since I am on a slightly customized version of 2.3.19, I cannot come to the greatest new version.

The question (or questions) is: is there a way to implement my strategy? My current workaround is nasty. I have created a subclass of MruCacheStorage, which reads a flag "canClear" from thread local, and then decides whether to let the clear proceed or not. I sandwitched the call to setCacheStorage() with canClear = false and canClear = true

        SharedCacheSettings.disallowClear();
        config.setCacheStorage(storage);
        SharedCacheSettings.allowClear();

AND 

public class SharedCacheStorage extends MruCacheStorage {
    public SharedCacheStorage(int maxStrongSize, int maxSoftSize) {
        super(maxStrongSize, maxSoftSize);
    }
    
    @Override
    public void clear() {
        if (!SharedCacheSettings.canClear())
            return;
        
        super.clear();
    }
}

What is your opinion about my hack? What kind of trouble am I inviting?

Thanks in advance.
Jaime Garza

------------------------------------------------------------------------------
Want fast and easy access to all the code in your enterprise? Index and
search up to 200,000 lines of code with a free copy of Black Duck
Code Sight - the same software that powers the world's largest code
search on Ohloh, the Black Duck Open Hub! Try it now.
http://p.sf.net/sfu/bds