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

📄 utilcache.java

📁 短信发送
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	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 name;
	}

	/** Returns the number of successful hits on the cache
	 * @return The number of successful cache hits
	 */
	public long getHitCount() {
		return hitCount;
	}

	/** Returns the number of cache misses
	 * @return The number of cache misses
	 */
	public long getMissCount() {
		return missCount;
	}

	/** Clears the hit and miss counters
	 */
	public void clearCounters() {
		hitCount = 0;
		missCount = 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(long maxSize) {
		// if the new maxSize is <= 0, clear keyLRUList
		if (maxSize <= 0) {
			keyLRUList.clear();
		} else if (maxSize > 0 && this.maxSize <= 0) {
			// if the new maxSize > 0 and the old is <= 0, fill in LRU list - order will be meaningless for now
			Iterator<Object> keys = cacheLineTable.keySet().iterator();

			while (keys.hasNext()) {
				keyLRUList.add(keys.next());
			}
		}

		// if the new maxSize is less than the current cache size, shrink the cache.
		if (maxSize > 0 && cacheLineTable.size() > maxSize) {
			while (cacheLineTable.size() > maxSize) {
				Object lastKey = keyLRUList.getLast();

				remove(lastKey);
			}
		}

		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<CacheLine> values = cacheLineTable.values().iterator();

			while (values.hasNext()) {
				UtilCache.CacheLine line = (UtilCache.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<CacheLine> values = cacheLineTable.values().iterator();

			while (values.hasNext()) {
				UtilCache.CacheLine line = (UtilCache.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;
	}

	/** 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) {
		UtilCache.CacheLine line = (UtilCache.CacheLine) cacheLineTable
				.get(key);

		if (hasExpired(line)) {
			remove(key);
			line = null;
		}
		if (line != null) {
			return true;
		} else {
			return false;
		}
	}

	/** 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) {
		if (key == null)
			return false;

		UtilCache.CacheLine line = (UtilCache.CacheLine) cacheLineTable
				.get(key);

		return hasExpired(line);
	}

	protected boolean hasExpired(UtilCache.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 (this.useSoftReference && line.getValue() == null)
			return true;
		if (expireTime <= 0)
			return false;

		if (line.loadTime <= 0)
			return true;
		if ((line.loadTime + 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<Object> keys = cacheLineTable.keySet().iterator();

		while (keys.hasNext()) {
			Object key = keys.next();

			if (hasExpired(key)) {
				remove(key);
			}
		}
	}

	/** 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 class CacheLine {
		public Object valueRef = null;
		public long loadTime = 0;
		public boolean useSoftReference = false;

		public CacheLine(Object value, boolean useSoftReference) {
			if (useSoftReference) {
				this.valueRef = new java.lang.ref.SoftReference(value);
			} else {
				this.valueRef = value;
			}
			this.useSoftReference = useSoftReference;
		}

		public CacheLine(Object value, boolean useSoftReference, long loadTime) {
			this(value, useSoftReference);
			this.loadTime = loadTime;
		}

		public Object getValue() {
			if (valueRef == null)
				return null;
			if (useSoftReference) {
				return ((java.lang.ref.SoftReference) valueRef).get();
			} else {
				return valueRef;
			}
		}

		public void setUseSoftReference(boolean useSoftReference) {
			if (this.useSoftReference != useSoftReference) {
				synchronized (this) {
					this.useSoftReference = useSoftReference;
					if (useSoftReference) {
						this.valueRef = new java.lang.ref.SoftReference(
								this.valueRef);
					} else {
						this.valueRef = ((java.lang.ref.SoftReference) this.valueRef)
								.get();
					}
				}
			}
		}
	}
}

⌨️ 快捷键说明

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