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

📄 jahiasimplecache.java

📁 java 写的一个新闻发布系统
💻 JAVA
字号:
////                                   ____.//                       __/\ ______|    |__/\.     _______//            __   .____|    |       \   |    +----+       \//    _______|  /--|    |    |    -   \  _    |    :    -   \_________//   \\______: :---|    :    :           |    :    |         \________>//           |__\---\_____________:______:    :____|____:_____\//                                      /_____|////                 . . . i n   j a h i a   w e   t r u s t . . .//////  JahiaSimpleCache class//  DJ      18.04.2002//  FH      8-oct-2003package org.jahia.data.cache;import java.util.*;                     // Hashtable/** * Simple cache based on a Hashtable and that deletes old values * when it's full. If the value is not loaded the programmer has to * load himself the value from the database and then put it into * cache. * * The object key must implement hashCode and equals method * * If you want to use a cache that automatically load/save * values from database/disk, check org.jahia.data.cache.JahiaCache * * @see org.jahia.data.cache.JahiaCache * @author David Jilli */public class JahiaSimpleCache {    private String cacheName;    private String cachedesc;    private int maxCachedObjects;    // if you want to cache an UNLIMTED number of values.... be careful    // that there can't be memory overflow!    public static int UNLIMITED = -2;    /**     * @associates Object     */    private Hashtable myTable = new Hashtable ();    /**     * @associates String     */    private Vector cachedObjectIDs = new Vector (); // list of all cached object IDS.    /**     * This constructor should only be called by JahiaSimpleCacheFactory, to keep a trace of     * every instance of JahiaSimpleCache.     */    public JahiaSimpleCache (String cacheName, String cachedesc, int maxCachedObjects) {        this.cacheName = cacheName;        this.cachedesc = cachedesc;        this.maxCachedObjects = maxCachedObjects;    }    /**     * Returns the name of the cache.     * @return cache name     */    final public String getCacheName () {        return this.cacheName;    }    /**     * Returns the desc of the cache.     * @return cache desc     */    final public String getCachedesc () {        return this.cachedesc;    }    /**     * Returns the maximum number of cached objects in this cache.     * @return max cached objects     */    final public int getMaxCachedObjects () {        return this.maxCachedObjects;    }    /**     * Returns the number of elements in this cache     * @return an integer corresponding to the number of elements in this     * cache.     */    final public int size () {        return this.myTable.size ();    }    /**     * Sets the maximum number of cached objects in the cache and resizes it on the fly.     * @param maxCachedObjects Maximum number of cached objets     */    public synchronized void setMaxCachedObjects (int maxCachedObjects) {        if (maxCachedObjects != UNLIMITED) {            this.maxCachedObjects = maxCachedObjects;            while (myTable.size () > maxCachedObjects) {                myTable.remove (cachedObjectIDs.get (0));                cachedObjectIDs.remove (0);            }        }    }    /**     * Gets the value of an object thru the cache, null if not in cache     *     * @param  objectKey key of the object requested     * @return The loaded object, <pre>null</pre> if not in cache     */    final public Object getValue (Object objectKey) {        return myTable.get (objectKey);    }    /**     * Sets the value of an object in the cache     * WARNING : The key is the SECOND parameter !!! (not very convenient as     * hashtables use the first parameter for the key !)     *     * @param  theObject The object to save     * @param  objectKey Key of the object     */    public synchronized void setValue (Object theObject, Object objectKey) {        doCacheObject (objectKey, theObject);    } // end setValue    /**     * Remove an object from the cache     *     * @param  objectKey Key of the object     */    public synchronized void removeValue (Object objectKey) {        cachedObjectIDs.remove (objectKey);        myTable.remove (objectKey);    } // end removeValue    /**     * Internal method : adds an object to the cache, and handles the maximum object limits     *     * @param  objectKey the Key of the Object     * @param  theObject the Object to add     */    private void doCacheObject (Object objectKey, Object theObject) {        if (maxCachedObjects != 0) {            if (maxCachedObjects != UNLIMITED) {                while (myTable.size () >= maxCachedObjects)   // if it's > we have a problem!                {                    myTable.remove (cachedObjectIDs.get (0));                    cachedObjectIDs.remove (0);                }            }            // cache the object and put it in the "cached object list"            myTable.put (objectKey, theObject);            cachedObjectIDs.add (objectKey);        }    }    final public Enumeration keys () {        return myTable.keys();    }}

⌨️ 快捷键说明

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