
ars.util.SimpleCache Maven / Gradle / Ivy
The newest version!
package ars.util;
import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.io.Serializable;
/**
* 数据缓存简单实现
*
* @author wuyongqiang
*/
public class SimpleCache implements Cache {
private boolean destroyed;
private final Server cleaner = this.initializeCleanupServer();
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private final Map values = new HashMap();
public SimpleCache() {
this.cleaner.start();
}
/**
* 缓存值包装类
*
* @author wuyongqiang
*/
class ValueWrapper implements Serializable {
private static final long serialVersionUID = 1L;
public final Object value; // 缓存值
public final int timeout; // 超时时间(秒)
public volatile long timestamp = System.currentTimeMillis(); // 时间戳(毫秒)
public ValueWrapper(Object value, int timeout) {
this.value = value;
this.timeout = timeout;
}
/**
* 判断缓存值是否过期
*
* @return true/false
*/
public boolean isExpired() {
return this.timeout > 0 && System.currentTimeMillis() - this.timestamp >= this.timeout * 1000;
}
}
/**
* 初始化数据清理服务
*
* @return 服务对象
*/
protected Server initializeCleanupServer() {
AbstractTimerServer server = new AbstractTimerServer() {
@Override
protected void execute() throws Exception {
lock.writeLock().lock();
try {
Iterator iterator = values.keySet().iterator();
while (iterator.hasNext()) {
ValueWrapper wrapper = values.get(iterator.next());
if (wrapper.isExpired()) {
iterator.remove();
}
}
} finally {
lock.writeLock().unlock();
}
}
};
server.setInterval(2 * 60 * 60);
return server;
}
@Override
public Object get(String key) {
if (key == null) {
throw new IllegalArgumentException("Key must not be null");
}
this.lock.readLock().lock();
try {
if (this.destroyed) {
throw new IllegalStateException("Cache already destroyed");
}
ValueWrapper wrapper = this.values.get(key);
if (wrapper == null || wrapper.isExpired()) {
return null;
}
wrapper.timestamp = System.currentTimeMillis();
return wrapper.value;
} finally {
this.lock.readLock().unlock();
}
}
@Override
public void set(String key, Object value) {
this.set(key, value, 0);
}
@Override
public void set(String key, Object value, int timeout) {
if (key == null) {
throw new IllegalArgumentException("Key must not be null");
}
this.lock.writeLock().lock();
try {
if (this.destroyed) {
throw new IllegalStateException("Cache already destroyed");
}
this.values.put(key, new ValueWrapper(value, timeout));
} finally {
this.lock.writeLock().unlock();
}
}
@Override
public void remove(String key) {
if (key == null) {
throw new IllegalArgumentException("Key must not be null");
}
this.lock.writeLock().lock();
try {
if (this.destroyed) {
throw new IllegalStateException("Cache already destroyed");
}
Iterator iterator = this.values.keySet().iterator();
while (iterator.hasNext()) {
if (Strings.matches(iterator.next(), key)) {
iterator.remove();
}
}
} finally {
this.lock.writeLock().unlock();
}
}
@Override
public boolean exists(String key) {
if (key == null) {
throw new IllegalArgumentException("Key must not be null");
}
this.lock.readLock().lock();
try {
if (this.destroyed) {
throw new IllegalStateException("Cache already destroyed");
}
ValueWrapper wrapper = this.values.get(key);
return wrapper != null && !wrapper.isExpired();
} finally {
this.lock.readLock().unlock();
}
}
@Override
public void clear() {
this.lock.writeLock().lock();
try {
if (this.destroyed) {
throw new IllegalStateException("Cache already destroyed");
}
this.values.clear();
} finally {
this.lock.writeLock().unlock();
}
}
@Override
public void destroy() {
if (!this.destroyed) {
this.lock.writeLock().lock();
try {
if (!this.destroyed) {
this.cleaner.stop();
this.destroyed = true;
}
} finally {
this.lock.writeLock().unlock();
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy