Dashboard > GeoTools 2.5 Users Guide > ... > 05 Util > ObjectCache
Added by jgarnett , last edited by jgarnett on Jun 28, 2007  (view change)
Labels: 
(None)

There is a utility class available for your caching needs:

class ObjectCaches {
     ObjectCache create( String policy, int size );
     ObjectCache create( Hints hints );
     String toKey( String code );
     Object toKey( String code1, String code2 );
}

The actual cache is of the form:

interface ObjectCache {
    Object get( Object key );
    void put( Object key, Object value );
    Object peek( Object key );
    writeLock( Object key );
    writeUnLock( Object key );
}

You can use this interface to safely work with a cache from multiple threads:

try {
    cache.writeLock( key );
    value = cache.peek( key );
    if( value == null ){
        value = generateContent( key );
        cache.put( key, value );
    }
}
finally {
    cache.writeUnLock();
}

As you can see the peek method is used to sample the cache from within an already established writeLock. Please make use of try / finally to ensure any acquired writeLock is released.

The javadocs has additional examples of how to use this class safely.

Comparison with JSR 107

Java Specification Request 107 has been tabled in order to define an object cache . We have made our implementation method compatible in the event JSR 107 is accepted.

Comparison with Commons Pool

You may of noticed that GeoTools makes use of the commons pool as a dependency - and that project is well known for offering a bang up implementation of ObjectPool. So your first question should very well be - why the heck does GeoTools have an ObjectCace of its very own.

Well the truth is that we actually need a cache (rather than a pool) and we need it to be threadsafe. You can configure an KeyedObjectPool to behave like a cache (by asking it to use weak references) - but truth is commons pool wants to manage a pool of instances so can return one right away for performance reasons.

Since we are worried about stressing memory (rather than only performance) our ObjectCache is set up have "per entry" read/write locks. We are using it to manage immutable objets so we do not mind returning the same value again and again.

Site running on a free Atlassian Confluence Open Source Project License granted to The Codehaus. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.6.2 Build:#919 Nov 26, 2007) - Bug/feature request - Contact Administrators