Lucene IndexReader,IndexWriter,IndexSearcher 缓存应用
1.IndexManager类,用于提供IndexReader,IndexWriter,IndexSearcher获取接口
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
publicclass IndexManager{
/**
*所有writer公共配置
*/
privatestaticfinal IndexWriterConfig iwc = new IndexWriterConfig(Version. LUCENE_44, newStandardAnalyzer(Version. LUCENE_44));
static {
iwc.setOpenMode(OpenMode. CREATE_OR_APPEND);
iwc.setRAMBufferSizeMB(20.0);
iwc.setMaxBufferedDocs(10000);
}
private Directory dir;
private IndexReader reader;
private IndexSearcher searcher;
private IndexWriter writer;
/**
*构造函数
* @paramindexPath
*/
public IndexManager(String indexPath) {
init( new File(indexPath));
}
/**
*构造函数
* @paramindexPath
*/
public IndexManager(File dirFile) {
init(dirFile);
}
privatevoid init(File dirFile) {
try {
dir = FSDirectory. open(dirFile);
//根据Directory对象,初始化indexReader对象
ReaderManager. getInstance().createIndexReader(dir);
//初始化writer对象
writer = new IndexWriter(dir, iwc);
} catch (IOException e) {
e.printStackTrace();
}
}
public IndexSearcher getSearcher() {
IndexReader ir = ReaderManager. getInstance().getIndexReader(dir);
if(reader == null || reader != ir)
{
reader = ir;
searcher = new IndexSearcher(reader);
}
returnsearcher;
}
public IndexWriter getWriter() {
returnwriter;
}
publicvoid commitWriter()
{
try {
writer.commit();
} catch (IOException e) {
rollback();
}
}
privatevoid rollback() {
try {
writer.rollback();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
2.ReaderManager类,用于提供IndexReader生命周期管理
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Map.Entry;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.store.Directory;
/**
*IndexReader生命周期,以及改变后reopen管理类
* @authoradmin
*
*/
publicclass ReaderManager {
/**
*reader回收Map
*/
privatestaticfinal Map<DirectoryReader, Long> recyleReaderMap = new HashMap<DirectoryReader, Long>();
/**
*oldreader回收最大生命周期
*/
privatestaticfinalint oldReaderMaxLifeTime = 60 * 1000;
privatestaticfinal Timer readerRefereshTimer = new Timer();
privatestaticfinal Map<Directory, DirectoryReader> readerMap = newHashMap<Directory, DirectoryReader>();
privatestaticfinal ReaderManager manager = new ReaderManager();
publicstaticfinalsynchronized ReaderManager getInstance()
{
return manager;
}
/**
*创建indexReader并放缓存
* @paramreader
*/
publicsynchronizedvoid createIndexReader(Directory dir)
{
try {
readerMap.put(dir, DirectoryReader. open(dir));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*获取IndexReader
* @paramdir
* @return
*/
public IndexReader getIndexReader(Directory dir)
{
return readerMap.get(dir);
}
static
{
readerRefereshTimer.schedule( new TimerTask(){
publicvoid run() {
//判断处理reader是否改变
for (Entry<Directory, DirectoryReader> entry : newHashMap<Directory, DirectoryReader>( readerMap).entrySet()) {
try {
DirectoryReader oldReader = entry.getValue();
DirectoryReader newReader = DirectoryReader. openIfChanged(oldReader);
if(newReader != null)
{
//替换旧reader对象
readerMap.put(entry.getKey(), newReader);
//放入回收MAP中
recyleReaderMap.put(oldReader, System. currentTimeMillis());
}
} catch (IOException e) {
e.printStackTrace();
}
}
//处理old reader回收
for (Entry<DirectoryReader, Long> entry : newHashMap<DirectoryReader, Long>( recyleReaderMap).entrySet()) {
if(System. currentTimeMillis() - entry.getValue()> oldReaderMaxLifeTime)
{
try {
entry.getKey().close();
} catch (IOException e) {
e.printStackTrace();
} finally {
recyleReaderMap.remove(entry.getKey());
}
}
}
}
}, 5 * 1000, 5 * 1000);
}
}