Hi folks,

I've got some questions about the cache and hoped you wouldn't mind me
harassing you about it. I've some queries around the cache implementation
within Magnolia and was hoping you could set me straight.


I've recently implemented a custom cache policy and as part of that work
stumbled upon this piece of code within the
info.magnolia.module.cache.cachepolicy.Default class.


// we need to synchronize on the cache instance, as multiple threads might
be accessing this
// concurrently, and we don't want to block the system if we're using a
blocking cache.
// (since hasElement() might place a mutex on the cache key)
synchronized (cache) {
    if (cache.hasElement(key)) {
        final Object cachedEntry = cache.get(key);
        return new CachePolicyResult(CachePolicyResult.useCache, key,
cachedEntry);
    } else {
        return new CachePolicyResult(CachePolicyResult.store, key, null);
    }
}



1) My first query is around the two calls to retrieve any given element. eg
you first call hasElement and then get. Could we not just call get and check
null as below?



final Object cachedEntry = cache.get(key);
if (cachedEntry != null) {
      return new CachePolicyResult(CachePolicyResult.useCache, key,
cachedEntry);
} else {
      return new CachePolicyResult(CachePolicyResult.store, key, null);
}



2) The second query I have is about the use of the synchronised block around
the cache retrieval. I've searched the entire codebase and all of the calls
which add to the cache have no such synchronized block, only the retrieval
does (I could easily have missed them though as I don't have the full
source).

The comment above the block doesn't make sense as by synchronising on the
cache object you're only marshalling access to that section of code;
subsequent calls to hasElement and get will block globally regardless of
this?

Under the covers, it appears to be using ehCache which is fully thread safe
and no doubt uses appropriate read/write locking.

I would also expect locking code to be encapsulated within the cache class
implementation, the code which uses the cache can then be properly agnostic
regarding the locking strategy.

Thanks for any pointers you can give.

Luke

----------------------------------------------------------------
For list details see
http://www.magnolia-cms.com/home/community/mailing-lists.html
To unsubscribe, E-mail to: <[email protected]>
----------------------------------------------------------------

Reply via email to