Skip to content

Commit

Permalink
extracted majority of caching implementation to TimedCache class whic…
Browse files Browse the repository at this point in the history
…h can be extended for different stores. might be good to move the store part to use composition in future
  • Loading branch information
rodnaph committed May 8, 2012
1 parent c0e3f16 commit 18fee9b
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 85 deletions.
99 changes: 14 additions & 85 deletions src/com/pugh/sockso/cache/ObjectCache.java
Expand Up @@ -8,8 +8,12 @@

import org.apache.log4j.Logger;

/**
* The object cache implements an in memory data store
*
*/
@Singleton
public class ObjectCache implements Cache {
public class ObjectCache extends TimedCache {

private static final Logger log = Logger.getLogger( ObjectCache.class );

Expand All @@ -27,106 +31,31 @@ public ObjectCache() {
}

/**
* Indicates if the key is cached
*
* @param key
*
* @return
*
*/

public boolean isCached( final String key ) {

final CachedObject object = readRaw( key );
final boolean isCached = isCacheOk( object );

log.debug( "Cache " +(isCached ? "hit" : "miss")+ " for " +key );

return isCached;

}

/**
* Writes an object to the cache, will never expire
*
* @param key
*
* @param value
*
*/

public void write( final String key, final Object value ) {

write( key, value, -1 );

}

/**
* Writes an object to expire in the specified number of seconds
*
* @param key
* @param value
* @param timeout
*
*/

public synchronized void write( final String key, final Object value, final int timeout ) {

log.debug(
"Write key " +key+
( timeout != -1 ? " expires in " +timeout+ " seconds" : "" )
);

data.put( key, new CachedObject( value, timeout ) );

}

/**
* Tries to read an object from the cache, returns null on cache miss
* Does a raw read on the internal data store
*
* @param key
*
* @return
*
*/

public Object read( final String key ) {

final CachedObject object = readRaw( key );

return isCacheOk( object )
? object.getValue()
: null;

}
protected synchronized CachedObject readRaw( final String key ) {

/**
* Indicates if an internal cache object is still fresh (might be null)
*
* @param object
*
* @return
*
*/

private boolean isCacheOk( final CachedObject object ) {

return ( object != null && !object.isExpired() );
return data.get( key );

}

/**
* Does a raw read on the internal data store
*
* Puts a cached object in the data cache
*
* @param key
*
* @return
*
* @param object
*
*/

private synchronized CachedObject readRaw( final String key ) {
protected void writeRaw( final String key, final CachedObject object ) {

return data.get( key );
data.put( key, object );

}

Expand Down
126 changes: 126 additions & 0 deletions src/com/pugh/sockso/cache/TimedCache.java
@@ -0,0 +1,126 @@

package com.pugh.sockso.cache;

import org.apache.log4j.Logger;

/**
* Timed cached implements simple cache with optional timeout for keys. At
* the moment this is implemented through extension, but might be a good idea
* at some point to refactor to use composition instead.
*
*/
abstract public class TimedCache implements Cache {

private static final Logger log = Logger.getLogger( ObjectCache.class );

/**
* Indicates if the key is cached
*
* @param key
*
* @return
*
*/

public boolean isCached( final String key ) {

final CachedObject object = readRaw( key );
final boolean isCached = isCacheOk( object );

log.debug( "TimedCache " +(isCached ? "hit" : "miss")+ " for " +key );

return isCached;

}

/**
* Writes an object to the cache, will never expire
*
* @param key
*
* @param value
*
*/

public void write( final String key, final Object value ) {

write( key, value, -1 );

}

/**
* Writes an object to expire in the specified number of seconds
*
* @param key
* @param value
* @param timeout
*
*/

public synchronized void write( final String key, final Object value, final int timeout ) {

log.debug(
"Write key " +key+
( timeout != -1 ? " expires in " +timeout+ " seconds" : "" )
);

writeRaw( key, new CachedObject( value, timeout ) );

}

/**
* Tries to read an object from the cache, returns null on cache miss
*
* @param key
*
* @return
*
*/

public Object read( final String key ) {

final CachedObject object = readRaw( key );

return isCacheOk( object )
? object.getValue()
: null;

}

/**
* Indicates if an internal cache object is still fresh (might be null)
*
* @param object
*
* @return
*
*/

private boolean isCacheOk( final CachedObject object ) {

return ( object != null && !object.isExpired() );

}

/**
* Does a raw read on the internal data store, returns null on cache miss
*
* @param key
*
* @return
*
*/

abstract protected CachedObject readRaw( final String key );

/**
* Performs a raw write for a cached object
*
* @param key
* @param object
*
*/

abstract protected void writeRaw( final String key, final CachedObject object );

}

0 comments on commit 18fee9b

Please sign in to comment.