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

📄 cmslrucache.java

📁 内容管理
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * @param oldItem The item to be deleted.
     */
    private void removeFromTable(CacheItem oldItem){
        if(C_DEBUG){
            System.err.println(" --remove from chaincache: "+oldItem.key);
        }
        int hashIndex = ((oldItem.key).hashCode() & 0x7FFFFFFF) % m_maxSize;
        CacheItem item = m_cache[hashIndex];
        if(item == oldItem){
            m_cache[hashIndex] = item.chain;
        }else{
            if(item != null){
                while(item.chain != null){
                    if(item.chain == oldItem){
                        item.chain = item.chain.chain;
                        return;
                    }
                    item = item.chain;
                }
            }
        }
    }

    /**
     * removes one item from the cache and from the sequence chain.
     */
    private synchronized void removeItem(CacheItem item){

        if(C_DEBUG){
            System.err.println("--remove item from cache: "+item.key);
        }
        if(m_size < 2){
            clearCache();
        }else{
            //first remove it from the hashtable
            removeFromTable(item);
            // now from the sequence chain
            if((item != head) && (item != tail)){
                item.previous.next = item.next;
                item.next.previous = item.previous;
            }else{
                if(item == head){
                    head = item.next;
                    head.previous = null;
                }
                if (item == tail){
                    tail = item.previous;
                    tail.next = null;
                }
            }
            m_size--;
        }
    }

    /**
     * Deletes all elements that depend on the template.
     * use only if the cache is for elements.
     *
     * @return Vector a Vector with deleted items. Each item is a Vector with two elements: key and value.
     */
    public synchronized Vector deleteElementsByTemplate(String templateName){
        CacheItem item = head;
        Vector ret = new Vector();
        while (item != null){
            if(templateName.equals(((CmsElementDescriptor)item.key).getTemplateName())){
                Vector actItem = new Vector();
                actItem.add(0, item.key);
                actItem.add(1, item.value);
                ret.add(actItem);
                removeItem(item);
            }
            item = item.next;
        }
        return ret;
    }

    /**
     * Deletes all elements that depend on the class.
     * use only if this cache is for elements.
     * @return Vector a Vector with deleted items. Each item is a Vector with two elements: key and value.
     */
    public synchronized Vector deleteElementsByClass(String className){
        CacheItem item = head;
        Vector ret = new Vector();
        while (item != null){
            if(className.equals(((CmsElementDescriptor)item.key).getClassName())){
                Vector actItem = new Vector();
                actItem.add(0, item.key);
                actItem.add(1, item.value);
                ret.add(actItem);
                removeItem(item);
            }
            item = item.next;
        }
        return ret;
    }

    /**
     * Deletes elements after publish. All elements that depend on the
     * uri and all element that say so have to be removed.
     * use only if this cache is for elements.
     * @return Vector a Vector with deleted items. Each item is a Vector with two elements: key and value.
     */
    public synchronized Vector deleteElementsAfterPublish(){
        CacheItem item = head;
        Vector ret = new Vector();
        while (item != null){
            try{
                if (((A_CmsElement)item.value).getCacheDirectives().shouldRenew()){
                    Vector actItem = new Vector();
                    actItem.add(0, item.key);
                    actItem.add(1, item.value);
                    ret.add(actItem);
                    removeItem(item);
                }
            }catch(NullPointerException e){
                // cachedirectives are null, so we delete this Element anyway.
                Vector actItem = new Vector();
                actItem.add(0, item.key);
                actItem.add(1, item.value);
                ret.add(actItem);
                removeItem(item);
            }
            item = item.next;
        }
        return ret;
    }

    /**
     * Deletes the uri from the Cache. Use only if this is the cache for uris.
     *
     */
    public synchronized void deleteUri(String uri){
        CacheItem item = head;
        while (item != null){
            if(uri.equals(((CmsUriDescriptor)item.key).getKey())){
                removeItem(item);
                // found the uri, ready.
                return;
            }
            item = item.next;
        }
    }

    /**
     * Clears the cache completly.
     */
    public synchronized void clearCache(){
        if(C_DEBUG){
            System.err.println("--LruCache restarted");
        }
        m_cache = new CacheItem[m_maxSize];
        m_size = 0;
        head = null;
        tail = null;
    }

    /**
     * Gets the Information of max size and size for the cache.
     *
     * @return a Vector whith informations about the size of the cache.
     */
    public Vector getCacheInfo(){

        if(C_DEBUG){
            System.err.println("...Cache size should be:"+ m_size + " by a max size of "+m_maxSize);
            int count = 0;
            CacheItem item = head;
            while (item != null){
                count++;
                item = item.next;
            }
            System.err.println("...Cache size is:"+count);
        }
        Vector info = new Vector();
        info.addElement(new Integer(m_maxSize ));
        info.addElement(new Integer(m_size));
        return info;
    }

    /**
     * gets all keys in the cache.
     * @return a vector with the keys.
     */
    public synchronized Vector getAllKeys(){
        Vector ret = new Vector();
        CacheItem item = head;
        while (item != null){
            ret.add(item.key);
            item = item.next;
        }
        return ret;
    }

    /*
     * used for debuging only. Checks if the Cache is in a valid condition.
    private void checkCondition(){
        System.err.println("");
        System.err.println("-- Verify condition of Cache");
        System.err.println("--size: "+m_size);
        CacheItem item = head;
        int count = 1;
        System.err.println("--");
        System.err.println("--testing content from head to tail:");
        while(item!=null){
            System.err.println("    --"+count+". "+item.key);
            count++;
            item=item.next;
        }
        System.err.println("");
        System.err.println("--now from tail to head:");
        item = tail;
        count--;
        while(item!=null){
            System.err.println("    --"+count+". "+item.key);
            count--;
            item=item.previous;
        }
        System.err.println("--now what is realy in cache:");
        count = 1;
        for (int i=0; i<m_maxSize; i++){
            item = m_cache[i];
            if(item == null){
//                System.err.print("    element "+i+" ");
//                System.err.println(" null");
            }else{
                System.err.print("    element "+i+" ");
                System.err.println(" count="+count++ +" "+item.key);
                while(item.chain != null){
                    item = item.chain;
                    System.err.println("        chainelement "+" count="+count++ +" "+item.key);
                }
            }
        }
        System.err.println("--test ready!!");
        System.err.println("");

    }
    */
}

⌨️ 快捷键说明

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