From 9158edb84ed12c2cab0bc8b8067770d2128c6dfa Mon Sep 17 00:00:00 2001 From: brido4125 Date: Thu, 4 Jan 2024 17:21:09 +0900 Subject: [PATCH] INTERNAL: Change asyncGets return future type. --- .../net/spy/memcached/ArcusClientPool.java | 4 +-- .../net/spy/memcached/MemcachedClient.java | 15 +++++------ .../internal/result/GetsResultImpl.java | 26 +++++++++++++++++++ 3 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 src/main/java/net/spy/memcached/internal/result/GetsResultImpl.java diff --git a/src/main/java/net/spy/memcached/ArcusClientPool.java b/src/main/java/net/spy/memcached/ArcusClientPool.java index 5b307a4bf..f2f7ff660 100644 --- a/src/main/java/net/spy/memcached/ArcusClientPool.java +++ b/src/main/java/net/spy/memcached/ArcusClientPool.java @@ -234,12 +234,12 @@ public GetFuture asyncGet(String key) { } @Override - public OperationFuture> asyncGets(String key, Transcoder tc) { + public GetFuture> asyncGets(String key, Transcoder tc) { return this.getClient().asyncGets(key, tc); } @Override - public OperationFuture> asyncGets(String key) { + public GetFuture> asyncGets(String key) { return this.getClient().asyncGets(key); } diff --git a/src/main/java/net/spy/memcached/MemcachedClient.java b/src/main/java/net/spy/memcached/MemcachedClient.java index 638c3d4a4..536176bf0 100644 --- a/src/main/java/net/spy/memcached/MemcachedClient.java +++ b/src/main/java/net/spy/memcached/MemcachedClient.java @@ -51,6 +51,7 @@ import net.spy.memcached.internal.GetFuture; import net.spy.memcached.internal.OperationFuture; import net.spy.memcached.internal.SingleElementInfiniteIterator; +import net.spy.memcached.internal.result.GetsResultImpl; import net.spy.memcached.internal.result.GetResult; import net.spy.memcached.internal.result.GetResultImpl; import net.spy.memcached.ops.CASOperationStatus; @@ -936,16 +937,15 @@ public GetFuture asyncGet(final String key) { * @throws IllegalStateException in the rare circumstance where queue * is too full to accept any more requests */ - public OperationFuture> asyncGets(final String key, + public GetFuture> asyncGets(final String key, final Transcoder tc) { final CountDownLatch latch = new CountDownLatch(1); - final OperationFuture> rv = - new OperationFuture>(latch, operationTimeout); + final GetFuture> rv = new GetFuture>(latch, operationTimeout); Operation op = opFact.gets(key, new GetsOperation.Callback() { - private CASValue val = null; + private GetResult> val = null; public void receivedStatus(OperationStatus status) { rv.set(val, status); @@ -954,8 +954,7 @@ public void receivedStatus(OperationStatus status) { public void gotData(String k, int flags, long cas, byte[] data) { assert key.equals(k) : "Wrong key returned"; assert cas > 0 : "CAS was less than zero: " + cas; - val = new CASValue(cas, tc.decode( - new CachedData(flags, data, tc.getMaxSize()))); + val = new GetsResultImpl(cas, new CachedData(flags, data, tc.getMaxSize()), tc); } public void complete() { @@ -976,7 +975,7 @@ public void complete() { * @throws IllegalStateException in the rare circumstance where queue * is too full to accept any more requests */ - public OperationFuture> asyncGets(final String key) { + public GetFuture> asyncGets(final String key) { return asyncGets(key, transcoder); } @@ -993,7 +992,7 @@ public OperationFuture> asyncGets(final String key) { * is too full to accept any more requests */ public CASValue gets(String key, Transcoder tc) { - OperationFuture> future = asyncGets(key, tc); + GetFuture> future = asyncGets(key, tc); try { return future.get(operationTimeout, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { diff --git a/src/main/java/net/spy/memcached/internal/result/GetsResultImpl.java b/src/main/java/net/spy/memcached/internal/result/GetsResultImpl.java new file mode 100644 index 000000000..e06fa6ec7 --- /dev/null +++ b/src/main/java/net/spy/memcached/internal/result/GetsResultImpl.java @@ -0,0 +1,26 @@ +package net.spy.memcached.internal.result; + +import net.spy.memcached.CASValue; +import net.spy.memcached.CachedData; +import net.spy.memcached.transcoders.Transcoder; + +public class GetsResultImpl implements GetResult> { + private final long cas; + private final CachedData cachedData; + private final Transcoder transcoder; + private volatile CASValue decodedValue = null; + + public GetsResultImpl(long cas, CachedData cachedData, Transcoder transcoder) { + this.cas = cas; + this.cachedData = cachedData; + this.transcoder = transcoder; + } + + @Override + public CASValue getDecodedValue() { + if (decodedValue == null) { + decodedValue = new CASValue(cas, transcoder.decode(cachedData)); + } + return decodedValue; + } +}