CacheSource 增加 setBytes 系列方法
This commit is contained in:
@@ -319,6 +319,8 @@ public final class CacheMemorySource<V extends Object> extends AbstractService i
|
||||
convertType = storeListType;
|
||||
} else if (entry.cacheType == CacheEntryType.OBJECT_SET) {
|
||||
convertType = storeSetType;
|
||||
} else if (entry.cacheType == CacheEntryType.BYTES) {
|
||||
convertType = byte[].class;
|
||||
}
|
||||
try {
|
||||
stream.println(convert.convertTo(convertType, entry));
|
||||
@@ -1505,6 +1507,74 @@ public final class CacheMemorySource<V extends Object> extends AbstractService i
|
||||
return CompletableFuture.supplyAsync(() -> removeLongSetItem(key, value), getExecutor()).whenComplete(futureCompleteConsumer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getBytes(final String key) {
|
||||
if (key == null) return null;
|
||||
CacheEntry entry = container.get(key);
|
||||
if (entry == null || entry.isExpired()) return null;
|
||||
return (byte[]) entry.objectValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<byte[]> getBytesAsync(final String key) {
|
||||
return CompletableFuture.supplyAsync(() -> getBytes(key), getExecutor()).whenComplete(futureCompleteConsumer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] getBytesAndRefresh(String key, final int expireSeconds) {
|
||||
if (key == null) return null;
|
||||
CacheEntry entry = container.get(key);
|
||||
if (entry == null || entry.isExpired()) return null;
|
||||
entry.lastAccessed = (int) (System.currentTimeMillis() / 1000);
|
||||
entry.expireSeconds = expireSeconds;
|
||||
return (byte[]) entry.objectValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<byte[]> getBytesAndRefreshAsync(final String key, final int expireSeconds) {
|
||||
return CompletableFuture.supplyAsync(() -> getBytesAndRefresh(key, expireSeconds), getExecutor()).whenComplete(futureCompleteConsumer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBytes(String key, byte[] value) {
|
||||
set(CacheEntryType.BYTES, key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> setBytesAsync(final String key, byte[] value) {
|
||||
return CompletableFuture.runAsync(() -> setBytes(key, value), getExecutor()).whenComplete(futureCompleteConsumer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBytes(final int expireSeconds, final String key, final byte[] value) {
|
||||
set(CacheEntryType.BYTES, expireSeconds, key, value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Void> setBytesAsync(final int expireSeconds, final String key, byte[] value) {
|
||||
return CompletableFuture.runAsync(() -> setBytes(expireSeconds, key, value), getExecutor()).whenComplete(futureCompleteConsumer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void setBytes(final String key, final Convert convert, final Type type, final T value) {
|
||||
set(CacheEntryType.BYTES, key, convert.convertToBytes(type, value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> CompletableFuture<Void> setBytesAsync(final String key, final Convert convert, final Type type, final T value) {
|
||||
return CompletableFuture.runAsync(() -> setBytes(key, convert, type, value), getExecutor()).whenComplete(futureCompleteConsumer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void setBytes(final int expireSeconds, final String key, final Convert convert, final Type type, final T value) {
|
||||
set(CacheEntryType.BYTES, expireSeconds, key, convert.convertToBytes(type, value));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> CompletableFuture<Void> setBytesAsync(final int expireSeconds, final String key, final Convert convert, final Type type, final T value) {
|
||||
return CompletableFuture.runAsync(() -> setBytes(expireSeconds, key, convert, type, value), getExecutor()).whenComplete(futureCompleteConsumer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> queryKeys() {
|
||||
return new ArrayList<>(container.keySet());
|
||||
|
||||
@@ -195,6 +195,18 @@ public interface CacheSource<V extends Object> {
|
||||
|
||||
public <T> List<T> spopSetItem(final String key, final int count, final Type componentType);
|
||||
|
||||
public byte[] getBytes(final String key);
|
||||
|
||||
public byte[] getBytesAndRefresh(final String key, final int expireSeconds);
|
||||
|
||||
public void setBytes(final String key, final byte[] value);
|
||||
|
||||
public void setBytes(final int expireSeconds, final String key, final byte[] value);
|
||||
|
||||
public <T> void setBytes(final String key, final Convert convert, final Type type, final T value);
|
||||
|
||||
public <T> void setBytes(final int expireSeconds, final String key, final Convert convert, final Type type, final T value);
|
||||
|
||||
public List<String> queryKeys();
|
||||
|
||||
public List<String> queryKeysStartsWith(String startsWith);
|
||||
@@ -427,6 +439,18 @@ public interface CacheSource<V extends Object> {
|
||||
|
||||
public <T> CompletableFuture<Integer> removeSetItemAsync(final String key, final Type componentType, final T value);
|
||||
|
||||
public CompletableFuture<byte[]> getBytesAsync(final String key);
|
||||
|
||||
public CompletableFuture<byte[]> getBytesAndRefreshAsync(final String key, final int expireSeconds);
|
||||
|
||||
public CompletableFuture<Void> setBytesAsync(final String key, final byte[] value);
|
||||
|
||||
public CompletableFuture<Void> setBytesAsync(final int expireSeconds, final String key, final byte[] value);
|
||||
|
||||
public <T> CompletableFuture<Void> setBytesAsync(final String key, final Convert convert, final Type type, final T value);
|
||||
|
||||
public <T> CompletableFuture<Void> setBytesAsync(final int expireSeconds, final String key, final Convert convert, final Type type, final T value);
|
||||
|
||||
public CompletableFuture<List<String>> queryKeysAsync();
|
||||
|
||||
public CompletableFuture<List<String>> queryKeysStartsWithAsync(String startsWith);
|
||||
@@ -506,7 +530,7 @@ public interface CacheSource<V extends Object> {
|
||||
}
|
||||
|
||||
public static enum CacheEntryType {
|
||||
LONG, STRING, OBJECT, ATOMIC, MAP,
|
||||
LONG, STRING, OBJECT, BYTES, ATOMIC, MAP,
|
||||
LONG_SET, STRING_SET, OBJECT_SET,
|
||||
LONG_LIST, STRING_LIST, OBJECT_LIST;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user