📄 cachingprovider.java
字号:
if( pageName == null ) return null; if( version == WikiPageProvider.LATEST_VERSION ) { result = getTextFromCache( pageName ); } else { WikiPage p = getPageInfoFromCache( pageName ); // // Or is this the latest version fetched by version number? // if( p != null && p.getVersion() == version ) { result = getTextFromCache( pageName ); } else { result = m_provider.getPageText( pageName, version ); } } return result; } /** * @throws RepositoryModifiedException If the page has been externally modified. */ private String getTextFromCache( String pageName ) throws ProviderException, RepositoryModifiedException { String text; boolean wasUpdated = false; if( pageName == null ) return null; WikiPage page = getPageInfoFromCache( pageName ); try { text = (String)m_textCache.getFromCache( pageName, m_pageContentExpiryPeriod ); wasUpdated = true; if( text == null ) { if( page != null ) { text = m_provider.getPageText( pageName, WikiPageProvider.LATEST_VERSION ); m_textCache.putInCache( pageName, text ); m_cacheMisses++; } else { return null; } } else { m_cacheHits++; } } catch( NeedsRefreshException e ) { if( pageExists(pageName) ) { text = m_provider.getPageText( pageName, WikiPageProvider.LATEST_VERSION ); m_textCache.putInCache( pageName, text ); wasUpdated = true; m_cacheMisses++; } else { m_textCache.putInCache( pageName, null ); wasUpdated = true; return null; // No page exists } } finally { if( !wasUpdated ) m_textCache.cancelUpdate(pageName); } return text; } /** * {@inheritDoc} */ public void putPageText( WikiPage page, String text ) throws ProviderException { synchronized(this) { m_provider.putPageText( page, text ); page.setLastModified( new Date() ); // Refresh caches properly m_cache.removeEntry( page.getName() ); m_textCache.removeEntry( page.getName() ); m_historyCache.removeEntry( page.getName() ); m_negCache.removeEntry( page.getName() ); // Refresh caches try { getPageInfoFromCache( page.getName() ); } catch(RepositoryModifiedException e) {} // Expected } } /** * {@inheritDoc} */ 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(); ) { WikiPage p = (WikiPage) i.next(); m_cache.putInCache( p.getName(), p ); // Requests for this page are now no longer denied m_negCache.putInCache( p.getName(), null ); } m_gotall = true; } } else { all = m_allCollector.getAllItems(); } return all; } /** * {@inheritDoc} */ public Collection getAllChangedSince( Date date ) { return m_provider.getAllChangedSince( date ); } /** * {@inheritDoc} */ public int getPageCount() throws ProviderException { return m_provider.getPageCount(); } /** * {@inheritDoc} */ public Collection findPages( QueryItem[] query ) { // // If the provider is a fast searcher, then // just pass this request through. // return m_provider.findPages( query ); // FIXME: Does not implement fast searching } // // FIXME: Kludge: make sure that the page is also parsed and it gets all the // necessary variables. // private void refreshMetadata( WikiPage page ) { if( page != null && !page.hasMetadata() ) { RenderingManager mgr = m_engine.getRenderingManager(); try { String data = m_provider.getPageText(page.getName(), page.getVersion()); WikiContext ctx = new WikiContext( m_engine, page ); MarkupParser parser = mgr.getParser( ctx, data ); parser.parse(); } catch( Exception ex ) { log.debug("Failed to retrieve variables for wikipage "+page); } } } /** * {@inheritDoc} */ public WikiPage getPageInfo( String pageName, int version ) throws ProviderException, RepositoryModifiedException { WikiPage page = null; WikiPage cached = getPageInfoFromCache( pageName ); int latestcached = (cached != null) ? cached.getVersion() : Integer.MIN_VALUE; if( version == WikiPageProvider.LATEST_VERSION || version == latestcached ) { if( cached == null ) { WikiPage data = m_provider.getPageInfo( pageName, version ); if( data != null ) { m_cache.putInCache( pageName, data ); // Requests for this page are now no longer denied m_negCache.putInCache( pageName, null ); } page = data; } else { page = cached; } } else { // We do not cache old versions. page = m_provider.getPageInfo( pageName, version ); //refreshMetadata( page ); } refreshMetadata( page ); return page; } /** * {@inheritDoc} */ public List getVersionHistory( String pageName ) throws ProviderException { List history = null; boolean wasUpdated = false; if( pageName == null ) return null; try { history = (List)m_historyCache.getFromCache( pageName, m_expiryPeriod ); log.debug("History cache hit for page "+pageName); m_historyCacheHits++; wasUpdated = true; } catch( NeedsRefreshException e ) { history = m_provider.getVersionHistory( pageName ); m_historyCache.putInCache( pageName, history ); log.debug("History cache miss for page "+pageName); m_historyCacheMisses++; wasUpdated = true; } finally { if( !wasUpdated ) m_historyCache.cancelUpdate( pageName ); } return history; } /** * {@inheritDoc} */ public synchronized String getProviderInfo() { return "Real provider: "+m_provider.getClass().getName()+ ". Cache misses: "+m_cacheMisses+ ". Cache hits: "+m_cacheHits+ ". History cache hits: "+m_historyCacheHits+ ". History cache misses: "+m_historyCacheMisses+ ". Cache consistency checks: "+m_expiryPeriod+"s"; } /** * {@inheritDoc} */ 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 ) { WikiPage cached = getPageInfoFromCache( pageName ); int latestcached = (cached != null) ? cached.getVersion() : Integer.MIN_VALUE; // // If we have this version cached, remove from cache. // if( version == WikiPageProvider.LATEST_VERSION || version == latestcached ) { m_cache.removeEntry( pageName ); m_textCache.removeEntry( pageName ); m_historyCache.removeEntry( pageName ); } m_provider.deleteVersion( pageName, version ); } } /** * {@inheritDoc} */ public void deletePage( String pageName ) throws ProviderException { // // See note in deleteVersion(). // synchronized(this) { m_cache.putInCache( pageName, null ); m_textCache.putInCache( pageName, null ); m_historyCache.putInCache( pageName, null ); m_negCache.putInCache( pageName, pageName ); m_provider.deletePage( pageName ); } } /** * {@inheritDoc} */ public void movePage( String from, String to ) throws ProviderException { m_provider.movePage( from, to ); synchronized(this) { // Clear any cached version of the old page log.debug("Removing from page "+from+" from cache");// m_cache.removeEntry( from ); m_cache.putInCache( from, null ); m_textCache.putInCache( from, null ); m_historyCache.putInCache( from, null ); m_negCache.putInCache( from, from ); // Clear the cache for the to page, if that page already exists //if ( m_cache.get( to ) != null ) //{ log.debug("Removing to page "+to+" from cache");// m_cache.removeEntry( to ); m_cache.putInCache( to, null ); m_textCache.putInCache( to, null ); m_historyCache.putInCache( to, null ); m_negCache.putInCache( to, to ); //} } } /** * Returns the actual used provider. * @since 2.0 * @return The real provider. */ public WikiPageProvider getRealProvider() { return m_provider; } /** * This is a simple class that keeps a list of all WikiPages that * we have in memory. Because the OSCache cannot give us a list * of all pages currently in cache, we'll have to check this * ourselves. * * * @since 2.4 */ private static class CacheItemCollector implements CacheEntryEventListener { private Map<String, WikiPage> m_allItems = new Hashtable<String, WikiPage>(); /** * Returns a clone of the set - you cannot manipulate this. * * @return A Set of WikiPage objects. */ public Set getAllItems() { Set<WikiPage> ret = new TreeSet<WikiPage>(); ret.addAll(m_allItems.values()); return ret; } public void cacheEntryAdded( CacheEntryEvent arg0 ) { cacheEntryUpdated( arg0 ); } public void cachePatternFlushed( CachePatternEvent ev ) { } public void cacheGroupFlushed( CacheGroupEvent ev ) { } public void cacheFlushed( CachewideEvent ev ) { } public void cacheEntryFlushed( CacheEntryEvent arg0 ) { cacheEntryRemoved( arg0 ); } public void cacheEntryRemoved( CacheEntryEvent arg0 ) { if( arg0.getEntry() != null ) { WikiPage item = (WikiPage) arg0.getEntry().getContent(); if( item != null ) { m_allItems.remove( item.getName() ); } } } public void cacheEntryUpdated( CacheEntryEvent arg0 ) { WikiPage item = (WikiPage) arg0.getEntry().getContent(); if( item != null ) { // Item added or replaced. m_allItems.put( item.getName(), item ); } else { // Removed item // FIXME: If the page system is changed during this time, we'll just fail gracefully m_allItems.remove( arg0.getKey() ); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -