History of CachedMap

HomePage | RecentChanges | Preferences


Revision 3 . . December 21, 2019 12:33 pm by 78-93-98-198.dsl.wavetel.us
Revision 2 . . July 20, 2005 4:29 am by WendySmoak
  

Difference (from prior major revision) (no other diffs)

Changed: 1,104c1

package edu.asu.vpia.benweb.dao;

import asjava.uniclientlibs.*;
import asjava.uniobjects.*;

import edu.asu.vpia.dao.UniSessionFactory;
import edu.asu.vpia.dao.dg.Entity;
import edu.asu.vpia.dao.dg.EntityImpl;
import edu.asu.vpia.dao.dg.EntityDAO;
import edu.asu.vpia.dao.dg.DemographicDAOFactory;

import java.util.AbstractMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* Experimental Cache-backed Map of Profiles
*
*@author Wendy Smoak (wsmoak@asu.edu)
*@created April 27, 2005
*/
public class ProfileMap extends AbstractMap
{

private static Log log = LogFactory.getLog( ProfileMap.class );
private static final String CACHE_NAME = "profiles";

public Set entrySet()
{
try {
Cache cache = CacheManager.getInstance().getCache( CACHE_NAME );
return new java.util.TreeSet( cache.getKeys() );
} catch ( CacheException ex ) {
log.error( "entrySet: problem retrieving cache: " + ex.getMessage() );
log.error( ex );
return null;
}

}


public Object get( Object key )
{
if( key == null || "".equals( key.toString() ) ) {
return null;
}

//look in the Cache, if found return the Object mapped to this key
Profile profile = null;
String code = key.toString();
try {
Cache cache = CacheManager.create().getCache( CACHE_NAME );
log.debug( "get: cache is " + cache );
Element e = cache.get( code );
if ( e != null ) {
profile = (Profile) e.getValue();
}
//if not, retrieve it from the database, cache it, then return it
if ( profile == null ) {
try {
ProfileDAO profileDAO = BenWebDAOFactory.getDAOFactory().getProfileDAO();
profile = profileDAO.read( code );
} catch ( Exception ex ) {
log.error( "get: problem reading profile " + key );
log.error( ex );
}
cache.put( new Element( code, (ProfileImpl) profile ) );
}
} catch ( CacheException ex ) {
log.error( "get: problem retrieving cache: " + ex.getMessage() );
log.error( ex );
return null;
}

return profile;
}


public String toString()
{
String answer = "profile cache";
try {
Cache cache = CacheManager.create().getCache( "profiles" );
List list = cache.getKeys();
answer = list.toString();
} catch ( CacheException ex ) {
log.error( ex );
}
return answer;
}

}


https://github.com/wendysmoak/wiki/wiki/CachedMap

HomePage | RecentChanges | Preferences
Search: