From 40c19c15218df2f501819e16b3d4a2c2221883fc Mon Sep 17 00:00:00 2001 From: Redkale <22250530@qq.com> Date: Fri, 10 Nov 2017 19:33:10 +0800 Subject: [PATCH] =?UTF-8?q?CacheSource=E5=A2=9E=E5=8A=A0incr=E6=88=96decr?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/org/redkale/source/CacheMemorySource.java | 51 +++++++++++++++++++ src/org/redkale/source/CacheSource.java | 18 ++++++- 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/src/org/redkale/source/CacheMemorySource.java b/src/org/redkale/source/CacheMemorySource.java index 54ea39806..d6c089ead 100644 --- a/src/org/redkale/source/CacheMemorySource.java +++ b/src/org/redkale/source/CacheMemorySource.java @@ -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 extends AbstractService impleme container.remove(key); } + @Override + public long incr(final String key) { + return incr(key, 1); + } + + @Override + public CompletableFuture 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 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 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 decrAsync(final String key, long num) { + return CompletableFuture.supplyAsync(() -> decr(key, num), getExecutor()); + } + @Override public CompletableFuture removeAsync(final String key) { return CompletableFuture.runAsync(() -> remove(key), getExecutor()); diff --git a/src/org/redkale/source/CacheSource.java b/src/org/redkale/source/CacheSource.java index 19d8724a2..f08297d75 100644 --- a/src/org/redkale/source/CacheSource.java +++ b/src/org/redkale/source/CacheSource.java @@ -23,7 +23,7 @@ import org.redkale.convert.json.JsonFactory; public interface CacheSource { public String getType(); - + public void initValueType(Type valueType); public void initTransient(boolean flag); @@ -48,6 +48,14 @@ public interface CacheSource { 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 getCollection(final String key); public int getCollectionSize(final String key); @@ -85,6 +93,14 @@ public interface CacheSource { public CompletableFuture removeAsync(final String key); + public CompletableFuture incrAsync(final String key); + + public CompletableFuture incrAsync(final String key, long num); + + public CompletableFuture decrAsync(final String key); + + public CompletableFuture decrAsync(final String key, long num); + public CompletableFuture> getCollectionAsync(final String key); public CompletableFuture getCollectionSizeAsync(final String key);