From 6b4ade0daff5159ce524eb8057a3d8938f67e4bf Mon Sep 17 00:00:00 2001 From: amcreynolds Date: Thu, 16 Nov 2017 12:04:48 -0800 Subject: [PATCH] Support Get And Touch (GATS) for ASCII Protocol Adds support for the "gats" command recently introduced in memcached 1.5.3. To avoid causing any interface changes to MemcachedClientIF, only the single key "gats" scenario is supported. Multi-key and "gat" (i.e. "gats" with no cas return value) will be included in a different pull request. See: * https://github.com/memcached/memcached/commit/7f4e0246e5c27baa9a7a690e5905f5ee56b80ece * https://github.com/memcached/memcached/wiki/ReleaseNotes153 --- .../protocol/ascii/AsciiOperationFactory.java | 6 ++++-- .../memcached/protocol/ascii/BaseGetOpImpl.java | 17 +++++++++++++---- .../ascii/GetAndTouchOperationImpl.java | 11 +++++++++++ .../protocol/ascii/AsciiToStringTest.java | 2 +- 4 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/main/java/net/spy/memcached/protocol/ascii/AsciiOperationFactory.java b/src/main/java/net/spy/memcached/protocol/ascii/AsciiOperationFactory.java index 8e877de54..ba27d1c2c 100644 --- a/src/main/java/net/spy/memcached/protocol/ascii/AsciiOperationFactory.java +++ b/src/main/java/net/spy/memcached/protocol/ascii/AsciiOperationFactory.java @@ -81,10 +81,12 @@ public FlushOperation flush(int delay, OperationCallback cb) { return new FlushOperationImpl(delay, cb); } + /** + * Get And Touch is only supported in memcached 1.5.3 and later. + */ public GetAndTouchOperation getAndTouch(String key, int expiration, GetAndTouchOperation.Callback cb) { - throw new UnsupportedOperationException("Get and touch is not supported " - + "for ASCII protocol"); + return new GetAndTouchOperationImpl(key, expiration, cb); } public GetOperation get(String key, GetOperation.Callback cb) { diff --git a/src/main/java/net/spy/memcached/protocol/ascii/BaseGetOpImpl.java b/src/main/java/net/spy/memcached/protocol/ascii/BaseGetOpImpl.java index 33af95c04..817ff93e5 100644 --- a/src/main/java/net/spy/memcached/protocol/ascii/BaseGetOpImpl.java +++ b/src/main/java/net/spy/memcached/protocol/ascii/BaseGetOpImpl.java @@ -203,11 +203,20 @@ public final void initialize() { size += afterKeyBytesSize(); ByteBuffer b = ByteBuffer.allocate(size); b.put(cmd.getBytes()); - for (byte[] k : keyBytes) { - b.put((byte) ' '); - b.put(k); + if (cmd.equals("gats")) { + afterKeyBytes(b); + for (byte[] k : keyBytes) { + b.put((byte) ' '); + b.put(k); + } + } + else { + for (byte[] k : keyBytes) { + b.put((byte) ' '); + b.put(k); + } + afterKeyBytes(b); } - afterKeyBytes(b); b.put(RN_BYTES); b.flip(); setBuffer(b); diff --git a/src/main/java/net/spy/memcached/protocol/ascii/GetAndTouchOperationImpl.java b/src/main/java/net/spy/memcached/protocol/ascii/GetAndTouchOperationImpl.java index 725bacb25..bc68ece9d 100644 --- a/src/main/java/net/spy/memcached/protocol/ascii/GetAndTouchOperationImpl.java +++ b/src/main/java/net/spy/memcached/protocol/ascii/GetAndTouchOperationImpl.java @@ -30,11 +30,22 @@ public class GetAndTouchOperationImpl extends BaseGetOpImpl implements GetAndTouchOperation { + private static final String CMD = "gats"; + + /** + * @deprecated use {@link #GetAndTouchOperationImpl(String, int, + * net.spy.memcached.ops.GetAndTouchOperation.Callback)} + */ public GetAndTouchOperationImpl(String c, int e, GetAndTouchOperation.Callback cb, String k) { super(c, e, cb, k); } + public GetAndTouchOperationImpl(String k, int e, + GetAndTouchOperation.Callback cb) { + super(CMD, e, cb, k); + } + @Override public int getExpiration() { return exp; diff --git a/src/test/java/net/spy/memcached/protocol/ascii/AsciiToStringTest.java b/src/test/java/net/spy/memcached/protocol/ascii/AsciiToStringTest.java index 3ecb7a6c0..907d36729 100644 --- a/src/test/java/net/spy/memcached/protocol/ascii/AsciiToStringTest.java +++ b/src/test/java/net/spy/memcached/protocol/ascii/AsciiToStringTest.java @@ -46,7 +46,7 @@ public void testFlush() { } public void testGetAndTouch() { - (new GetAndTouchOperationImpl("gat", 15, null, "key")).toString(); + (new GetAndTouchOperationImpl("key", 15, null)).toString(); } public void testTouch() {