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

📄 cmsflexcache.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * @param key the key to add to the cache.
     */
    void putKey(CmsFlexCacheKey key) {
        if (! isEnabled()) return;
        Object o = m_resourceMap.get(key.Resource);
        if (o == null) {
            // No variation map for this resource yet, so create one
            CmsFlexCacheVariation variationMap = new CmsFlexCacheVariation( key );
            m_resourceMap.put( key.Resource, variationMap );
            this.m_VariationCache.add( (I_CmsFlexLruCacheObject)variationMap );
            if (DEBUG > 1) System.err.println("FlexCache: Added pre-calculated key for resource " + key.Resource);
        }
        // If != null the key is already in the cache, so we just do nothing
    }
    
    /**
     * This method adds new entries to the cache.<p>
     *
     * The key describes the conditions under which the value can be cached.
     * Usually the key belongs to the response.
     * The variation describes the conditions under which the
     * entry was created. This is usually calculated from the request.
     * If the variation is != null, the entry is cachable.<p>
     *
     * @param key the key for the new value entry. Usually calculated from the response
     * @param entry the CmsFlexCacheEntry to store in the cache
     * @param variation the pre-calculated variation for the entry
     * @return true if the value was added to the cache, false otherwise
     */
    boolean put(CmsFlexCacheKey key, CmsFlexCacheEntry entry, String variation) {
        if (! isEnabled()) return false;
        if (DEBUG > 1) System.err.println("FlexCache: Trying to add entry for resource " + key.Resource);
        if (variation != null) {
            // This is a cachable result
            key.Variation = variation;
            if (DEBUG > 1) System.err.println("FlexCache: Adding entry for resource " + key.Resource + " with variation:" + key.Variation);
            put(key, entry);
            // Note that duplicates are NOT checked, it it assumed that this is done beforehand,
            // while checking if the entry is already in the cache or not.
            return true;
        } else {
            // Result is not cachable
            if (DEBUG > 1) System.err.println("FlexCache: Nothing added because resource is not cachable for this request!");
            return false;
        }
    }
    
    /**
     * Removes an entry from the cache.<p>
     *
     * @param key the key which describes the entry to remove from the cache
     */
    void remove(CmsFlexCacheKey key) {
        if (! isEnabled()) return;
        Object o = m_resourceMap.get(key.Resource);
        if (o != null) {
            //Object old = ((HashMap)o).remove(key.Variation);
            Object old = ((HashMap)o).get(key.Variation);
            if (old != null) {
                this.getEntryLruCache().remove( (I_CmsFlexLruCacheObject)old );
            }
        };
    }
    
    /**
     * Checks if the cache is empty or if at last one element is contained.<p>
     *
     * @return true if the cache is empty, false otherwise
     */
    boolean isEmpty() {
        if (! isEnabled()) return true;
        return m_resourceMap.isEmpty();
    }
    
    /**
     * Emptys the cache completely.<p>
     */
    private synchronized void clear() {
        if (! isEnabled()) return;
        
        m_resourceMap.clear();
        m_resourceMap = java.util.Collections.synchronizedMap(new CmsLruHashMap(C_INITIAL_CAPACITY_CACHE));
        
        m_size = 0;
        
        this.m_EntryLruCache.clear();
        this.m_VariationCache.clear();
        
        if (I_CmsLogChannels.C_LOGGING && A_OpenCms.isLogging(I_CmsLogChannels.C_FLEX_CACHE)) 
            A_OpenCms.log(I_CmsLogChannels.C_FLEX_CACHE, "[FlexCache] Complete cache cleared - clear() called" );
    }
    
    /**
     * Save a value to the cache.<p>
     *
     * @param key the key under shich the value is saved
     * @param value the value to save in the cache
     */
    private void put( CmsFlexCacheKey key, CmsFlexCacheEntry theCacheEntry ) {
        Object o = m_resourceMap.get(key.Resource);
        if (key.m_timeout > 0) theCacheEntry.setTimeout(key.m_timeout * 60000);
        if (o != null) {
            // We already have a variation map for this resource
            java.util.Map m = ((CmsFlexCacheVariation)o).map;
            boolean wasAdded = true;
            if (! m.containsKey(key.Variation)) {
                wasAdded = this.m_EntryLruCache.add( (I_CmsFlexLruCacheObject)theCacheEntry );
            }
            else {
                wasAdded = this.m_EntryLruCache.touch( (I_CmsFlexLruCacheObject)theCacheEntry );
            }
            
            if (wasAdded) {
                theCacheEntry.setVariationData( key.Variation, m );
                m.put(key.Variation, theCacheEntry);
                this.m_VariationCache.touch( (I_CmsFlexLruCacheObject)o );
            }
        } else {
            // No variation map for this resource yet, so create one
            CmsFlexCacheVariation list = new CmsFlexCacheVariation(key);

            boolean wasAdded = this.m_EntryLruCache.add( (I_CmsFlexLruCacheObject)theCacheEntry );
            
            if (wasAdded) {
                theCacheEntry.setVariationData( key.Variation, list.map );
                list.map.put(key.Variation, theCacheEntry);
                m_resourceMap.put(key.Resource, list);
                this.m_VariationCache.add( (I_CmsFlexLruCacheObject)list );
            }
        }
        
        if (DEBUG > 0) System.err.println("FlexCache: Entry "  + m_size + " added for resource " + key.Resource + " with variation " + key.Variation);
        if (DEBUG > 2) System.err.println("FlexCache: Entry added was:\n" + theCacheEntry.toString() );
    }
    
    /**
     * Internal method to determine if a user has Administration permissions.<p>
     */
    private boolean isAdmin(CmsObject cms) {
        boolean result;
        try {
            result = cms.getRequestContext().isAdmin();
        } catch (Exception e) {
            result = false;
        }
        return result;
    }
    

    /**
     * Internal method to perform cache clearance.<p>
     * 
     * It clears "one half" of the cache, i.e. either
     * the online or the offline parts.
     * A parameter is used to indicate if only
     * the entries or keys and entries are to be cleared.<p>
     * 
     * @param suffix used to distinguish between "[Online]" and "[Offline]" entries
     * @param entriesOnly if <code>true</code>, only entries will be cleared, otherwise
     *         the entries and the keys will be cleared
     */    
    private synchronized void clearOneHalf(String suffix, boolean entriesOnly) {
        java.util.Set keys = new java.util.HashSet(m_resourceMap.keySet());
        java.util.Iterator i = keys.iterator();
        while (i.hasNext()) {
            String s = (String)i.next();
            if (s.endsWith(suffix)) {
                CmsFlexCacheVariation v = (CmsFlexCacheVariation)m_resourceMap.get(s);
                if (entriesOnly) {
                    // Clear only entry
                    m_size -= v.map.size();
                    java.util.Iterator allEntries = v.map.values().iterator();
                    while (allEntries.hasNext()) {
                        I_CmsFlexLruCacheObject nextObject = (I_CmsFlexLruCacheObject)allEntries.next();
                        allEntries.remove();
                        this.m_EntryLruCache.remove( nextObject );
                    }
                    v.map = java.util.Collections.synchronizedMap(new HashMap(C_INITIAL_CAPACITY_VARIATIONS));
                } else {
                    // Clear key and entry
                    m_size -= v.map.size();
                    java.util.Iterator allEntries = v.map.values().iterator();
                    while (allEntries.hasNext()) {
                        I_CmsFlexLruCacheObject nextObject = (I_CmsFlexLruCacheObject)allEntries.next();
                        allEntries.remove();
                        this.m_EntryLruCache.remove( nextObject );
                    }
                    
                    this.m_VariationCache.remove( (I_CmsFlexLruCacheObject)v );
                    
                    v.map = null;
                    v.key = null;
                    m_resourceMap.remove(s);
                }
            }
        }
        if (I_CmsLogChannels.C_LOGGING && A_OpenCms.isLogging(I_CmsLogChannels.C_FLEX_CACHE)) 
            A_OpenCms.log(I_CmsLogChannels.C_FLEX_CACHE, "[FlexCache] Part of the FlexCache cleared - clearOneHalf(" + suffix + ", " + entriesOnly + ") called" );
    }
    
    /**
     * Returns the LRU cache where the CacheEntries are cached.<p>
     *
     * @return the LRU cache where the CacheEntries are cached
     */
    public CmsFlexLruCache getEntryLruCache() {
        return this.m_EntryLruCache;
    }
        
    /**
     * A simple data container class for the FlexCache variations.<p>
     * 
     * @see com.opencms.flex.cache.CmsFlexCache
     * @see com.opencms.flex.util.I_CmsFlexLruCacheObject
     * @author Alexander Kandzior (a.kandzior@alkacon.com)
     * @author Thomas Weckert (t.weckert@alkacon.com)
     * @version $Revision: 1.17 $ 
     */
    class CmsFlexCacheVariation extends Object implements com.opencms.flex.util.I_CmsFlexLruCacheObject {
        
        /** Pointer to the next cache entry in the LRU cache */
        private I_CmsFlexLruCacheObject m_Next;
        
        /** Pointer to the previous cache entry in the LRU cache. */
        private I_CmsFlexLruCacheObject m_Previous;        
        
        /** The key belonging to the resource */
        public CmsFlexCacheKey key;
        
        /** Maps variations to CmsFlexCacheEntries */
        public Map map;
        
        /** Internal debug switch */
        private static final int DEBUG = 0;
                
        /**
         * Generates a new instance of CmsFlexCacheVariation.<p>
         *
         * @param theKey The (resource) key to contruct this variation list for
         */
        public CmsFlexCacheVariation(CmsFlexCacheKey theKey ) {
            this.key = theKey;
            this.map = java.util.Collections.synchronizedMap( new HashMap(CmsFlexCache.C_INITIAL_CAPACITY_VARIATIONS) );
        }
        
        // implementation of the com.opencms.flex.util.I_CmsFlexLruCacheObject interface methods
        
        /**
         * @see com.opencms.flex.util.I_CmsFlexLruCacheObject#setNextLruObject(I_CmsFlexLruCacheObject)
         */
        public void setNextLruObject( I_CmsFlexLruCacheObject theNextEntry ) {
            this.m_Next = theNextEntry;
        }
        
        /**
         * @see com.opencms.flex.util.I_CmsFlexLruCacheObject#getNextLruObject()
         */
        public I_CmsFlexLruCacheObject getNextLruObject() {
            return this.m_Next;
        }
        
        /**
         * @see com.opencms.flex.util.I_CmsFlexLruCacheObject#setPreviousLruObject(I_CmsFlexLruCacheObject)
         */
        public void setPreviousLruObject( I_CmsFlexLruCacheObject thePreviousEntry ) {
            this.m_Previous = thePreviousEntry;
        }
        
        /**
         * @see com.opencms.flex.util.I_CmsFlexLruCacheObject#getPreviousLruObject()
         */
        public I_CmsFlexLruCacheObject getPreviousLruObject() {
            return this.m_Previous;
        }  
        
        /**
         * @see com.opencms.flex.util.I_CmsFlexLruCacheObject#addToLruCache()
         */
        public void addToLruCache() {
            // NOOP
            if (DEBUG>0) System.out.println( "Added resource " + this.key.Resource + " to the LRU cache" );
        }
        
        /**
         * @see com.opencms.flex.util.I_CmsFlexLruCacheObject#removeFromLruCache()
         */
        public void removeFromLruCache() {
            // NOOP            
            if (DEBUG>0) System.out.println( "Removed resource " + this.key.Resource + " from the LRU cache" );
        }
        
        /**
         * @see com.opencms.flex.util.I_CmsFlexLruCacheObject#getLruCacheCosts()
         */
        public int getLruCacheCosts() {
            return this.map.size();
        }     
    }    
}

⌨️ 快捷键说明

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