Update of /cvsroot/roller/roller/src/org/roller/presentation/caching
In directory sc8-pr-cvs1:/tmp/cvs-serv30233/src/org/roller/presentation/caching
Added Files:
CommonsUDBCache.java DefaultUDBCache.java UDBCacheFactory.java
UserDisplayBeanCache.java
Log Message:
Added a "configurable" means to caching UserDisplayBeans. The cache
implementation can be specified in web.xml (web-settings.xml) but defaults to
DefaultUDBCache (a simple wrapper around a HashMap).
Some more work needs to be done on making the implementations allow
configuration information. Besides the Default implementation I've created one
for Commons Caching, which was added to web-settings.xml.
--- NEW FILE: CommonsUDBCache.java ---
package org.roller.presentation.caching;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.cache.Cache;
import org.apache.commons.cache.CacheSingleton;
import org.apache.commons.cache.LRUEvictionPolicy;
import org.apache.commons.cache.SimpleCache;
import org.apache.commons.cache.MemoryStash;
import org.roller.presentation.ServletLogger;
import org.roller.RollerException;
import org.roller.model.Roller;
import org.roller.model.UserData;
import org.roller.presentation.users.UserDisplayBean;
/**
* @author llavandowska
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class CommonsUDBCache implements UserDisplayBeanCache
{
private static CommonsUDBCache INSTANCE = new CommonsUDBCache();
private static String cacheName = "UserDisplayBean";
private int maxObjects = 500;
private long expireInterval = 1000l*60*30; // 1 second * 1 min * 1/2 hr
private ServletLogger logger = new ServletLogger();
/** Private constructor to prevent outside instantiation **/
private CommonsUDBCache() { };
/**
* Get Cache from Commons CacheSingleton.
* If one happens to not exist yet, make one.
*/
private synchronized Cache getCache()
{
if(CacheSingleton.hasCache(cacheName))
{
return CacheSingleton.getCache(cacheName);
}
SimpleCache cache = new SimpleCache(
new MemoryStash( maxObjects ), new
LRUEvictionPolicy() );
if(null != cache)
{
CacheSingleton.putCache(cacheName, cache);
return cache;
}
logger.log("Unable to get cache in CommonsUDBCache", 4);
return null;
}
/**
* @see
org.roller.presentation.caching.UserDisplayBeanCache#getInstance(ServletContext)
*/
public static UserDisplayBeanCache getInstance(ServletContext sc)
{
return INSTANCE;
}
/**
* @see
org.roller.presentation.caching.UserDisplayBeanCache#putIntoCache(UserData,
Roller, HttpServletRequest)
*/
public UserDisplayBean putIntoCache(
UserData user,
Roller roller,
HttpServletRequest req)
{
Cache cache = this.getCache();
try
{
UserDisplayBean bean = new UserDisplayBean( user,
roller, req );
cache.store(user.getId(), bean,
new Long(System.currentTimeMillis() +
expireInterval),
null);
return bean;
}
catch (RollerException e)
{
logger.log("Error storing new UserDisplayBean", e);
}
return null;
}
/**
* @see
org.roller.presentation.caching.UserDisplayBeanCache#getFromCache(UserData,
Roller, HttpServletRequest)
*/
public UserDisplayBean getFromCache(
UserData user,
Roller roller,
HttpServletRequest req)
{
Cache cache = this.getCache();
if (cache == null) return null;
UserDisplayBean bean = (UserDisplayBean)
cache.retrieve(user.getId());
if (bean == null)
{
bean = putIntoCache( user, roller, req );
}
return bean;
}
/**
* @see
org.roller.presentation.caching.UserDisplayBeanCache#removeFromCache(UserData)
*/
public void removeFromCache(UserData user)
{
Cache cache = this.getCache();
cache.clear( user.getId() );
}
}
--- NEW FILE: DefaultUDBCache.java ---
package org.roller.presentation.caching;
import java.util.HashMap;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.roller.presentation.ServletLogger;
import org.roller.RollerException;
import org.roller.model.Roller;
import org.roller.model.UserData;
import org.roller.presentation.users.UserDisplayBean;
/**
* @author llavandowska
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class DefaultUDBCache implements UserDisplayBeanCache
{
private static DefaultUDBCache INSTANCE = new DefaultUDBCache();
private HashMap cache = new HashMap();
private ServletLogger logger = new ServletLogger();
/** Private constructor to prevent outside instantiation **/
private DefaultUDBCache() { };
/**
* @see
org.roller.presentation.caching.UserDisplayBeanCache#getInstance(ServletContext)
*/
public static UserDisplayBeanCache getInstance(ServletContext sc)
{
return INSTANCE;
}
/**
* @see
org.roller.presentation.caching.UserDisplayBeanCache#putIntoCache(UserData,
Roller, HttpServletRequest)
*/
public UserDisplayBean putIntoCache(
UserData user,
Roller roller,
HttpServletRequest req)
{
try
{
UserDisplayBean bean = new UserDisplayBean( user,
roller, req );
cache.put( user.getId(), bean );
return bean;
}
catch (RollerException e)
{
logger.log("Error creating new UserDisplayBean", e);
}
return null;
}
/**
* @see
org.roller.presentation.caching.UserDisplayBeanCache#getFromCache(UserData,
Roller, HttpServletRequest)
*/
public UserDisplayBean getFromCache(
UserData user,
Roller roller,
HttpServletRequest req)
{
if (cache.get( user.getId() ) != null)
{
logger.log("Found in cache: " + user.getId(), 0);
return (UserDisplayBean)cache.get(user.getId());
}
try
{
logger.log("Not found in cache: " + user.getId(), 0);
UserDisplayBean bean = new UserDisplayBean( user,
roller, req );
cache.put( user.getId(), bean );
return bean;
}
catch (RollerException e)
{
logger.log("Error creating new UserDisplayBean", e);
}
return null;
}
/**
* @see
org.roller.presentation.caching.UserDisplayBeanCache#removeFromCache(UserData)
*/
public void removeFromCache(UserData user)
{
logger.log("Removing " + user.getId() + " from
DefaultUDBCache", 0);
cache.remove(user.getId());
}
}
--- NEW FILE: UDBCacheFactory.java ---
package org.roller.presentation.caching;
/**
* @author llavandowska
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public class UDBCacheFactory
{
private static UserDisplayBeanCache mCache;
public static UserDisplayBeanCache getCache()
{
return mCache;
}
public static void setCache(UserDisplayBeanCache cache)
{
mCache = cache;
}
}
--- NEW FILE: UserDisplayBeanCache.java ---
package org.roller.presentation.caching;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.roller.model.Roller;
import org.roller.model.UserData;
import org.roller.presentation.users.UserDisplayBean;
/**
* @author llavandowska
*
* To change this generated comment edit the template variable "typecomment":
* Window>Preferences>Java>Templates.
* To enable and disable the creation of type comments go to
* Window>Preferences>Java>Code Generation.
*/
public interface UserDisplayBeanCache
{
/**
* Method getInstance.
* @param sc
* @return UserDisplayBeanCache
*/
// How do you declare a static method in an Interface ??
//public abstract UserDisplayBeanCache getInstance(ServletContext sc);
/**
* Method putIntoCache.
* @param user
* @param roller
* @param req
* @return UserDisplayBean
*/
public UserDisplayBean putIntoCache(UserData user, Roller roller,
HttpServletRequest req);
/**
* Method getFromCache.
* @param user
* @param roller
* @param req
* @return UserDisplayBean
*/
public UserDisplayBean getFromCache(UserData user, Roller roller,
HttpServletRequest req);
/**
* Method removeFromCache.
* @param user
*/
public void removeFromCache(UserData user);
}
-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf
|