From 7cb60119db6539d50b57f3a4fc7b02dbb0bf7a43 Mon Sep 17 00:00:00 2001 From: dengliming Date: Tue, 23 Mar 2021 00:31:09 +0800 Subject: [PATCH] Add support for OBJECT FREQ --- .../java/io/lettuce/core/AbstractRedisAsyncCommands.java | 5 +++++ .../io/lettuce/core/AbstractRedisReactiveCommands.java | 5 +++++ src/main/java/io/lettuce/core/RedisCommandBuilder.java | 7 +++++++ .../io/lettuce/core/api/async/RedisKeyAsyncCommands.java | 9 +++++++++ .../core/api/reactive/RedisKeyReactiveCommands.java | 9 +++++++++ .../java/io/lettuce/core/api/sync/RedisKeyCommands.java | 9 +++++++++ .../cluster/api/async/NodeSelectionKeyAsyncCommands.java | 9 +++++++++ .../core/cluster/api/sync/NodeSelectionKeyCommands.java | 9 +++++++++ .../java/io/lettuce/core/protocol/CommandKeyword.java | 2 +- .../core/api/coroutines/RedisKeyCoroutinesCommands.kt | 9 +++++++++ .../api/coroutines/RedisKeyCoroutinesCommandsImpl.kt | 2 ++ .../templates/io/lettuce/core/api/RedisKeyCommands.java | 9 +++++++++ .../core/commands/KeyCommandIntegrationTests.java | 7 +++++++ 13 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java b/src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java index 92cdca01bd..0b85d97eec 100644 --- a/src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java +++ b/src/main/java/io/lettuce/core/AbstractRedisAsyncCommands.java @@ -1219,6 +1219,11 @@ public RedisFuture objectEncoding(K key) { return dispatch(commandBuilder.objectEncoding(key)); } + @Override + public RedisFuture objectFreq(K key) { + return dispatch(commandBuilder.objectFreq(key)); + } + @Override public RedisFuture objectIdletime(K key) { return dispatch(commandBuilder.objectIdletime(key)); diff --git a/src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java b/src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java index a2fa09306a..01c4280232 100644 --- a/src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java +++ b/src/main/java/io/lettuce/core/AbstractRedisReactiveCommands.java @@ -1284,6 +1284,11 @@ public Mono objectEncoding(K key) { return createMono(() -> commandBuilder.objectEncoding(key)); } + @Override + public Mono objectFreq(K key) { + return createMono(() -> commandBuilder.objectFreq(key)); + } + @Override public Mono objectIdletime(K key) { return createMono(() -> commandBuilder.objectIdletime(key)); diff --git a/src/main/java/io/lettuce/core/RedisCommandBuilder.java b/src/main/java/io/lettuce/core/RedisCommandBuilder.java index 82d6a0961c..5afb37b5e5 100644 --- a/src/main/java/io/lettuce/core/RedisCommandBuilder.java +++ b/src/main/java/io/lettuce/core/RedisCommandBuilder.java @@ -1697,6 +1697,13 @@ Command objectEncoding(K key) { return createCommand(OBJECT, new StatusOutput<>(codec), args); } + Command objectFreq(K key) { + notNullKey(key); + + CommandArgs args = new CommandArgs<>(codec).add(FREQ).addKey(key); + return createCommand(OBJECT, new IntegerOutput<>(codec), args); + } + Command objectIdletime(K key) { notNullKey(key); diff --git a/src/main/java/io/lettuce/core/api/async/RedisKeyAsyncCommands.java b/src/main/java/io/lettuce/core/api/async/RedisKeyAsyncCommands.java index ce1e921e18..4a7fc356c6 100644 --- a/src/main/java/io/lettuce/core/api/async/RedisKeyAsyncCommands.java +++ b/src/main/java/io/lettuce/core/api/async/RedisKeyAsyncCommands.java @@ -207,6 +207,15 @@ public interface RedisKeyAsyncCommands { */ RedisFuture objectEncoding(K key); + /** + * returns the logarithmic access frequency counter of the object stored at the specified key. + * + * @param key the key. + * @return Long. + * @since 6.1 + */ + RedisFuture objectFreq(K key); + /** * returns the number of seconds since the object stored at the specified key is idle (not requested by read or write * operations). diff --git a/src/main/java/io/lettuce/core/api/reactive/RedisKeyReactiveCommands.java b/src/main/java/io/lettuce/core/api/reactive/RedisKeyReactiveCommands.java index c261f11896..880f8c9542 100644 --- a/src/main/java/io/lettuce/core/api/reactive/RedisKeyReactiveCommands.java +++ b/src/main/java/io/lettuce/core/api/reactive/RedisKeyReactiveCommands.java @@ -209,6 +209,15 @@ public interface RedisKeyReactiveCommands { */ Mono objectEncoding(K key); + /** + * returns the logarithmic access frequency counter of the object stored at the specified key. + * + * @param key the key. + * @return Long. + * @since 6.1 + */ + Mono objectFreq(K key); + /** * returns the number of seconds since the object stored at the specified key is idle (not requested by read or write * operations). diff --git a/src/main/java/io/lettuce/core/api/sync/RedisKeyCommands.java b/src/main/java/io/lettuce/core/api/sync/RedisKeyCommands.java index 5a6328d123..4f0f135a65 100644 --- a/src/main/java/io/lettuce/core/api/sync/RedisKeyCommands.java +++ b/src/main/java/io/lettuce/core/api/sync/RedisKeyCommands.java @@ -206,6 +206,15 @@ public interface RedisKeyCommands { */ String objectEncoding(K key); + /** + * returns the logarithmic access frequency counter of the object stored at the specified key. + * + * @param key the key. + * @return Long. + * @since 6.1 + */ + Long objectFreq(K key); + /** * returns the number of seconds since the object stored at the specified key is idle (not requested by read or write * operations). diff --git a/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionKeyAsyncCommands.java b/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionKeyAsyncCommands.java index 8b1fdc3239..d30748106d 100644 --- a/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionKeyAsyncCommands.java +++ b/src/main/java/io/lettuce/core/cluster/api/async/NodeSelectionKeyAsyncCommands.java @@ -206,6 +206,15 @@ public interface NodeSelectionKeyAsyncCommands { */ AsyncExecutions objectEncoding(K key); + /** + * returns the logarithmic access frequency counter of the object stored at the specified key. + * + * @param key the key. + * @return Long. + * @since 6.1 + */ + AsyncExecutions objectFreq(K key); + /** * returns the number of seconds since the object stored at the specified key is idle (not requested by read or write * operations). diff --git a/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionKeyCommands.java b/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionKeyCommands.java index 52fc65f095..2c46490917 100644 --- a/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionKeyCommands.java +++ b/src/main/java/io/lettuce/core/cluster/api/sync/NodeSelectionKeyCommands.java @@ -206,6 +206,15 @@ public interface NodeSelectionKeyCommands { */ Executions objectEncoding(K key); + /** + * returns the logarithmic access frequency counter of the object stored at the specified key. + * + * @param key the key. + * @return Long. + * @since 6.1 + */ + Executions objectFreq(K key); + /** * returns the number of seconds since the object stored at the specified key is idle (not requested by read or write * operations). diff --git a/src/main/java/io/lettuce/core/protocol/CommandKeyword.java b/src/main/java/io/lettuce/core/protocol/CommandKeyword.java index 17c0fb6b27..12e2f3e349 100644 --- a/src/main/java/io/lettuce/core/protocol/CommandKeyword.java +++ b/src/main/java/io/lettuce/core/protocol/CommandKeyword.java @@ -31,7 +31,7 @@ public enum CommandKeyword implements ProtocolKeyword { BY, BYLEX, BYSCORE, CACHING, CAT, CH, CHANNELS, COPY, COUNT, COUNTKEYSINSLOT, CONSUMERS, CREATE, DB, DELSLOTS, DELUSER, DESC, SOFT, HARD, ENCODING, - FAILOVER, FORGET, FLUSH, FORCE, FLUSHSLOTS, GENPASS, GETNAME, GETUSER, GETKEYSINSLOT, GETREDIR, GROUP, GROUPS, HTSTATS, ID, IDLE, + FAILOVER, FORGET, FLUSH, FORCE, FREQ, FLUSHSLOTS, GENPASS, GETNAME, GETUSER, GETKEYSINSLOT, GETREDIR, GROUP, GROUPS, HTSTATS, ID, IDLE, IDLETIME, JUSTID, KILL, KEYSLOT, LEFT, LEN, LIMIT, LIST, LOAD, LOG, MATCH, diff --git a/src/main/kotlin/io/lettuce/core/api/coroutines/RedisKeyCoroutinesCommands.kt b/src/main/kotlin/io/lettuce/core/api/coroutines/RedisKeyCoroutinesCommands.kt index bfd04c42a0..d1b75d0cc0 100644 --- a/src/main/kotlin/io/lettuce/core/api/coroutines/RedisKeyCoroutinesCommands.kt +++ b/src/main/kotlin/io/lettuce/core/api/coroutines/RedisKeyCoroutinesCommands.kt @@ -191,6 +191,15 @@ interface RedisKeyCoroutinesCommands { */ suspend fun objectEncoding(key: K): String? + /** + * returns the logarithmic access frequency counter of the object stored at the specified key. + * + * @param key the key. + * @return Long. + * @since 6.1 + */ + suspend fun objectFreq(key: K): Long? + /** * returns the number of seconds since the object stored at the specified key is idle (not requested by read or write * operations). diff --git a/src/main/kotlin/io/lettuce/core/api/coroutines/RedisKeyCoroutinesCommandsImpl.kt b/src/main/kotlin/io/lettuce/core/api/coroutines/RedisKeyCoroutinesCommandsImpl.kt index ca22916c8b..570a6e43d5 100644 --- a/src/main/kotlin/io/lettuce/core/api/coroutines/RedisKeyCoroutinesCommandsImpl.kt +++ b/src/main/kotlin/io/lettuce/core/api/coroutines/RedisKeyCoroutinesCommandsImpl.kt @@ -68,6 +68,8 @@ internal class RedisKeyCoroutinesCommandsImpl(internal val ops override suspend fun objectEncoding(key: K): String? = ops.objectEncoding(key).awaitFirstOrNull() + override suspend fun objectFreq(key: K): Long? = ops.objectFreq(key).awaitFirstOrNull() + override suspend fun objectIdletime(key: K): Long? = ops.objectIdletime(key).awaitFirstOrNull() override suspend fun objectRefcount(key: K): Long? = ops.objectRefcount(key).awaitFirstOrNull() diff --git a/src/main/templates/io/lettuce/core/api/RedisKeyCommands.java b/src/main/templates/io/lettuce/core/api/RedisKeyCommands.java index 602a7ed23a..ed0b005a52 100644 --- a/src/main/templates/io/lettuce/core/api/RedisKeyCommands.java +++ b/src/main/templates/io/lettuce/core/api/RedisKeyCommands.java @@ -200,6 +200,15 @@ public interface RedisKeyCommands { */ String objectEncoding(K key); + /** + * returns the logarithmic access frequency counter of the object stored at the specified key. + * + * @param key the key. + * @return Long. + * @since 6.1 + */ + Long objectFreq(K key); + /** * returns the number of seconds since the object stored at the specified key is idle (not requested by read or write * operations). diff --git a/src/test/java/io/lettuce/core/commands/KeyCommandIntegrationTests.java b/src/test/java/io/lettuce/core/commands/KeyCommandIntegrationTests.java index 12cc5bf2e7..debc773c0b 100644 --- a/src/test/java/io/lettuce/core/commands/KeyCommandIntegrationTests.java +++ b/src/test/java/io/lettuce/core/commands/KeyCommandIntegrationTests.java @@ -210,6 +210,13 @@ void objectEncoding() { assertThat(redis.objectEncoding(key)).isEqualTo("int"); } + @Test + void objectFreq() { + redis.configSet("maxmemory-policy", "allkeys-lfu"); + redis.set(key, value); + assertThat(redis.objectFreq(key)).isGreaterThan(0); + } + @Test void objectIdletime() { redis.set(key, value);