📄 cachingprovider.java
字号:
doc.add(Field.Text(LUCENE_PAGE_CONTENTS, new StringReader(text + " " + TextUtil.beautifyString(page.getName())))); writer.addDocument(doc); } public boolean pageExists( String page ) { CacheItem item = (CacheItem)m_cache.get( page ); /* // FIXME: This section is commented out because it makes // some tests run. Probably should be removed before release. if( checkIfPageChanged( item ) ) { try { revalidatePage( item.m_page ); } catch( ProviderException e ) {} // FIXME: Should do something! return m_provider.pageExists( page ); } */ // // A null item means that the page either does not // exist, or has not yet been cached; a non-null // means that the page does exist. // if( item != null ) { return true; } // // If we have a list of all pages in memory, then any page // not in the cache must be non-existent. // // FIXME: There's a problem here; if someone modifies the // repository by adding a page outside JSPWiki, // we won't notice it. if( m_gotall ) { return false; } // // We could add the page to the cache here as well, // but in order to understand whether that is a // good thing or not we would need to analyze // the JSPWiki calling patterns extensively. Presumably // it would be a good thing if pageExists() is called // many times before the first getPageText() is called, // and the whole page is cached. // return m_provider.pageExists( page ); } /** * @throws RepositoryModifiedException If the page has been externally modified. */ public String getPageText( String page, int version ) throws ProviderException { String result = null; if( version == WikiPageProvider.LATEST_VERSION ) { if( pageExists( page ) ) { result = getTextFromCache( page ); } } else { CacheItem item = (CacheItem)m_cache.get( page ); // // Or is this the latest version fetched by version number? // if( item != null && item.m_page.getVersion() == version ) { result = getTextFromCache( page ); } else { result = m_provider.getPageText( page, version ); } } return result; } /** * Returns true, if the page has been changed outside of JSPWiki. */ private boolean checkIfPageChanged( CacheItem item ) { if( item == null ) return false; long currentTime = System.currentTimeMillis(); if( currentTime - item.m_lastChecked > m_milliSecondsBetweenChecks ) { // log.debug("Consistency check: has page "+item.m_page.getName()+" been changed?"); try { WikiPage cached = item.m_page; WikiPage current = m_provider.getPageInfo( cached.getName(), LATEST_VERSION ); // // Page has been deleted. // if( current == null ) { log.debug("Page "+cached.getName()+" has been removed externally."); return true; } item.m_lastChecked = currentTime; long epsilon = 1000L; // FIXME: This should be adjusted according to provider granularity. Date curDate = current.getLastModified(); Date cacDate = cached.getLastModified(); // log.debug("cached date = "+cacDate+", current date = "+curDate); if( curDate != null && cacDate != null && curDate.getTime() - cacDate.getTime() > epsilon ) { log.debug("Page "+current.getName()+" has been externally modified, refreshing contents."); return true; } } catch( ProviderException e ) { log.error("While checking cache, got error: ",e); } } return false; } /** * Removes the page from cache, and attempts to reload all information. */ private synchronized void revalidatePage( WikiPage page ) throws ProviderException { m_cache.remove( page.getName() ); m_textCache.flushEntry( page.getName() ); m_historyCache.flushEntry( page.getName() ); addPage( page.getName(), null ); // If fetch fails, we want info to go directly to user } /** * @throws RepositoryModifiedException If the page has been externally modified. */ private String getTextFromCache( String page ) throws ProviderException { CacheItem item; synchronized(this) { item = (CacheItem)m_cache.get( page ); } // // Check if page has been changed externally. If it has, then // we need to refresh all of the information. // if( checkIfPageChanged( item ) ) { revalidatePage( item.m_page ); throw new RepositoryModifiedException( page ); } if( item == null ) { // Page has never been seen. // log.debug("Page "+page+" never seen."); String text = m_provider.getPageText( page, WikiPageProvider.LATEST_VERSION ); addPage( page, text ); m_cacheMisses++; return text; } else { String text; try { text = (String)m_textCache.getFromCache( page, m_refreshPeriod ); } catch( NeedsRefreshException e ) { text = m_provider.getPageText( page, WikiPageProvider.LATEST_VERSION ); m_textCache.putInCache( page, text ); m_cacheMisses++; return text; } // log.debug("Page "+page+" found in cache."); m_cacheHits++; return text; } } public void putPageText( WikiPage page, String text ) throws ProviderException { synchronized(this) { if( m_useLucene ) { // Add work item to m_updates queue. Object[] pair = new Object[2]; pair[0] = page; pair[1] = text; m_updates.add(pair); } m_provider.putPageText( page, text ); revalidatePage( page ); } } private synchronized void updateLuceneIndex( WikiPage page, String text ) { IndexWriter writer = null; log.info("Updating Lucene index for page '" + page.getName() + "'..."); try { deleteFromLucene(page); // Now add back the new version. writer = new IndexWriter(m_luceneDirectory, new StandardAnalyzer(), false); luceneIndexPage(page, text, writer); m_updateCount++; if( m_updateCount >= LUCENE_OPTIMIZE_COUNT ) { writer.optimize(); m_updateCount = 0; } } catch ( IOException e ) { log.error("Unable to update page '" + page.getName() + "' from Lucene index", e); } finally { try { if( writer != null ) writer.close(); } catch( IOException e ) {} } log.info("Done updating Lucene index for page '" + page.getName() + "'."); } private void deleteFromLucene( WikiPage page ) { try { // Must first remove existing version of page. IndexReader reader = IndexReader.open(m_luceneDirectory); reader.delete(new Term(LUCENE_ID, page.getName())); reader.close(); } catch ( IOException e ) { log.error("Unable to update page '" + page.getName() + "' from Lucene index", e); } } public Collection getAllPages() throws ProviderException { Collection all; if( m_gotall == false ) { all = m_provider.getAllPages(); // Make sure that all pages are in the cache. synchronized(this) { for( Iterator i = all.iterator(); i.hasNext(); ) { CacheItem item = new CacheItem(); item.m_page = (WikiPage) i.next(); item.m_lastChecked = System.currentTimeMillis(); m_cache.put( item.m_page.getName(), item ); } m_gotall = true; } } else { all = new ArrayList(); for( Iterator i = m_cache.values().iterator(); i.hasNext(); ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -