⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cachingprovider.java

📁 wiki建站资源 java编写的 很好用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    }    /**     *  @throws RepositoryModifiedException If the page has been externally modified.     */    private String getTextFromCache( String pageName )        throws ProviderException,               RepositoryModifiedException    {        String text;        if( pageName == null ) return null;                WikiPage page = getPageInfoFromCache( pageName );        try        {            text = (String)m_textCache.getFromCache( pageName,                                                     m_pageContentExpiryPeriod );                        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 );                m_cacheMisses++;            }            else            {                m_textCache.putInCache( pageName, null );                return null; // No page exists            }        }                return text;    }    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.flushEntry( page.getName() );            m_textCache.flushEntry( page.getName() );            m_historyCache.flushEntry( page.getName() );            m_negCache.flushEntry( page.getName() );                        // Refresh caches            try            {                getPageInfoFromCache( page.getName() );            }            catch(RepositoryModifiedException e) {} // Expected        }    }    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;    }    public Collection getAllChangedSince( Date date )    {        return m_provider.getAllChangedSince( date );    }    public int getPageCount()        throws ProviderException    {        return m_provider.getPageCount();    }    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() )        {            StringReader in = null;            StringWriter out = null;                            try            {                in = new StringReader( m_provider.getPageText(page.getName(), page.getVersion()) );                                                                     TranslatorReader tr = new TranslatorReader( new WikiContext(m_engine, page), in );                                out = new StringWriter();                FileUtil.copyContents( tr, out );            }            catch( Exception ex )            {                log.debug("Failed to retrieve variables for wikipage "+page);            }            finally            {                if( in != null ) in.close();                if( out != null ) try { out.close(); } catch( IOException ex ) {};            }        }    }        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;    }    public List getVersionHistory( String page )        throws ProviderException    {        List history = null;        if( page == null ) return null;        try        {            history = (List)m_historyCache.getFromCache( page,                                                         m_expiryPeriod );            log.debug("History cache hit for page "+page);            m_historyCacheHits++;        }        catch( NeedsRefreshException e )        {            history = m_provider.getVersionHistory( page );            m_historyCache.putInCache( page, history );            log.debug("History cache miss for page "+page);            m_historyCacheMisses++;        }        return history;    }    public synchronized String getProviderInfo()    {                      return("Real provider: "+m_provider.getClass().getName()+               "<br />Cache misses: "+m_cacheMisses+               "<br />Cache hits: "+m_cacheHits+               "<br />History cache hits: "+m_historyCacheHits+               "<br />History cache misses: "+m_historyCacheMisses+               "<br />Cache consistency checks: "+m_expiryPeriod+"s");    }    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.flushEntry( pageName );                m_textCache.putInCache( pageName, null );                m_historyCache.putInCache( pageName, null );            }            m_provider.deleteVersion( pageName, version );        }    }    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 );        }    }    /**     *  Returns the actual used provider.     *  @since 2.0     */    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.     *      *  @author jalkanen     *     *  @since     */    private class CacheItemCollector        implements CacheEntryEventListener    {        private TreeSet m_allItems = new TreeSet();                /**         *          * Returns a clone of the set - you cannot manipulate this.         * @return         */        public Set getAllItems()        {            return (Set)m_allItems.clone();        }                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 )        {            WikiPage item = (WikiPage) arg0.getEntry().getContent();            if( item != null )            {                m_allItems.remove( item );            }        }        public void cacheEntryUpdated( CacheEntryEvent arg0 )        {            WikiPage item = (WikiPage) arg0.getEntry().getContent();            if( item != null )            {                // Item added or replaced.                m_allItems.add( item );            }            else            {                // Removed item                // FIXME: If the page system is changed during this time, we'll just fail gracefully                                try                {                    for( Iterator i = m_allItems.iterator(); i.hasNext(); )                    {                        WikiPage p = (WikiPage)i.next();                                            if( p.getName().equals( arg0.getKey() ) )                        {                            i.remove();                            break;                        }                    }                }                catch( Exception e )                {}            }        }    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -