CacheSource增加incr或decr方法
This commit is contained in:
@@ -9,6 +9,7 @@ import java.io.*;
|
|||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.*;
|
import java.util.concurrent.*;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
import java.util.logging.*;
|
import java.util.logging.*;
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
@@ -336,6 +337,56 @@ public class CacheMemorySource<V extends Object> extends AbstractService impleme
|
|||||||
container.remove(key);
|
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
|
@Override
|
||||||
public CompletableFuture<Void> removeAsync(final String key) {
|
public CompletableFuture<Void> removeAsync(final String key) {
|
||||||
return CompletableFuture.runAsync(() -> remove(key), getExecutor());
|
return CompletableFuture.runAsync(() -> remove(key), getExecutor());
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ import org.redkale.convert.json.JsonFactory;
|
|||||||
public interface CacheSource<V extends Object> {
|
public interface CacheSource<V extends Object> {
|
||||||
|
|
||||||
public String getType();
|
public String getType();
|
||||||
|
|
||||||
public void initValueType(Type valueType);
|
public void initValueType(Type valueType);
|
||||||
|
|
||||||
public void initTransient(boolean flag);
|
public void initTransient(boolean flag);
|
||||||
@@ -48,6 +48,14 @@ public interface CacheSource<V extends Object> {
|
|||||||
|
|
||||||
public void remove(final String key);
|
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 Collection<V> getCollection(final String key);
|
||||||
|
|
||||||
public int getCollectionSize(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<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<Collection<V>> getCollectionAsync(final String key);
|
||||||
|
|
||||||
public CompletableFuture<Integer> getCollectionSizeAsync(final String key);
|
public CompletableFuture<Integer> getCollectionSizeAsync(final String key);
|
||||||
|
|||||||
Reference in New Issue
Block a user