The Oscache that Lucene uses, record it.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.