结合ehcache缓存对lucene使用单例模式搜索
我采用和ehcache缓存结合使用单例模式,其实就是给indexsearcher加了个缓存
使用ehcache在内存中设置一个缓存,时间为10分钟。
当IndexSearcher对象不存在时,创建静态对象同时在缓存中写入一个值,表示对象已经存在了.
搜索时如果缓存过期,则重新创建IndexSearcherpublic class MySearcher { private IndexSearcher searcher = null; private static MySearcher mySearcher = null; public static MySearcher getInstance() throws IOException{ CacheManager manager = CacheManager.getInstance(); net.sf.ehcache.Cache cache = manager.getCache("seacher_cache"); String seacherKey = "Searcher_Id"; net.sf.ehcache.Element searchElement = cache.get(seacherKey); if (mySearcher==null || searchElement==null){ mySearcher = new MySearcher(); searchElement = new net.sf.ehcache.Element(seacherKey,1); cache.put(searchElement); } return mySearcher; } public MySearcher() throws IOException{ this.searcher = new IndexSearcher("d:/index"); } }
相关推荐
幸运小侯子 2020-08-14
haidaoxianzi 2020-07-16
jimgreatly 2020-05-25
dongCSDN 2020-05-08
小鱿鱼 2020-02-12
javamagicsun 2019-12-17
neweastsun 2019-12-02
GavinZhera 2019-11-06
wangxiaoxue 2019-10-20
Justagreenonion 2019-10-22
xieronghua 2019-10-11
lxpandsq 2017-11-18
xtiawxf 2019-04-25
fuel 2016-04-29
奶牛老爹 2016-04-24
zhongjcbill 2015-09-02
编程点滴 2014-12-08
MichaelHsu 2014-08-11
SolitudeSky 2018-03-23