📄 cachingattachmentprovider.java
字号:
{ Attachment att = (Attachment) i.next(); if( name.equals( att.getFileName() ) ) { return att; } } return null; } /** * Refreshes the cache content and updates counters. * * @return The newly fetched object from the provider. */ @SuppressWarnings("unchecked") private final Collection<Attachment> refresh( WikiPage page ) throws ProviderException { m_cacheMisses++; Collection<Attachment> c = m_provider.listAttachments( page ); m_cache.putInCache( page.getName(), c ); return c; } /** * {@inheritDoc} */ public Attachment getAttachmentInfo( WikiPage page, String name, int version ) throws ProviderException { if( log.isDebugEnabled() ) { log.debug("Getting attachments for "+page+", name="+name+", version="+version); } // // We don't cache previous versions // if( version != WikiProvider.LATEST_VERSION ) { log.debug("...we don't cache old versions"); return m_provider.getAttachmentInfo( page, name, version ); } try { Collection c = (Collection)m_cache.getFromCache( page.getName(), m_refreshPeriod ); if( c == null ) { log.debug("...wasn't in the cache"); c = refresh( page ); if( c == null ) return null; // No such attachment } else { log.debug("...FOUND in the cache"); m_cacheHits++; } return findAttachmentFromCollection( c, name ); } catch( NeedsRefreshException nre ) { log.debug("...needs refresh"); Collection c = null; try { c = refresh( page ); } catch( Exception ex ) { log.warn("Provider failed, returning cached content",ex); m_cache.cancelUpdate( page.getName() ); c = (Collection)nre.getCacheContent(); } if( c != null ) { return findAttachmentFromCollection( c, name ); } } return null; } /** * {@inheritDoc} */ public List getVersionHistory( Attachment att ) { return m_provider.getVersionHistory( att ); } /** * {@inheritDoc} */ public void deleteVersion( Attachment att ) throws ProviderException { // This isn't strictly speaking correct, but it does not really matter m_cache.removeEntry( att.getParentName() ); m_provider.deleteVersion( att ); } /** * {@inheritDoc} */ public void deleteAttachment( Attachment att ) throws ProviderException { m_cache.removeEntry( att.getParentName() ); m_attCache.removeEntry( att.getName() ); m_provider.deleteAttachment( att ); } /** * {@inheritDoc} */ public synchronized String getProviderInfo() { return "Real provider: "+m_provider.getClass().getName()+ ". Cache misses: "+m_cacheMisses+ ". Cache hits: "+m_cacheHits; } /** * Returns the WikiAttachmentProvider that this caching provider delegates to. * * @return The real provider underneath this one. */ public WikiAttachmentProvider getRealProvider() { return m_provider; } /** * {@inheritDoc} */ public void moveAttachmentsForPage( String oldParent, String newParent ) throws ProviderException { m_provider.moveAttachmentsForPage(oldParent, newParent); m_cache.removeEntry( newParent ); m_cache.removeEntry( oldParent ); // // This is a kludge to make sure that the pages are removed // from the other cache as well. // String checkName = oldParent + "/"; Collection<String> names = cloneCollection( m_allCollector.m_allItems.keySet() ); for( String name : names ) { if( name.startsWith( checkName ) ) { m_attCache.removeEntry( name ); } } } /** * Keep a list of all Attachments in the OSCache (OSCache does not provide * something like that) Idea copied from CacheItemCollector The cache is used to * speed up the getRecentChanges function * * @author Harry Metske * @since 2.5 */ private static class CachedAttachmentCollector implements CacheEntryEventListener { private static final Logger log = Logger.getLogger( CachedAttachmentCollector.class ); private Map<String, Attachment> m_allItems = new HashMap<String, Attachment>(); /** * Returns a clone of the set - you cannot manipulate this. * * @return A list of all items. */ public List<Attachment> getAllItems() { List<Attachment> ret = new LinkedList<Attachment>(); ret.addAll( m_allItems.values() ); log.info( "returning " + ret.size() + " attachments" ); return ret; } public void cacheEntryRemoved( CacheEntryEvent aEvent ) { if( aEvent != null ) { if( log.isDebugEnabled() ) { log.debug( "attachment cache entry removed: " + aEvent.getKey() ); } CacheEntry e = aEvent.getEntry(); if( e != null ) { Attachment item = (Attachment) e.getContent(); if( item != null ) { m_allItems.remove( item.getName() ); } } } } public void cacheEntryUpdated( CacheEntryEvent aEvent ) { if( log.isDebugEnabled() ) { log.debug( "attachment cache entry updated: " + aEvent.getKey() ); } Attachment item = (Attachment) aEvent.getEntry().getContent(); if( item != null ) { // Item added or replaced. m_allItems.put( item.getName(), item ); } else { m_allItems.remove( aEvent.getKey() ); } } public void cacheEntryAdded( CacheEntryEvent aEvent ) { cacheEntryUpdated( aEvent ); } public void cachePatternFlushed( CachePatternEvent aEvent ) { // do nothing } public void cacheGroupFlushed( CacheGroupEvent aEvent ) { // do nothing } public void cacheFlushed( CachewideEvent aEvent ) { // do nothing } public void cacheEntryFlushed( CacheEntryEvent aEvent ) { cacheEntryRemoved( aEvent ); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -