This commit is contained in:
RedKale
2016-02-23 16:17:33 +08:00
parent 6ff5115c78
commit a99413f49d
2 changed files with 21 additions and 18 deletions

View File

@@ -213,34 +213,36 @@ public class CacheSourceService<K extends Serializable, V extends Object> implem
@Override
@MultiRun
public V getAndRefresh(K key) {
public V getAndRefresh(K key, final int expireSeconds) {
if (key == null) return null;
CacheEntry entry = container.get(key);
if (entry == null || entry.isExpired() || entry.value == null) return null;
entry.lastAccessed = (int) (System.currentTimeMillis() / 1000);
entry.expireSeconds = expireSeconds;
if (entry.isListCacheType()) return (V) new ArrayList((Collection) entry.value);
if (entry.isSetCacheType()) return (V) new HashSet((Collection) entry.value);
return (V) entry.getValue();
}
@Override
public void getAndRefresh(final CompletionHandler<V, K> handler, @DynAttachment final K key) {
V rs = getAndRefresh(key);
public void getAndRefresh(final CompletionHandler<V, K> handler, @DynAttachment final K key, final int expireSeconds) {
V rs = getAndRefresh(key, expireSeconds);
if (handler != null) handler.completed(rs, key);
}
@Override
@MultiRun
public void refresh(K key) {
public void refresh(K key, final int expireSeconds) {
if (key == null) return;
CacheEntry entry = container.get(key);
if (entry == null) return;
entry.lastAccessed = (int) (System.currentTimeMillis() / 1000);
entry.expireSeconds = expireSeconds;
}
@Override
public void refresh(final CompletionHandler<Void, K> handler, final K key) {
refresh(key);
public void refresh(final CompletionHandler<Void, K> handler, final K key, final int expireSeconds) {
refresh(key, expireSeconds);
if (handler != null) handler.completed(null, key);
}
@@ -325,13 +327,13 @@ public class CacheSourceService<K extends Serializable, V extends Object> implem
}
@Override
public Collection<V> getCollectionAndRefresh(final K key) {
return (Collection<V>) getAndRefresh(key);
public Collection<V> getCollectionAndRefresh(final K key, final int expireSeconds) {
return (Collection<V>) getAndRefresh(key, expireSeconds);
}
@Override
public void getCollectionAndRefresh(final CompletionHandler<Collection<V>, K> handler, @DynAttachment final K key) {
if (handler != null) handler.completed(getCollectionAndRefresh(key), key);
public void getCollectionAndRefresh(final CompletionHandler<Collection<V>, K> handler, @DynAttachment final K key, final int expireSeconds) {
if (handler != null) handler.completed(getCollectionAndRefresh(key, expireSeconds), key);
}
@Override

View File

@@ -13,10 +13,11 @@ import java.util.*;
*
* @param <K> key的类型
* @param <V> value的类型
* <p> 详情见: http://www.redkale.org
* <p>
* 详情见: http://www.redkale.org
* @author zhangjx
*/
public interface CacheSource<K extends Serializable, V extends Object> {
public interface CacheSource<K extends Serializable, V extends Object> {
default boolean isOpen() {
return true;
@@ -26,9 +27,9 @@ public interface CacheSource<K extends Serializable, V extends Object> {
public V get(final K key);
public V getAndRefresh(final K key);
public V getAndRefresh(final K key, final int expireSeconds);
public void refresh(final K key);
public void refresh(final K key, final int expireSeconds);
public void set(final K key, final V value);
@@ -40,7 +41,7 @@ public interface CacheSource<K extends Serializable, V extends Object> {
public Collection<V> getCollection(final K key);
public Collection<V> getCollectionAndRefresh(final K key);
public Collection<V> getCollectionAndRefresh(final K key, final int expireSeconds);
public void appendListItem(final K key, final V value);
@@ -55,9 +56,9 @@ public interface CacheSource<K extends Serializable, V extends Object> {
public void get(final CompletionHandler<V, K> handler, final K key);
public void getAndRefresh(final CompletionHandler<V, K> handler, final K key);
public void getAndRefresh(final CompletionHandler<V, K> handler, final K key, final int expireSeconds);
public void refresh(final CompletionHandler<Void, K> handler, final K key);
public void refresh(final CompletionHandler<Void, K> handler, final K key, final int expireSeconds);
public void set(final CompletionHandler<Void, K> handler, final K key, final V value);
@@ -69,7 +70,7 @@ public interface CacheSource<K extends Serializable, V extends Object> {
public void getCollection(final CompletionHandler<Collection<V>, K> handler, final K key);
public void getCollectionAndRefresh(final CompletionHandler<Collection<V>, K> handler, final K key);
public void getCollectionAndRefresh(final CompletionHandler<Collection<V>, K> handler, final K key, final int expireSeconds);
public void appendListItem(final CompletionHandler<Void, K> handler, final K key, final V value);