M
Maciej Boniewicz
Hello! I have problem with TreeCache. I wrote simpe class to Caching values
based on TreeCache, and it works, but very often
it looses values(there are ie empty Strings as values (no null values!!!)).
Thanks for any help. Here is my cache class:
package utils;
import java.util.*;
import org.jboss.cache.*;
import org.apache.log4j.*;
public class Cache {
static Logger logger = Logger.getLogger(Cache.class);
private static HashMap lastAccesses = new HashMap();
private static int life_time=0;
private static TreeCache jtr = null;
private static void init()
{
if(jtr==null)
{
ResourceBundle data;
String xml_path="";
try
{
data = ResourceBundle.getBundle("Consts");
xml_path=data.getString("cache_xml_path");
life_time=Integer.parseInt(data.getString("cache_life_time"))*1000;
jtr = new TreeCache();
org.jboss.cache.PropertyConfigurator config = new
org.jboss.cache.PropertyConfigurator();
config.configure(jtr, xml_path);
jtr.startService();
}catch(Exception e)
{
logger.debug(e.toString());
}
}
}
public static Object getObject(String object_name)
{
init();
if(lastAccesses.containsKey(object_name))
{
if(new
Date().getTime()-((Date)lastAccesses.get(object_name)).getTime()>=life_time)
return null;
}else
return null;
try
{
if(jtr.exists("/",(Object)object_name))
return (Object)jtr.get("/",(Object)object_name);
else
return null;
}catch(Exception e)
{
logger.debug(e.toString());
}
return null;
}
public static void setObject(String object_name,Object obj)
{
Object oldobj;
init();
try
{
oldobj=jtr.put("/",(Object)object_name,obj);
if(oldobj!=null)
oldobj=null;
oldobj=lastAccesses.put(object_name,(Object)new Date());
if(oldobj!=null)
oldobj=null;
}catch(Exception e)
{
logger.debug(e.toString());
}
}
}
based on TreeCache, and it works, but very often
it looses values(there are ie empty Strings as values (no null values!!!)).
Thanks for any help. Here is my cache class:
package utils;
import java.util.*;
import org.jboss.cache.*;
import org.apache.log4j.*;
public class Cache {
static Logger logger = Logger.getLogger(Cache.class);
private static HashMap lastAccesses = new HashMap();
private static int life_time=0;
private static TreeCache jtr = null;
private static void init()
{
if(jtr==null)
{
ResourceBundle data;
String xml_path="";
try
{
data = ResourceBundle.getBundle("Consts");
xml_path=data.getString("cache_xml_path");
life_time=Integer.parseInt(data.getString("cache_life_time"))*1000;
jtr = new TreeCache();
org.jboss.cache.PropertyConfigurator config = new
org.jboss.cache.PropertyConfigurator();
config.configure(jtr, xml_path);
jtr.startService();
}catch(Exception e)
{
logger.debug(e.toString());
}
}
}
public static Object getObject(String object_name)
{
init();
if(lastAccesses.containsKey(object_name))
{
if(new
Date().getTime()-((Date)lastAccesses.get(object_name)).getTime()>=life_time)
return null;
}else
return null;
try
{
if(jtr.exists("/",(Object)object_name))
return (Object)jtr.get("/",(Object)object_name);
else
return null;
}catch(Exception e)
{
logger.debug(e.toString());
}
return null;
}
public static void setObject(String object_name,Object obj)
{
Object oldobj;
init();
try
{
oldobj=jtr.put("/",(Object)object_name,obj);
if(oldobj!=null)
oldobj=null;
oldobj=lastAccesses.put(object_name,(Object)new Date());
if(oldobj!=null)
oldobj=null;
}catch(Exception e)
{
logger.debug(e.toString());
}
}
}