The Oscache that Lucene uses, record it.
Manipulating Cache Classes
Package com.jq.util;
Import Java.util.Date;
Import Com.opensymphony.oscache.base.CacheEntry;
Import Com.opensymphony.oscache.base.EntryRefreshPolicy;
Import com.opensymphony.oscache.base.NeedsRefreshException;
Import Com.opensymphony.oscache.general.GeneralCacheAdministrator;
/**
* @author
*/
public class Basecache extends Generalcacheadministrator {
Expiration time (in seconds);
private int refreshperiod;
keyword prefix character;
Private String Keyprefix;
Private static final long serialversionuid = -5437192926052145345l;
Public Basecache (String keyprefix, int refreshperiod) {
Super ();
This.keyprefix = Keyprefix;
This.refreshperiod = RefreshPeriod;
}
/**
* Add the cached object;
*/
public void put (String key, Object value) {
This.putincache (This.keyprefix + "_" + key, value);
}
/**
* Delete the cached object;
*/
public void Remove (String key) {
This.flushentry (This.keyprefix + "_" + key);
}
/**
* Delete all cached objects;
*/
public void RemoveAll (date date) {
This.flushall (date);
}
public void RemoveAll () {
This.flushall ();
}
/**
* Gets the cached object;
*/
Public Object get (String key) throws Exception {
try {
Return This.getfromcache (This.keyprefix + "_" + key,
This.refreshperiod);
} catch (Needsrefreshexception e) {
This.cancelupdate (This.keyprefix + "_" + key);
return null;
}
}
/**
*
* @param key
* @param refreshperiod
* @return objects that are cached
* @throws Exception
*/
Public Object get (String key, int refreshperiod) throws Exception {
try {
Return This.getfromcache (This.keyprefix + "_" + key, RefreshPeriod);
} catch (Needsrefreshexception e) {
This.cancelupdate (This.keyprefix + "_" + key);
return null;
}
}
}
Lucene Operation Oscache
Package com.jq.util;
public class Lucenecache {
private static Basecache Lucenecache = null;
private static String Keyprefix = "Lucenecache";
public static int times = 5;//per second
public static Basecache getinstance () {
if (Lucenecache = = null) {
Lucenecache = new Basecache (Keyprefix, times);
}
return lucenecache;
}
public static void Main (string[] args) {
Lucenecache = getinstance ();
Lucenecache.put ("Nihao", "Hello");
try {
System.out.println (Lucenecache.get ("Nihao"));
Thread.Sleep (4000);
System.out.println (Lucenecache.get ("Nihao"));
System.out.println (Lucenecache.get ("1"));
} catch (Exception e) {
E.printstacktrace ();
}
}
}
Copyright NOTICE: This article for Bo Master http://www.zuiniusn.com original article, without Bo Master permission not reproduced.
The Oscache that Lucene uses, record it.