cachingprovider.java
来自「我想下载一个东西」· Java 代码 · 共 555 行 · 第 1/2 页
JAVA
555 行
{ 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 { m_provider.putPageText( page, text ); // Invalidate cache after writing to it. // FIXME: possible race condition here. Someone might still get // the old version. revalidatePage( page ); } // FIXME: This MUST be cached somehow. private boolean m_gotall = false; public Collection getAllPages() throws ProviderException { Collection all; if( m_gotall == false ) { all = m_provider.getAllPages(); // Make sure that all pages are in the cache. // FIXME: This has the unfortunate side effect of clearing // 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(); ) { all.add( ((CacheItem)i.next()).m_page ); } } return all; } // Null text for no page // Returns null if no page could be found. private synchronized CacheItem addPage( String pageName, String text ) throws ProviderException { CacheItem item = null; WikiPage newpage = m_provider.getPageInfo( pageName, WikiPageProvider.LATEST_VERSION ); if( newpage != null ) { item = new CacheItem(); item.m_page = newpage; if( text != null ) { m_textCache.putInCache( pageName, text ); } m_cache.put( pageName, item ); } return item; } public Collection getAllChangedSince( Date date ) { return m_provider.getAllChangedSince( date ); } public int getPageCount() throws ProviderException { return m_provider.getPageCount(); } public Collection findPages( QueryItem[] query ) { TreeSet res = new TreeSet( new SearchResultComparator() ); SearchMatcher matcher = new SearchMatcher( query ); Collection allPages = null; try { allPages = getAllPages(); } catch( ProviderException pe ) { log.error( "Unable to retrieve page list", pe ); return( null ); } Iterator it = allPages.iterator(); while( it.hasNext() ) { try { WikiPage page = (WikiPage) it.next(); String pageName = page.getName(); String pageContent = getTextFromCache( pageName ); SearchResult comparison = matcher.matchPageContent( pageName, pageContent ); if( comparison != null ) { res.add( comparison ); } } catch( ProviderException pe ) { log.error( "Unable to retrieve page from cache", pe ); } catch( IOException ioe ) { log.error( "Failed to search page", ioe ); } } return( res ); } public WikiPage getPageInfo( String page, int version ) throws ProviderException { CacheItem item = (CacheItem)m_cache.get( page ); int latestcached = (item != null) ? item.m_page.getVersion() : Integer.MIN_VALUE; if( version == WikiPageProvider.LATEST_VERSION || version == latestcached ) { if( item == null ) { item = addPage( page, null ); if( item == null ) { return null; } } return item.m_page; } else { // We do not cache old versions. return m_provider.getPageInfo( page, version ); } } public List getVersionHistory( String page ) throws ProviderException { return m_provider.getVersionHistory( page ); } public String getProviderInfo() { int cachedPages = 0; long totalSize = 0; /* for( Iterator i = m_cache.values().iterator(); i.hasNext(); ) { CacheItem item = (CacheItem) i.next(); String text = (String) item.m_text.get(); if( text != null ) { cachedPages++; totalSize += text.length()*2; } } totalSize = (totalSize+512)/1024L; */ return("Real provider: "+m_provider.getClass().getName()+ "<br />Cache misses: "+m_cacheMisses+ "<br />Cache hits: "+m_cacheHits); } public void deleteVersion( String pageName, int version ) throws ProviderException { // // Luckily, this is such a rare operation it is okay // to synchronize against the whole thing. // synchronized( this ) { CacheItem item = (CacheItem)m_cache.get( pageName ); int latestcached = (item != null) ? item.m_page.getVersion() : Integer.MIN_VALUE; // // If we have this version cached, remove from cache. // if( version == WikiPageProvider.LATEST_VERSION || version == latestcached ) { m_cache.remove( pageName ); } m_provider.deleteVersion( pageName, version ); } } public void deletePage( String pageName ) throws ProviderException { // // See note in deleteVersion(). // synchronized(this) { m_cache.remove( pageName ); m_provider.deletePage( pageName ); } } /** * Returns the actual used provider. * @since 2.0 */ public WikiPageProvider getRealProvider() { return m_provider; } private class CacheItem { WikiPage m_page; long m_lastChecked = 0L; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?