![JAR search and dependency download from the Maven repository](/logo.png)
com.aspire.nm.component.util.CacheUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of miniServer Show documentation
Show all versions of miniServer Show documentation
miniServer is a dajiangnan's WebFrameWork
The newest version!
package com.aspire.nm.component.util;
import org.apache.commons.collections4.map.LRUMap;
import org.apache.log4j.Logger;
import java.util.Collections;
import java.util.Map;
public class CacheUtil {
private static Logger logger = Logger.getLogger(CacheUtil.class);
private int cacheSize = 10000;
private Map cache = null;
public CacheUtil(int cacheSizeIn) {
cacheSize = cacheSizeIn;
cache = Collections.synchronizedMap(new LRUMap(cacheSize));
}
public CacheUtil() {
cache = Collections.synchronizedMap(new LRUMap(cacheSize));
}
public boolean put(String key, Object value,long expireTime) {
try {
cache.put(key, new CacheItem(value,expireTime));
return true;
} catch (Exception e) {
logger.error("cache " + key + "put failed");
return false;
}
}
public Object get(String key) {
Object obj = cache.get(key);
if (obj == null) {
return null;
}
CacheItem cacheItem = (CacheItem) obj;
if((System.currentTimeMillis() - cacheItem
.getTimeStamp()) > cacheItem.getExpiredTime()){
logger.debug("cacheItem expired, key:"+ key);
return null;
}
logger.debug("cache " + key + " hit.");
return cacheItem.getCacheObject();
}
public void remove(Object key) {
cache.remove(key);
}
public void clear() {
cache.clear();
}
public static void main(String [] args){
CacheUtil cache = new CacheUtil(3);
cache.put("1", 1,2000);
cache.put("2", 2,2000);
cache.put("3", 3,2000);
cache.put("4", 4,2000);
try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
}
System.out.println(cache.get("1"));
System.out.println(cache.get("2"));
System.out.println(cache.get("3"));
System.out.println(cache.get("4"));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy