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

📄 utilcache.java

📁 Sequoia ERP是一个真正的企业级开源ERP解决方案。它提供的模块包括:电子商务应用(e-commerce), POS系统(point of sales),知识管理,存货与仓库管理
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        return line;    }    public List values() {        if (cacheLineTable == null) {            return null;        }                List valuesList = FastList.newInstance();        Iterator i = cacheLineTable.keySet().iterator();        while (i.hasNext()) {            Object key = i.next();            valuesList.add(this.get(key));        }        return valuesList;    }    public long getSizeInBytes() {        long totalSize = 0;        Iterator i = cacheLineTable.values().iterator();        while (i.hasNext()) {            totalSize += ((CacheLine) i.next()).getSizeInBytes();        }        return totalSize;    }    /** Removes an element from the cache according to the specified key     * @param key The key for the element, used to reference it in the hastables and LRU linked list     * @return The value of the removed element specified by the key     */    public synchronized Object remove(Object key) {        return this.removeInternal(key, true);    }        /** This is used for internal remove calls because we only want to count external calls */    protected synchronized Object removeInternal(Object key, boolean countRemove) {                if (key == null) {            if (Debug.verboseOn()) Debug.logVerbose("In UtilCache tried to remove with null key, using NullObject for cache " + this.getName(), module);            key = ObjectType.NULL;        }        CacheLine line = (CacheLine) cacheLineTable.remove(key);        if (line != null) {            noteRemoval(key, line.getValue());            if (countRemove) this.removeHitCount++;            return line.getValue();        } else {            if (countRemove) this.removeMissCount++;            return null;        }    }    /** Removes all elements from this cache */    public synchronized void clear() {        Iterator it = cacheLineTable.keySet().iterator();        while (it.hasNext()) {            Object key = it.next();            CacheLine line = getInternalNoCheck(key);            noteRemoval(key, line == null ? null : line.getValue());        }        cacheLineTable.clear();        clearCounters();    }    /** Removes all elements from this cache */    public static void clearAllCaches() {        Iterator entries = utilCacheTable.entrySet().iterator();        while (entries.hasNext()) {            Map.Entry entry = (Map.Entry) entries.next();            UtilCache utilCache = (UtilCache) entry.getValue();            utilCache.clear();        }    }    /** Getter for the name of the UtilCache instance.     * @return The name of the instance     */    public String getName() {        return this.name;    }    /** Returns the number of successful hits on the cache     * @return The number of successful cache hits     */    public long getHitCount() {        return this.hitCount;    }    /** Returns the number of cache misses from entries that are not found in the cache     * @return The number of cache misses     */    public long getMissCountNotFound() {        return this.missCountNotFound;    }    /** Returns the number of cache misses from entries that are expired     * @return The number of cache misses     */    public long getMissCountExpired() {        return this.missCountExpired;    }    /** Returns the number of cache misses from entries that are have had the soft reference cleared out (by garbage collector and such)     * @return The number of cache misses     */    public long getMissCountSoftRef() {        return this.missCountSoftRef;    }    /** Returns the number of cache misses caused by any reason     * @return The number of cache misses     */    public long getMissCountTotal() {        return this.missCountSoftRef + this.missCountNotFound + this.missCountExpired;    }        public long getRemoveHitCount() {        return this.removeHitCount;    }        public long getRemoveMissCount() {        return this.removeMissCount;    }    /** Clears the hit and miss counters     */    public void clearCounters() {        this.hitCount = 0;        this.missCountNotFound = 0;        this.missCountExpired = 0;        this.missCountSoftRef = 0;        this.removeHitCount = 0;        this.removeMissCount = 0;    }    /** Sets the maximum number of elements in the cache.     * If 0, there is no maximum.     * @param maxSize The maximum number of elements in the cache     */    public void setMaxSize(int maxSize) {        cacheLineTable.setLru((int) maxSize);        this.maxSize = maxSize;    }    /** Returns the current maximum number of elements in the cache     * @return The maximum number of elements in the cache     */    public long getMaxSize() {        return maxSize;    }    /** Sets the expire time for the cache elements.     * If 0, elements never expire.     * @param expireTime The expire time for the cache elements     */    public void setExpireTime(long expireTime) {        // if expire time was <= 0 and is now greater, fill expire table now        if (this.expireTime <= 0 && expireTime > 0) {            long currentTime = System.currentTimeMillis();            Iterator values = cacheLineTable.values().iterator();            while (values.hasNext()) {                CacheLine line = (CacheLine) values.next();                line.loadTime = currentTime;            }        } else if (this.expireTime <= 0 && expireTime > 0) {            // if expire time was > 0 and is now <=, do nothing, just leave the load times in place, won't hurt anything...        }        this.expireTime = expireTime;    }    /** return the current expire time for the cache elements     * @return The expire time for the cache elements     */    public long getExpireTime() {        return expireTime;    }    /** Set whether or not the cache lines should use a soft reference to the data */    public void setUseSoftReference(boolean useSoftReference) {        if (this.useSoftReference != useSoftReference) {            this.useSoftReference = useSoftReference;            Iterator values = cacheLineTable.values().iterator();            while (values.hasNext()) {                CacheLine line = (CacheLine) values.next();                line.setUseSoftReference(useSoftReference);            }        }    }    /** Return whether or not the cache lines should use a soft reference to the data */    public boolean getUseSoftReference() {        return this.useSoftReference;    }        public boolean getUseFileSystemStore() {        return this.useFileSystemStore;    }    /** Returns the number of elements currently in the cache     * @return The number of elements currently in the cache     */    public long size() {        return cacheLineTable.size();    }    /** Returns a boolean specifying whether or not an element with the specified key is in the cache.     * If the requested element hasExpired, it is removed before it is looked up which causes the function to return false.     * @param key The key for the element, used to reference it in the hastables and LRU linked list     * @return True is the cache contains an element corresponding to the specified key, otherwise false     */    public boolean containsKey(Object key) {        CacheLine line = getInternal(key, false);        if (line != null) {            return true;        } else {            return false;        }    }        /**      * NOTE: this returns an unmodifiable copy of the keySet, so removing from here won't have an effect,      * and calling a remove while iterating through the set will not cause a concurrent modification exception.     * This behavior is necessary for now for the persisted cache feature.      */    public Set getCacheLineKeys() {        return cacheLineTable.keySet();    }    public Collection getCacheLineValues() {        return cacheLineTable.values();    }    /** Returns a boolean specifying whether or not the element corresponding to the key has expired.     * Only returns true if element is in cache and has expired. Error conditions return false, if no expireTable entry, returns true.     * Always returns false if expireTime <= 0.     * Also, if SoftReference in the CacheLine object has been cleared by the gc return true.     *     * @param key The key for the element, used to reference it in the hastables and LRU linked list     * @return True is the element corresponding to the specified key has expired, otherwise false     */    public boolean hasExpired(Object key) {        CacheLine line = getInternalNoCheck(key);        return hasExpired(line);    }    protected boolean hasExpired(CacheLine line) {        if (line == null) return false;        // check this BEFORE checking to see if expireTime <= 0, ie if time expiration is enabled        // check to see if we are using softReference first, slight performance increase        if (line.softReferenceCleared()) return true;                // check if expireTime <= 0, ie if time expiration is not enabled        if (line.expireTime <= 0) return false;        // check if the time was saved for this; if the time was not saved, but expire time is > 0, then we don't know when it was saved so expire it to be safe        if (line.loadTime <= 0) return true;                if ((line.loadTime + line.expireTime) < System.currentTimeMillis()) {            return true;        } else {            return false;        }    }    /** Clears all expired cache entries; also clear any cache entries where the SoftReference in the CacheLine object has been cleared by the gc */    public void clearExpired() {        Iterator keys = cacheLineTable.keySet().iterator();        while (keys.hasNext()) {            Object key = keys.next();            if (hasExpired(key)) {                removeInternal(key, false);            }        }    }    /** Send a key addition event to all registered listeners */    protected void noteAddition(Object key, Object newValue) {        synchronized (listeners) {            Iterator it = listeners.iterator();            while (it.hasNext()) {                CacheListener listener = (CacheListener) it.next();                listener.noteKeyAddition(this, key, newValue);            }        }    }    /** Send a key removal event to all registered listeners */    protected void noteRemoval(Object key, Object oldValue) {        synchronized (listeners) {            Iterator it = listeners.iterator();            while (it.hasNext()) {                CacheListener listener = (CacheListener) it.next();                listener.noteKeyRemoval(this, key, oldValue);            }        }    }    /** Send a key update event to all registered listeners */    protected void noteUpdate(Object key, Object newValue, Object oldValue) {        synchronized (listeners) {            Iterator it = listeners.iterator();            while (it.hasNext()) {                CacheListener listener = (CacheListener) it.next();                listener.noteKeyUpdate(this, key, newValue, oldValue);            }        }    }    /** Adds an event listener for key removals */    public void addListener(CacheListener listener) {        synchronized (listeners) {            listeners.add(listener);        }    }        /** Removes an event listener for key removals */    public void removeListener(CacheListener listener) {        synchronized (listeners) {            listeners.remove(listener);        }    }        /** Clears all expired cache entries from all caches */    public static void clearExpiredFromAllCaches() {        Iterator entries = utilCacheTable.entrySet().iterator();        while (entries.hasNext()) {            Map.Entry entry = (Map.Entry) entries.next();            UtilCache utilCache = (UtilCache) entry.getValue();            utilCache.clearExpired();        }    }        /** Checks for a non-expired key in a specific cache */    public static boolean validKey(String cacheName, Object key) {        UtilCache cache = (UtilCache) utilCacheTable.get(cacheName);        if (cache != null) {            if (cache.containsKey(key))                return true;        }        return false;    }        public static void clearCachesThatStartWith(String startsWith) {        synchronized (utilCacheTable) {            Iterator it = utilCacheTable.entrySet().iterator();            while (it.hasNext()) {                    Map.Entry entry = (Map.Entry) it.next();                    String name = (String) entry.getKey();                    if (name.startsWith(startsWith)) {                        UtilCache cache = (UtilCache) entry.getValue();                        cache.clear();                    }            }        }    }    public static void clearCache(String cacheName) {        synchronized (UtilCache.utilCacheTable) {            UtilCache cache = (UtilCache) UtilCache.utilCacheTable.get(cacheName);            if (cache == null) return;            cache.clear();        }    }}

⌨️ 快捷键说明

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