CacheSource增加incr或decr方法

This commit is contained in:
Redkale
2017-11-10 19:33:10 +08:00
parent 7089fae390
commit 40c19c1521
2 changed files with 68 additions and 1 deletions

View File

@@ -9,6 +9,7 @@ import java.io.*;
import java.lang.reflect.Type;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
import java.util.logging.*;
import javax.annotation.Resource;
@@ -336,6 +337,56 @@ public class CacheMemorySource<V extends Object> extends AbstractService impleme
container.remove(key);
}
@Override
public long incr(final String key) {
return incr(key, 1);
}
@Override
public CompletableFuture<Long> incrAsync(final String key) {
return CompletableFuture.supplyAsync(() -> incr(key), getExecutor());
}
@Override
public long incr(final String key, long num) {
CacheEntry entry = container.get(key);
if (entry == null) {
synchronized (container) {
entry = container.get(key);
if (entry == null) {
entry = new CacheEntry(CacheEntryType.OBJECT, key, new AtomicLong(), null, null);
container.put(key, entry);
}
}
}
return ((AtomicLong) entry.objectValue).addAndGet(num);
}
@Override
public CompletableFuture<Long> incrAsync(final String key, long num) {
return CompletableFuture.supplyAsync(() -> incr(key, num), getExecutor());
}
@Override
public long decr(final String key) {
return incr(key, -1);
}
@Override
public CompletableFuture<Long> decrAsync(final String key) {
return CompletableFuture.supplyAsync(() -> decr(key), getExecutor());
}
@Override
public long decr(final String key, long num) {
return incr(key, -num);
}
@Override
public CompletableFuture<Long> decrAsync(final String key, long num) {
return CompletableFuture.supplyAsync(() -> decr(key, num), getExecutor());
}
@Override
public CompletableFuture<Void> removeAsync(final String key) {
return CompletableFuture.runAsync(() -> remove(key), getExecutor());

View File

@@ -23,7 +23,7 @@ import org.redkale.convert.json.JsonFactory;
public interface CacheSource<V extends Object> {
public String getType();
public void initValueType(Type valueType);
public void initTransient(boolean flag);
@@ -48,6 +48,14 @@ public interface CacheSource<V extends Object> {
public void remove(final String key);
public long incr(final String key);
public long incr(final String key, long num);
public long decr(final String key);
public long decr(final String key, long num);
public Collection<V> getCollection(final String key);
public int getCollectionSize(final String key);
@@ -85,6 +93,14 @@ public interface CacheSource<V extends Object> {
public CompletableFuture<Void> removeAsync(final String key);
public CompletableFuture<Long> incrAsync(final String key);
public CompletableFuture<Long> incrAsync(final String key, long num);
public CompletableFuture<Long> decrAsync(final String key);
public CompletableFuture<Long> decrAsync(final String key, long num);
public CompletableFuture<Collection<V>> getCollectionAsync(final String key);
public CompletableFuture<Integer> getCollectionSizeAsync(final String key);