Lucene用到的oscache,记录下来
2012-05-17 08:40
330 查看
Lucene用到的oscache,记录下来
操作缓存类
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 {
// 过期时间(单位为秒);
private int refreshPeriod;
// 关键字前缀字符;
private String keyPrefix;
private static final long serialVersionUID = -5437192926052145345L;
public BaseCache(String keyPrefix, int refreshPeriod) {
super();
this.keyPrefix = keyPrefix;
this.refreshPeriod = refreshPeriod;
}
/**
* 添加被缓存的对象;
*/
public void put(String key, Object value) {
this.putInCache(this.keyPrefix + "_" + key, value);
}
/**
* 删除被缓存的对象;
*/
public void remove(String key) {
this.flushEntry(this.keyPrefix + "_" + key);
}
/**
* 删除所有被缓存的对象;
*/
public void removeAll(Date date) {
this.flushAll(date);
}
public void removeAll() {
this.flushAll();
}
/**
* 获取被缓存的对象;
*/
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 被缓存的对象
* @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 操作oscache
package com.jq.util;
public class LuceneCache {
private static BaseCache luceneCache = null;
private static String keyPrefix = "lucenecache";
public static int times = 5;//单位秒
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", "你好");
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();
}
}
}
操作缓存类
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 {
// 过期时间(单位为秒);
private int refreshPeriod;
// 关键字前缀字符;
private String keyPrefix;
private static final long serialVersionUID = -5437192926052145345L;
public BaseCache(String keyPrefix, int refreshPeriod) {
super();
this.keyPrefix = keyPrefix;
this.refreshPeriod = refreshPeriod;
}
/**
* 添加被缓存的对象;
*/
public void put(String key, Object value) {
this.putInCache(this.keyPrefix + "_" + key, value);
}
/**
* 删除被缓存的对象;
*/
public void remove(String key) {
this.flushEntry(this.keyPrefix + "_" + key);
}
/**
* 删除所有被缓存的对象;
*/
public void removeAll(Date date) {
this.flushAll(date);
}
public void removeAll() {
this.flushAll();
}
/**
* 获取被缓存的对象;
*/
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 被缓存的对象
* @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 操作oscache
package com.jq.util;
public class LuceneCache {
private static BaseCache luceneCache = null;
private static String keyPrefix = "lucenecache";
public static int times = 5;//单位秒
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", "你好");
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();
}
}
}
相关文章推荐
- Lucene用到的oscache,记录下来
- Lucene用到的oscache,记录下来
- Lucene用到的oscache,记录下来
- 在网上找的一个Flex文件上传代码,记录下来,以后用到的时候可以马上能用:
- 关于orcale的数据库脚本,记录下来,方便自己以后用到查找
- JNI技术-----环境搭建(记录下来,以后可能会用到)
- eclipse swing 环境配置,其实就是dotnet 中的winform 开发,记录下来,给需要学习的人使用,
- 将一些想法记录下来以供以后回忆的地方
- PHP 个人用到的琐碎代码记录
- mysql 索引。看别人的文章,感觉很有用,记录下来
- JBoss-5.1.0.GA配置连接池再次出错,随记录下来以备后用。
- 在CSDN一哥们的Blog中发现一句话感觉深有感触,记录下来先--
- 关于viewPager matrix 计算 ,现在记录下来
- 编程的真正奥义在于,把人类的思维、设计、语言、逻辑和精神创造以一种计算机可以识别和储存的方式记录下来。
- 常用用的header状态值,记录下来以免忘记
- 不成功的MVC Repository模式,记录下来,后面看看原因在哪里(三) IDevTypeRepository 及 DevTypeRepository
- SQL高级查询——50句查询(含答案) ---参考别人的,感觉很好就记录下来留着自己看。
- 前几天参加了阿里巴巴的电话面试,把他们问的问题都记录了下来,希望对那些正准备面试的人有用。
- 多写Blog,把遇到的问题记录下来
- 启动tomcat的一些info报错,目前还没有解决办法,先记录下来。