Redis는 데이터베이스, 캐시 또는 메시지 브로커로 사용할 수 있는 인메모리 key-value 저장소입니다. 데이터는 Redis의 인메모리 데이터 구조에 있는 키에서 작동하여 추가, 쿼리, 수정 및 삭제됩니다.
Jedis와 Lettuce의 가장 중요한 차이점은 Java 8의 CompleteStage 인터페이스를 통한 비동기식 지원 및 Reactive Streams 지원입니다. Lettuce는 Redis 데이터베이스 서버에서 비동기 요청을 하고 스트림을 생성할 수 있는 자연스러운 인터페이스를 제공합니다.
또한 서버와 통신하는 데도 사용합니다. 이렇게 하면 "무거운" API를 만들 수 있지만 둘 이상의 스레드와 연결을 공유하는 데 더 적합합니다.
pom.xml에 필요한 종속성을 추가합니다.
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
<version>5.2.0.RELEASE</version>
</dependency>
Redis에 연결순서는 다음과 같습니다.
- Redis URI 생성
- URI를 사용하여 RedisClient에 연결
- Redis Connection 열기
- RedisCommants 생성
RedisClient redisClient = RedisClient.create(RedisURI.Builder.withHost(host).withPort(port).withDatabase(database).build());
StatefulRedisConnection<String, String> connection = redisClient.connect();
StatefulRedisConnection는 Redis 서버에 대한 thread-safe 연결로, 서버에 대한 연결을 유지하고 필요할 경우 다시 연결합니다. 일단 연결이 되면, 동기/비동기식으로 Redis 명령을 실행할 수 있습니다.
RedisClient는 Redis 서버와 통신하기 위한 Netty 리소스를 보유하기 때문에 상당한 시스템 리소스를 사용합니다. 다중 연결이 필요한 응용 프로그램은 단일 RedisClient를 사용해야 합니다.
Jedis와 비슷하게 Lettuce는 메서드의 형태로 완전한 Redis 명령 집합을 제공합니다.
아래와 같이 설정해주면 Redis와 동기식으로 통신하기 위한 인터페이스를 얻게 됩니다.
RedisCommands<String, String> redisCommands = connection.sync();
String value를 설정, 조회할 수 있고
redisCommands.set("key", "Hello, Redis!");
String value = redisCommands.get("key");
hash로 설정할 수 있습니다.
redisCommands.hset("recordName", "FirstName", "John");
redisCommands.hset("recordName", "LastName", "Smith");
Map<String, String> record = redisCommands.hgetall("recordName");
Lettuce의 동기식 API는 비동기식 API를 사용합니다. Blocking은 command level에서 수행됩니다. 즉, 둘 이상의 클라이언트가 동기식 연결을 공유할 수 있습니다.
RedisCommands 인터페이스는 다음과 같은 인터페이스들을 상속받습니다.
package io.lettuce.core.api.sync;
/**
* A complete synchronous and thread-safe Redis API with 400+ Methods.
*/
public interface RedisCommands<K, V> extends BaseRedisCommands<K, V>, RedisClusterCommands<K, V>, RedisGeoCommands<K, V>, RedisHashCommands<K, V>, RedisHLLCommands<K, V>, RedisKeyCommands<K, V>, RedisListCommands<K, V>, RedisScriptingCommands<K, V>, RedisServerCommands<K, V>, RedisSetCommands<K, V>, RedisSortedSetCommands<K, V>, RedisStreamCommands<K, V>, RedisStringCommands<K, V>, RedisTransactionalCommands<K, V> {
}
- String
// String value 설정
redisCommands.set(key, value);
// key의 String value 조회
String value = redisCommands.get(key);
package io.lettuce.core.api.sync;
/**
* Synchronous executed commands for Strings.
*
* @param <K> Key type.
* @param <V> Value type.
* @author Mark Paluch
* @since 4.0
* @generated by io.lettuce.apigenerator.CreateSyncApi
*/
public interface RedisStringCommands<K, V> {
/**
* Append a value to a key.
*
* @param key the key
* @param value the value
* @return Long integer-reply the length of the string after the append operation.
*/
Long append(K key, V value);
/**
* Count set bits in a string.
*
* @param key the key
*
* @return Long integer-reply The number of bits set to 1.
*/
Long bitcount(K key);
/**
* Count set bits in a string.
*
* @param key the key
* @param start the start
* @param end the end
*
* @return Long integer-reply The number of bits set to 1.
*/
Long bitcount(K key, long start, long end);
/**
* Execute {@code BITFIELD} with its subcommands.
*
* @param key the key
* @param bitFieldArgs the args containing subcommands, must not be {@literal null}.
*
* @return Long bulk-reply the results from the bitfield commands.
*/
List<Long> bitfield(K key, BitFieldArgs bitFieldArgs);
/**
* Find first bit set or clear in a string.
*
* @param key the key
* @param state the state
*
* @return Long integer-reply The command returns the position of the first bit set to 1 or 0 according to the request.
*
* If we look for set bits (the bit argument is 1) and the string is empty or composed of just zero bytes, -1 is
* returned.
*
* If we look for clear bits (the bit argument is 0) and the string only contains bit set to 1, the function returns
* the first bit not part of the string on the right. So if the string is tree bytes set to the value 0xff the
* command {@code BITPOS key 0} will return 24, since up to bit 23 all the bits are 1.
*
* Basically the function consider the right of the string as padded with zeros if you look for clear bits and
* specify no range or the <em>start</em> argument <strong>only</strong>.
*/
Long bitpos(K key, boolean state);
/**
* Find first bit set or clear in a string.
*
* @param key the key
* @param state the bit type: long
* @param start the start type: long
* @return Long integer-reply The command returns the position of the first bit set to 1 or 0 according to the request.
*
* If we look for set bits (the bit argument is 1) and the string is empty or composed of just zero bytes, -1 is
* returned.
*
* If we look for clear bits (the bit argument is 0) and the string only contains bit set to 1, the function returns
* the first bit not part of the string on the right. So if the string is tree bytes set to the value 0xff the
* command {@code BITPOS key 0} will return 24, since up to bit 23 all the bits are 1.
*
* Basically the function consider the right of the string as padded with zeros if you look for clear bits and
* specify no range or the <em>start</em> argument <strong>only</strong>.
* @since 5.0.1
*/
Long bitpos(K key, boolean state, long start);
/**
* Find first bit set or clear in a string.
*
* @param key the key
* @param state the bit type: long
* @param start the start type: long
* @param end the end type: long
* @return Long integer-reply The command returns the position of the first bit set to 1 or 0 according to the request.
*
* If we look for set bits (the bit argument is 1) and the string is empty or composed of just zero bytes, -1 is
* returned.
*
* If we look for clear bits (the bit argument is 0) and the string only contains bit set to 1, the function returns
* the first bit not part of the string on the right. So if the string is tree bytes set to the value 0xff the
* command {@code BITPOS key 0} will return 24, since up to bit 23 all the bits are 1.
*
* Basically the function consider the right of the string as padded with zeros if you look for clear bits and
* specify no range or the <em>start</em> argument <strong>only</strong>.
*
* However this behavior changes if you are looking for clear bits and specify a range with both
* <strong>start</strong> and <strong>end</strong>. If no clear bit is found in the specified range, the function
* returns -1 as the user specified a clear range and there are no 0 bits in that range.
*/
Long bitpos(K key, boolean state, long start, long end);
/**
* Perform bitwise AND between strings.
*
* @param destination result key of the operation
* @param keys operation input key names
* @return Long integer-reply The size of the string stored in the destination key, that is equal to the size of the longest
* input string.
*/
Long bitopAnd(K destination, K... keys);
/**
* Perform bitwise NOT between strings.
*
* @param destination result key of the operation
* @param source operation input key names
* @return Long integer-reply The size of the string stored in the destination key, that is equal to the size of the longest
* input string.
*/
Long bitopNot(K destination, K source);
/**
* Perform bitwise OR between strings.
*
* @param destination result key of the operation
* @param keys operation input key names
* @return Long integer-reply The size of the string stored in the destination key, that is equal to the size of the longest
* input string.
*/
Long bitopOr(K destination, K... keys);
/**
* Perform bitwise XOR between strings.
*
* @param destination result key of the operation
* @param keys operation input key names
* @return Long integer-reply The size of the string stored in the destination key, that is equal to the size of the longest
* input string.
*/
Long bitopXor(K destination, K... keys);
/**
* Decrement the integer value of a key by one.
*
* @param key the key
* @return Long integer-reply the value of {@code key} after the decrement
*/
Long decr(K key);
/**
* Decrement the integer value of a key by the given number.
*
* @param key the key
* @param amount the decrement type: long
* @return Long integer-reply the value of {@code key} after the decrement
*/
Long decrby(K key, long amount);
/**
* Get the value of a key.
*
* @param key the key
* @return V bulk-string-reply the value of {@code key}, or {@literal null} when {@code key} does not exist.
*/
V get(K key);
/**
* Returns the bit value at offset in the string value stored at key.
*
* @param key the key
* @param offset the offset type: long
* @return Long integer-reply the bit value stored at <em>offset</em>.
*/
Long getbit(K key, long offset);
/**
* Get a substring of the string stored at a key.
*
* @param key the key
* @param start the start type: long
* @param end the end type: long
* @return V bulk-string-reply
*/
V getrange(K key, long start, long end);
/**
* Set the string value of a key and return its old value.
*
* @param key the key
* @param value the value
* @return V bulk-string-reply the old value stored at {@code key}, or {@literal null} when {@code key} did not exist.
*/
V getset(K key, V value);
/**
* Increment the integer value of a key by one.
*
* @param key the key
* @return Long integer-reply the value of {@code key} after the increment
*/
Long incr(K key);
/**
* Increment the integer value of a key by the given amount.
*
* @param key the key
* @param amount the increment type: long
* @return Long integer-reply the value of {@code key} after the increment
*/
Long incrby(K key, long amount);
/**
* Increment the float value of a key by the given amount.
*
* @param key the key
* @param amount the increment type: double
* @return Double bulk-string-reply the value of {@code key} after the increment.
*/
Double incrbyfloat(K key, double amount);
/**
* Get the values of all the given keys.
*
* @param keys the key
* @return List<V> array-reply list of values at the specified keys.
*/
List<KeyValue<K, V>> mget(K... keys);
/**
* Stream over the values of all the given keys.
*
* @param channel the channel
* @param keys the keys
*
* @return Long array-reply list of values at the specified keys.
*/
Long mget(KeyValueStreamingChannel<K, V> channel, K... keys);
/**
* Set multiple keys to multiple values.
*
* @param map the null
* @return String simple-string-reply always {@code OK} since {@code MSET} can't fail.
*/
String mset(Map<K, V> map);
/**
* Set multiple keys to multiple values, only if none of the keys exist.
*
* @param map the null
* @return Boolean integer-reply specifically:
*
* {@code 1} if the all the keys were set. {@code 0} if no key was set (at least one key already existed).
*/
Boolean msetnx(Map<K, V> map);
/**
* Set the string value of a key.
*
* @param key the key
* @param value the value
*
* @return String simple-string-reply {@code OK} if {@code SET} was executed correctly.
*/
String set(K key, V value);
/**
* Set the string value of a key.
*
* @param key the key
* @param value the value
* @param setArgs the setArgs
*
* @return String simple-string-reply {@code OK} if {@code SET} was executed correctly.
*/
String set(K key, V value, SetArgs setArgs);
/**
* Sets or clears the bit at offset in the string value stored at key.
*
* @param key the key
* @param offset the offset type: long
* @param value the value type: string
* @return Long integer-reply the original bit value stored at <em>offset</em>.
*/
Long setbit(K key, long offset, int value);
/**
* Set the value and expiration of a key.
*
* @param key the key
* @param seconds the seconds type: long
* @param value the value
* @return String simple-string-reply
*/
String setex(K key, long seconds, V value);
/**
* Set the value and expiration in milliseconds of a key.
*
* @param key the key
* @param milliseconds the milliseconds type: long
* @param value the value
* @return String simple-string-reply
*/
String psetex(K key, long milliseconds, V value);
/**
* Set the value of a key, only if the key does not exist.
*
* @param key the key
* @param value the value
* @return Boolean integer-reply specifically:
*
* {@code 1} if the key was set {@code 0} if the key was not set
*/
Boolean setnx(K key, V value);
/**
* Overwrite part of a string at key starting at the specified offset.
*
* @param key the key
* @param offset the offset type: long
* @param value the value
* @return Long integer-reply the length of the string after it was modified by the command.
*/
Long setrange(K key, long offset, V value);
/**
* Get the length of the value stored in a key.
*
* @param key the key
* @return Long integer-reply the length of the string at {@code key}, or {@code 0} when {@code key} does not exist.
*/
Long strlen(K key);
}
- Set
// set에 member 추가
redisCommands.sadd(key, member1, ...);
// key에 sey의 member가 존재하는지 확인
Boolean isMember = redisCommands.sismember(key, member);
package io.lettuce.core.api.sync;
/**
* Synchronous executed commands for Sets.
*
* @param <K> Key type.
* @param <V> Value type.
* @author Mark Paluch
* @since 4.0
* @generated by io.lettuce.apigenerator.CreateSyncApi
*/
public interface RedisSetCommands<K, V> {
/**
* Add one or more members to a set.
*
* @param key the key
* @param members the member type: value
* @return Long integer-reply the number of elements that were added to the set, not including all the elements already
* present into the set.
*/
Long sadd(K key, V... members);
/**
* Get the number of members in a set.
*
* @param key the key
* @return Long integer-reply the cardinality (number of elements) of the set, or {@literal false} if {@code key} does not
* exist.
*/
Long scard(K key);
/**
* Subtract multiple sets.
*
* @param keys the key
* @return Set<V> array-reply list with members of the resulting set.
*/
Set<V> sdiff(K... keys);
/**
* Subtract multiple sets.
*
* @param channel the channel
* @param keys the keys
* @return Long count of members of the resulting set.
*/
Long sdiff(ValueStreamingChannel<V> channel, K... keys);
/**
* Subtract multiple sets and store the resulting set in a key.
*
* @param destination the destination type: key
* @param keys the key
* @return Long integer-reply the number of elements in the resulting set.
*/
Long sdiffstore(K destination, K... keys);
/**
* Intersect multiple sets.
*
* @param keys the key
* @return Set<V> array-reply list with members of the resulting set.
*/
Set<V> sinter(K... keys);
/**
* Intersect multiple sets.
*
* @param channel the channel
* @param keys the keys
* @return Long count of members of the resulting set.
*/
Long sinter(ValueStreamingChannel<V> channel, K... keys);
/**
* Intersect multiple sets and store the resulting set in a key.
*
* @param destination the destination type: key
* @param keys the key
* @return Long integer-reply the number of elements in the resulting set.
*/
Long sinterstore(K destination, K... keys);
/**
* Determine if a given value is a member of a set.
*
* @param key the key
* @param member the member type: value
* @return Boolean integer-reply specifically:
*
* {@literal true} if the element is a member of the set. {@literal false} if the element is not a member of the
* set, or if {@code key} does not exist.
*/
Boolean sismember(K key, V member);
/**
* Move a member from one set to another.
*
* @param source the source key
* @param destination the destination type: key
* @param member the member type: value
* @return Boolean integer-reply specifically:
*
* {@literal true} if the element is moved. {@literal false} if the element is not a member of {@code source} and no
* operation was performed.
*/
Boolean smove(K source, K destination, V member);
/**
* Get all the members in a set.
*
* @param key the key
* @return Set<V> array-reply all elements of the set.
*/
Set<V> smembers(K key);
/**
* Get all the members in a set.
*
* @param channel the channel
* @param key the keys
* @return Long count of members of the resulting set.
*/
Long smembers(ValueStreamingChannel<V> channel, K key);
/**
* Remove and return a random member from a set.
*
* @param key the key
* @return V bulk-string-reply the removed element, or {@literal null} when {@code key} does not exist.
*/
V spop(K key);
/**
* Remove and return one or multiple random members from a set.
*
* @param key the key
* @param count number of members to pop
* @return V bulk-string-reply the removed element, or {@literal null} when {@code key} does not exist.
*/
Set<V> spop(K key, long count);
/**
* Get one random member from a set.
*
* @param key the key
*
* @return V bulk-string-reply without the additional {@code count} argument the command returns a Bulk Reply with the
* randomly selected element, or {@literal null} when {@code key} does not exist.
*/
V srandmember(K key);
/**
* Get one or multiple random members from a set.
*
* @param key the key
* @param count the count type: long
* @return Set<V> bulk-string-reply without the additional {@code count} argument the command returns a Bulk Reply
* with the randomly selected element, or {@literal null} when {@code key} does not exist.
*/
List<V> srandmember(K key, long count);
/**
* Get one or multiple random members from a set.
*
* @param channel streaming channel that receives a call for every value
* @param key the key
* @param count the count
* @return Long count of members of the resulting set.
*/
Long srandmember(ValueStreamingChannel<V> channel, K key, long count);
/**
* Remove one or more members from a set.
*
* @param key the key
* @param members the member type: value
* @return Long integer-reply the number of members that were removed from the set, not including non existing members.
*/
Long srem(K key, V... members);
/**
* Add multiple sets.
*
* @param keys the key
* @return Set<V> array-reply list with members of the resulting set.
*/
Set<V> sunion(K... keys);
/**
* Add multiple sets.
*
* @param channel streaming channel that receives a call for every value
* @param keys the keys
* @return Long count of members of the resulting set.
*/
Long sunion(ValueStreamingChannel<V> channel, K... keys);
/**
* Add multiple sets and store the resulting set in a key.
*
* @param destination the destination type: key
* @param keys the key
* @return Long integer-reply the number of elements in the resulting set.
*/
Long sunionstore(K destination, K... keys);
/**
* Incrementally iterate Set elements.
*
* @param key the key
* @return ValueScanCursor<V> scan cursor.
*/
ValueScanCursor<V> sscan(K key);
/**
* Incrementally iterate Set elements.
*
* @param key the key
* @param scanArgs scan arguments
* @return ValueScanCursor<V> scan cursor.
*/
ValueScanCursor<V> sscan(K key, ScanArgs scanArgs);
/**
* Incrementally iterate Set elements.
*
* @param key the key
* @param scanCursor cursor to resume from a previous scan, must not be {@literal null}
* @param scanArgs scan arguments
* @return ValueScanCursor<V> scan cursor.
*/
ValueScanCursor<V> sscan(K key, ScanCursor scanCursor, ScanArgs scanArgs);
/**
* Incrementally iterate Set elements.
*
* @param key the key
* @param scanCursor cursor to resume from a previous scan, must not be {@literal null}
* @return ValueScanCursor<V> scan cursor.
*/
ValueScanCursor<V> sscan(K key, ScanCursor scanCursor);
/**
* Incrementally iterate Set elements.
*
* @param channel streaming channel that receives a call for every value
* @param key the key
* @return StreamScanCursor scan cursor.
*/
StreamScanCursor sscan(ValueStreamingChannel<V> channel, K key);
/**
* Incrementally iterate Set elements.
*
* @param channel streaming channel that receives a call for every value
* @param key the key
* @param scanArgs scan arguments
* @return StreamScanCursor scan cursor.
*/
StreamScanCursor sscan(ValueStreamingChannel<V> channel, K key, ScanArgs scanArgs);
/**
* Incrementally iterate Set elements.
*
* @param channel streaming channel that receives a call for every value
* @param key the key
* @param scanCursor cursor to resume from a previous scan, must not be {@literal null}
* @param scanArgs scan arguments
* @return StreamScanCursor scan cursor.
*/
StreamScanCursor sscan(ValueStreamingChannel<V> channel, K key, ScanCursor scanCursor, ScanArgs scanArgs);
/**
* Incrementally iterate Set elements.
*
* @param channel streaming channel that receives a call for every value
* @param key the key
* @param scanCursor cursor to resume from a previous scan, must not be {@literal null}
* @return StreamScanCursor scan cursor.
*/
StreamScanCursor sscan(ValueStreamingChannel<V> channel, K key, ScanCursor scanCursor);
}
- SortedSet
// Set의 member목록 조회 - start가 0이고 stop이 -1이면 전체 조회
List<String> selectSetValueList = redisCommands.zrange(key, start, stop).stram().map(String::new).collect(toList());
// Set의 member 추가/수정 - score에 따라 순서가 정해짐 (오름차순)
// 새로운 member면 추가되고, 존재하는 member면 score가 수정됨.
redisCommands.zadd(key, score, member);
// Set member 삭제 - 삭제된 수를 return함
Long removeCount = redisCommands.zrem(key, member1, ...);
package io.lettuce.core.api.sync;
/**
* Synchronous executed commands for Sorted Sets.
*
* @param <K> Key type.
* @param <V> Value type.
* @author Mark Paluch
* @since 4.0
* @generated by io.lettuce.apigenerator.CreateSyncApi
*/
public interface RedisSortedSetCommands<K, V> {
/**
* Removes and returns a member with the lowest scores in the sorted set stored at one of the keys.
*
* @param timeout the timeout in seconds.
* @param keys the keys.
* @return KeyValue<K, ScoredValue<V>> multi-bulk containing the name of the key, the score and the popped member.
* @since 5.1
*/
KeyValue<K, ScoredValue<V>> bzpopmin(long timeout, K... keys);
/**
* Removes and returns a member with the highest scores in the sorted set stored at one of the keys.
*
* @param timeout the timeout in seconds.
* @param keys the keys.
* @return KeyValue<K, ScoredValue<V>> multi-bulk containing the name of the key, the score and the popped member.
* @since 5.1
*/
KeyValue<K, ScoredValue<V>> bzpopmax(long timeout, K... keys);
/**
* Add one or more members to a sorted set, or update its score if it already exists.
*
* @param key the key
* @param score the score
* @param member the member
*
* @return Long integer-reply specifically:
*
* The number of elements added to the sorted sets, not including elements already existing for which the score was
* updated.
*/
Long zadd(K key, double score, V member);
/**
* Add one or more members to a sorted set, or update its score if it already exists.
*
* @param key the key
* @param scoresAndValues the scoresAndValue tuples (score,value,score,value,...)
* @return Long integer-reply specifically:
*
* The number of elements added to the sorted sets, not including elements already existing for which the score was
* updated.
*/
Long zadd(K key, Object... scoresAndValues);
/**
* Add one or more members to a sorted set, or update its score if it already exists.
*
* @param key the key
* @param scoredValues the scored values
* @return Long integer-reply specifically:
*
* The number of elements added to the sorted sets, not including elements already existing for which the score was
* updated.
*/
Long zadd(K key, ScoredValue<V>... scoredValues);
/**
* Add one or more members to a sorted set, or update its score if it already exists.
*
* @param key the key
* @param zAddArgs arguments for zadd
* @param score the score
* @param member the member
*
* @return Long integer-reply specifically:
*
* The number of elements added to the sorted sets, not including elements already existing for which the score was
* updated.
*/
Long zadd(K key, ZAddArgs zAddArgs, double score, V member);
/**
* Add one or more members to a sorted set, or update its score if it already exists.
*
* @param key the key
* @param zAddArgs arguments for zadd
* @param scoresAndValues the scoresAndValue tuples (score,value,score,value,...)
* @return Long integer-reply specifically:
*
* The number of elements added to the sorted sets, not including elements already existing for which the score was
* updated.
*/
Long zadd(K key, ZAddArgs zAddArgs, Object... scoresAndValues);
/**
* Add one or more members to a sorted set, or update its score if it already exists.
*
* @param key the ke
* @param zAddArgs arguments for zadd
* @param scoredValues the scored values
* @return Long integer-reply specifically:
*
* The number of elements added to the sorted sets, not including elements already existing for which the score was
* updated.
*/
Long zadd(K key, ZAddArgs zAddArgs, ScoredValue<V>... scoredValues);
/**
* Add one or more members to a sorted set, or update its score if it already exists applying the {@code INCR} option. ZADD
* acts like ZINCRBY.
*
* @param key the key
* @param score the score
* @param member the member
* @return Long integer-reply specifically: The total number of elements changed
*/
Double zaddincr(K key, double score, V member);
/**
* Add one or more members to a sorted set, or update its score if it already exists applying the {@code INCR} option. ZADD
* acts like ZINCRBY.
*
* @param key the key
* @param zAddArgs arguments for zadd
* @param score the score
* @param member the member
* @return Long integer-reply specifically: The total number of elements changed
* @since 4.3
*/
Double zaddincr(K key, ZAddArgs zAddArgs, double score, V member);
/**
* Get the number of members in a sorted set.
*
* @param key the key
* @return Long integer-reply the cardinality (number of elements) of the sorted set, or {@literal false} if {@code key}
* does not exist.
*/
Long zcard(K key);
/**
* Count the members in a sorted set with scores within the given {@link Range}.
*
* @param key the key
* @param range the range
* @return Long integer-reply the number of elements in the specified score range.
* @since 4.3
*/
Long zcount(K key, Range<? extends Number> range);
/**
* Increment the score of a member in a sorted set.
*
* @param key the key
* @param amount the increment type: long
* @param member the member type: value
* @return Double bulk-string-reply the new score of {@code member} (a double precision floating point number), represented
* as string.
*/
Double zincrby(K key, double amount, V member);
/**
* Intersect multiple sorted sets and store the resulting sorted set in a new key.
*
* @param destination the destination
* @param keys the keys
* @return Long integer-reply the number of elements in the resulting sorted set at {@code destination}.
*/
Long zinterstore(K destination, K... keys);
/**
* Intersect multiple sorted sets and store the resulting sorted set in a new key.
*
* @param destination the destination
* @param storeArgs the storeArgs
* @param keys the keys
* @return Long integer-reply the number of elements in the resulting sorted set at {@code destination}.
*/
Long zinterstore(K destination, ZStoreArgs storeArgs, K... keys);
/**
* Count the number of members in a sorted set between a given lexicographical range.
*
* @param key the key
* @param range the range
* @return Long integer-reply the number of elements in the specified score range.
* @since 4.3
*/
Long zlexcount(K key, Range<? extends V> range);
/**
* Removes and returns up to count members with the lowest scores in the sorted set stored at key.
*
* @param key the key
* @return ScoredValue<V> the removed element.
* @since 5.1
*/
ScoredValue<V> zpopmin(K key);
/**
* Removes and returns up to count members with the lowest scores in the sorted set stored at key.
*
* @param key the key.
* @param count the number of elements to return.
* @return List<ScoredValue<V>> array-reply list of popped scores and elements.
* @since 5.1
*/
List<ScoredValue<V>> zpopmin(K key, long count);
/**
* Removes and returns up to count members with the highest scores in the sorted set stored at key.
*
* @param key the key
* @return ScoredValue<V> the removed element.
* @since 5.1
*/
ScoredValue<V> zpopmax(K key);
/**
* Removes and returns up to count members with the highest scores in the sorted set stored at key.
*
* @param key the key.
* @param count the number of elements to return.
* @return List<ScoredValue<V>> array-reply list of popped scores and elements.
* @since 5.1
*/
List<ScoredValue<V>> zpopmax(K key, long count);
/**
* Return a range of members in a sorted set, by index.
*
* @param key the key
* @param start the start
* @param stop the stop
* @return List<V> array-reply list of elements in the specified range.
*/
List<V> zrange(K key, long start, long stop);
/**
* Return a range of members in a sorted set, by index.
*
* @param channel streaming channel that receives a call for every value
* @param key the key
* @param start the start
* @param stop the stop
* @return Long count of elements in the specified range.
*/
Long zrange(ValueStreamingChannel<V> channel, K key, long start, long stop);
/**
* Return a range of members with scores in a sorted set, by index.
*
* @param key the key
* @param start the start
* @param stop the stop
* @return List<V> array-reply list of elements in the specified range.
*/
List<ScoredValue<V>> zrangeWithScores(K key, long start, long stop);
/**
* Stream over a range of members with scores in a sorted set, by index.
*
* @param channel streaming channel that receives a call for every value
* @param key the key
* @param start the start
* @param stop the stop
* @return Long count of elements in the specified range.
*/
Long zrangeWithScores(ScoredValueStreamingChannel<V> channel, K key, long start, long stop);
/**
* Return a range of members in a sorted set, by lexicographical range.
*
* @param key the key
* @param range the range
* @return List<V> array-reply list of elements in the specified range.
* @since 4.3
*/
List<V> zrangebylex(K key, Range<? extends V> range);
/**
* Return a range of members in a sorted set, by lexicographical range.
*
* @param key the key
* @param range the range
* @param limit the limit
* @return List<V> array-reply list of elements in the specified range.
* @since 4.3
*/
List<V> zrangebylex(K key, Range<? extends V> range, Limit limit);
/**
* Return a range of members in a sorted set, by score.
*
* @param key the key
* @param range the range
* @return List<V> array-reply list of elements in the specified score range.
* @since 4.3
*/
List<V> zrangebyscore(K key, Range<? extends Number> range);
/**
* Return a range of members in a sorted set, by score.
*
* @param key the key
* @param range the range
* @param limit the limit
* @return List<V> array-reply list of elements in the specified score range.
* @since 4.3
*/
List<V> zrangebyscore(K key, Range<? extends Number> range, Limit limit);
/**
* Stream over a range of members in a sorted set, by score.
*
* @param channel streaming channel that receives a call for every value
* @param key the key
* @param range the range
* @return Long count of elements in the specified score range.
* @since 4.3
*/
Long zrangebyscore(ValueStreamingChannel<V> channel, K key, Range<? extends Number> range);
/**
* Stream over a range of members in a sorted set, by score.
*
* @param channel streaming channel that receives a call for every value
* @param key the key
* @param range the range
* @param limit the limit
* @return Long count of elements in the specified score range.
* @since 4.3
*/
Long zrangebyscore(ValueStreamingChannel<V> channel, K key, Range<? extends Number> range, Limit limit);
/**
* Return a range of members with score in a sorted set, by score.
*
* @param key the key
* @param range the range
* @return List<ScoredValue<V>> array-reply list of elements in the specified score range.
* @since 4.3
*/
List<ScoredValue<V>> zrangebyscoreWithScores(K key, Range<? extends Number> range);
/**
* Return a range of members with score in a sorted set, by score.
*
* @param key the key
* @param range the range
* @param limit the limit
* @return List<ScoredValue<V>> array-reply list of elements in the specified score range.
* @since 4.3
*/
List<ScoredValue<V>> zrangebyscoreWithScores(K key, Range<? extends Number> range, Limit limit);
/**
* Stream over a range of members with scores in a sorted set, by score.
*
* @param channel streaming channel that receives a call for every scored value
* @param key the key
* @param range the range
* @return Long count of elements in the specified score range.
* @since 4.3
*/
Long zrangebyscoreWithScores(ScoredValueStreamingChannel<V> channel, K key, Range<? extends Number> range);
/**
* Stream over a range of members with scores in a sorted set, by score.
*
* @param channel streaming channel that receives a call for every scored value
* @param key the key
* @param range the range
* @param limit the limit
* @return Long count of elements in the specified score range.
* @since 4.3
*/
Long zrangebyscoreWithScores(ScoredValueStreamingChannel<V> channel, K key, Range<? extends Number> range, Limit limit);
/**
* Determine the index of a member in a sorted set.
*
* @param key the key
* @param member the member type: value
* @return Long integer-reply the rank of {@code member}. If {@code member} does not exist in the sorted set or {@code key}
* does not exist,
*/
Long zrank(K key, V member);
/**
* Remove one or more members from a sorted set.
*
* @param key the key
* @param members the member type: value
* @return Long integer-reply specifically:
*
* The number of members removed from the sorted set, not including non existing members.
*/
Long zrem(K key, V... members);
/**
* Remove all members in a sorted set between the given lexicographical range.
*
* @param key the key
* @param range the range
* @return Long integer-reply the number of elements removed.
* @since 4.3
*/
Long zremrangebylex(K key, Range<? extends V> range);
/**
* Remove all members in a sorted set within the given indexes.
*
* @param key the key
* @param start the start type: long
* @param stop the stop type: long
* @return Long integer-reply the number of elements removed.
*/
Long zremrangebyrank(K key, long start, long stop);
/**
* Remove all members in a sorted set within the given scores.
*
* @param key the key
* @param range the range
* @return Long integer-reply the number of elements removed.
* @since 4.3
*/
Long zremrangebyscore(K key, Range<? extends Number> range);
/**
* Return a range of members in a sorted set, by index, with scores ordered from high to low.
*
* @param key the key
* @param start the start
* @param stop the stop
* @return List<V> array-reply list of elements in the specified range.
*/
List<V> zrevrange(K key, long start, long stop);
/**
* Stream over a range of members in a sorted set, by index, with scores ordered from high to low.
*
* @param channel streaming channel that receives a call for every scored value
* @param key the key
* @param start the start
* @param stop the stop
* @return Long count of elements in the specified range.
*/
Long zrevrange(ValueStreamingChannel<V> channel, K key, long start, long stop);
/**
* Return a range of members with scores in a sorted set, by index, with scores ordered from high to low.
*
* @param key the key
* @param start the start
* @param stop the stop
* @return List<V> array-reply list of elements in the specified range.
*/
List<ScoredValue<V>> zrevrangeWithScores(K key, long start, long stop);
/**
* Stream over a range of members with scores in a sorted set, by index, with scores ordered from high to low.
*
* @param channel streaming channel that receives a call for every scored value
* @param key the key
* @param start the start
* @param stop the stop
* @return Long count of elements in the specified range.
*/
Long zrevrangeWithScores(ScoredValueStreamingChannel<V> channel, K key, long start, long stop);
/**
* Return a range of members in a sorted set, by lexicographical range ordered from high to low.
*
* @param key the key
* @param range the range
* @return List<V> array-reply list of elements in the specified score range.
* @since 4.3
*/
List<V> zrevrangebylex(K key, Range<? extends V> range);
/**
* Return a range of members in a sorted set, by lexicographical range ordered from high to low.
*
* @param key the key
* @param range the range
* @param limit the limit
* @return List<V> array-reply list of elements in the specified score range.
* @since 4.3
*/
List<V> zrevrangebylex(K key, Range<? extends V> range, Limit limit);
/**
* Return a range of members in a sorted set, by score, with scores ordered from high to low.
*
* @param key the key
* @param range the range
* @return List<V> array-reply list of elements in the specified score range.
* @since 4.3
*/
List<V> zrevrangebyscore(K key, Range<? extends Number> range);
/**
* Return a range of members in a sorted set, by score, with scores ordered from high to low.
*
* @param key the key
* @param range the range
* @param limit the limit
* @return List<V> array-reply list of elements in the specified score range.
* @since 4.3
*/
List<V> zrevrangebyscore(K key, Range<? extends Number> range, Limit limit);
/**
* Stream over a range of members in a sorted set, by score, with scores ordered from high to low.
*
* @param channel streaming channel that receives a call for every value
* @param key the key
* @param range the range
* @param limit the limit
* @return Long count of elements in the specified range.
* @since 4.3
*/
Long zrevrangebyscore(ValueStreamingChannel<V> channel, K key, Range<? extends Number> range, Limit limit);
/**
* Return a range of members with scores in a sorted set, by score, with scores ordered from high to low.
*
* @param key the key
* @param range the range
* @return List<ScoredValue<V>> array-reply list of elements in the specified score range.
* @since 4.3
*/
List<ScoredValue<V>> zrevrangebyscoreWithScores(K key, Range<? extends Number> range);
/**
* Return a range of members with scores in a sorted set, by score, with scores ordered from high to low.
*
* @param key the key
* @param range the range
* @param limit limit
* @return List<V> array-reply list of elements in the specified score range.
* @since 4.3
*/
List<ScoredValue<V>> zrevrangebyscoreWithScores(K key, Range<? extends Number> range, Limit limit);
/**
* Stream over a range of members with scores in a sorted set, by score, with scores ordered from high to low.
*
* @param channel streaming channel that receives a call for every scored value
* @param key the key
* @param range the range
* @return Long count of elements in the specified range.
*/
Long zrevrangebyscoreWithScores(ScoredValueStreamingChannel<V> channel, K key, Range<? extends Number> range);
/**
* Stream over a range of members with scores in a sorted set, by score, with scores ordered from high to low.
*
* @param channel streaming channel that receives a call for every scored value
* @param key the key
* @param range the range
* @param limit the limit
* @return Long count of elements in the specified range.
* @since 4.3
*/
Long zrevrangebyscoreWithScores(ScoredValueStreamingChannel<V> channel, K key, Range<? extends Number> range, Limit limit);
/**
* Determine the index of a member in a sorted set, with scores ordered from high to low.
*
* @param key the key
* @param member the member type: value
* @return Long integer-reply the rank of {@code member}. If {@code member} does not exist in the sorted set or {@code key}
* does not exist,
*/
Long zrevrank(K key, V member);
/**
* Incrementally iterate sorted sets elements and associated scores.
*
* @param key the key
* @return ScoredValueScanCursor<V> scan cursor.
*/
ScoredValueScanCursor<V> zscan(K key);
/**
* Incrementally iterate sorted sets elements and associated scores.
*
* @param key the key
* @param scanArgs scan arguments
* @return ScoredValueScanCursor<V> scan cursor.
*/
ScoredValueScanCursor<V> zscan(K key, ScanArgs scanArgs);
/**
* Incrementally iterate sorted sets elements and associated scores.
*
* @param key the key
* @param scanCursor cursor to resume from a previous scan, must not be {@literal null}
* @param scanArgs scan arguments
* @return ScoredValueScanCursor<V> scan cursor.
*/
ScoredValueScanCursor<V> zscan(K key, ScanCursor scanCursor, ScanArgs scanArgs);
/**
* Incrementally iterate sorted sets elements and associated scores.
*
* @param key the key
* @param scanCursor cursor to resume from a previous scan, must not be {@literal null}
* @return ScoredValueScanCursor<V> scan cursor.
*/
ScoredValueScanCursor<V> zscan(K key, ScanCursor scanCursor);
/**
* Incrementally iterate sorted sets elements and associated scores.
*
* @param channel streaming channel that receives a call for every scored value
* @param key the key
* @return StreamScanCursor scan cursor.
*/
StreamScanCursor zscan(ScoredValueStreamingChannel<V> channel, K key);
/**
* Incrementally iterate sorted sets elements and associated scores.
*
* @param channel streaming channel that receives a call for every scored value
* @param key the key
* @param scanArgs scan arguments
* @return StreamScanCursor scan cursor.
*/
StreamScanCursor zscan(ScoredValueStreamingChannel<V> channel, K key, ScanArgs scanArgs);
/**
* Incrementally iterate sorted sets elements and associated scores.
*
* @param channel streaming channel that receives a call for every scored value
* @param key the key
* @param scanCursor cursor to resume from a previous scan, must not be {@literal null}
* @param scanArgs scan arguments
* @return StreamScanCursor scan cursor.
*/
StreamScanCursor zscan(ScoredValueStreamingChannel<V> channel, K key, ScanCursor scanCursor, ScanArgs scanArgs);
/**
* Incrementally iterate sorted sets elements and associated scores.
*
* @param channel streaming channel that receives a call for every scored value
* @param key the key
* @param scanCursor cursor to resume from a previous scan, must not be {@literal null}
* @return StreamScanCursor scan cursor.
*/
StreamScanCursor zscan(ScoredValueStreamingChannel<V> channel, K key, ScanCursor scanCursor);
/**
* Get the score associated with the given member in a sorted set.
*
* @param key the key
* @param member the member type: value
* @return Double bulk-string-reply the score of {@code member} (a double precision floating point number), represented as
* string.
*/
Double zscore(K key, V member);
/**
* Add multiple sorted sets and store the resulting sorted set in a new key.
*
* @param destination destination key
* @param keys source keys
* @return Long integer-reply the number of elements in the resulting sorted set at {@code destination}.
*/
Long zunionstore(K destination, K... keys);
/**
* Add multiple sorted sets and store the resulting sorted set in a new key.
*
* @param destination the destination
* @param storeArgs the storeArgs
* @param keys the keys
* @return Long integer-reply the number of elements in the resulting sorted set at {@code destination}.
*/
Long zunionstore(K destination, ZStoreArgs storeArgs, K... keys);
}
- Hash
// hash field에 String value 설정
redisCommands.hset(key, field, value);
// hash의 모든 field와 value 조회
Map<String, String> record = redisCommands.hgetall(key);
package io.lettuce.core.api.sync;
/**
* Synchronous executed commands for Hashes (Key-Value pairs).
*
* @param <K> Key type.
* @param <V> Value type.
* @author Mark Paluch
* @since 4.0
* @generated by io.lettuce.apigenerator.CreateSyncApi
*/
public interface RedisHashCommands<K, V> {
/**
* Delete one or more hash fields.
*
* @param key the key
* @param fields the field type: key
* @return Long integer-reply the number of fields that were removed from the hash, not including specified but non existing
* fields.
*/
Long hdel(K key, K... fields);
/**
* Determine if a hash field exists.
*
* @param key the key
* @param field the field type: key
* @return Boolean integer-reply specifically:
*
* {@literal true} if the hash contains {@code field}. {@literal false} if the hash does not contain {@code field},
* or {@code key} does not exist.
*/
Boolean hexists(K key, K field);
/**
* Get the value of a hash field.
*
* @param key the key
* @param field the field type: key
* @return V bulk-string-reply the value associated with {@code field}, or {@literal null} when {@code field} is not present
* in the hash or {@code key} does not exist.
*/
V hget(K key, K field);
/**
* Increment the integer value of a hash field by the given number.
*
* @param key the key
* @param field the field type: key
* @param amount the increment type: long
* @return Long integer-reply the value at {@code field} after the increment operation.
*/
Long hincrby(K key, K field, long amount);
/**
* Increment the float value of a hash field by the given amount.
*
* @param key the key
* @param field the field type: key
* @param amount the increment type: double
* @return Double bulk-string-reply the value of {@code field} after the increment.
*/
Double hincrbyfloat(K key, K field, double amount);
/**
* Get all the fields and values in a hash.
*
* @param key the key
* @return Map<K,V> array-reply list of fields and their values stored in the hash, or an empty list when {@code key}
* does not exist.
*/
Map<K, V> hgetall(K key);
/**
* Stream over all the fields and values in a hash.
*
* @param channel the channel
* @param key the key
*
* @return Long count of the keys.
*/
Long hgetall(KeyValueStreamingChannel<K, V> channel, K key);
/**
* Get all the fields in a hash.
*
* @param key the key
* @return List<K> array-reply list of fields in the hash, or an empty list when {@code key} does not exist.
*/
List<K> hkeys(K key);
/**
* Stream over all the fields in a hash.
*
* @param channel the channel
* @param key the key
*
* @return Long count of the keys.
*/
Long hkeys(KeyStreamingChannel<K> channel, K key);
/**
* Get the number of fields in a hash.
*
* @param key the key
* @return Long integer-reply number of fields in the hash, or {@code 0} when {@code key} does not exist.
*/
Long hlen(K key);
/**
* Get the values of all the given hash fields.
*
* @param key the key
* @param fields the field type: key
* @return List<V> array-reply list of values associated with the given fields, in the same
*/
List<KeyValue<K, V>> hmget(K key, K... fields);
/**
* Stream over the values of all the given hash fields.
*
* @param channel the channel
* @param key the key
* @param fields the fields
*
* @return Long count of the keys
*/
Long hmget(KeyValueStreamingChannel<K, V> channel, K key, K... fields);
/**
* Set multiple hash fields to multiple values.
*
* @param key the key
* @param map the null
* @return String simple-string-reply
*/
String hmset(K key, Map<K, V> map);
/**
* Incrementally iterate hash fields and associated values.
*
* @param key the key
* @return MapScanCursor<K, V> map scan cursor.
*/
MapScanCursor<K, V> hscan(K key);
/**
* Incrementally iterate hash fields and associated values.
*
* @param key the key
* @param scanArgs scan arguments
* @return MapScanCursor<K, V> map scan cursor.
*/
MapScanCursor<K, V> hscan(K key, ScanArgs scanArgs);
/**
* Incrementally iterate hash fields and associated values.
*
* @param key the key
* @param scanCursor cursor to resume from a previous scan, must not be {@literal null}
* @param scanArgs scan arguments
* @return MapScanCursor<K, V> map scan cursor.
*/
MapScanCursor<K, V> hscan(K key, ScanCursor scanCursor, ScanArgs scanArgs);
/**
* Incrementally iterate hash fields and associated values.
*
* @param key the key
* @param scanCursor cursor to resume from a previous scan, must not be {@literal null}
* @return MapScanCursor<K, V> map scan cursor.
*/
MapScanCursor<K, V> hscan(K key, ScanCursor scanCursor);
/**
* Incrementally iterate hash fields and associated values.
*
* @param channel streaming channel that receives a call for every key-value pair
* @param key the key
* @return StreamScanCursor scan cursor.
*/
StreamScanCursor hscan(KeyValueStreamingChannel<K, V> channel, K key);
/**
* Incrementally iterate hash fields and associated values.
*
* @param channel streaming channel that receives a call for every key-value pair
* @param key the key
* @param scanArgs scan arguments
* @return StreamScanCursor scan cursor.
*/
StreamScanCursor hscan(KeyValueStreamingChannel<K, V> channel, K key, ScanArgs scanArgs);
/**
* Incrementally iterate hash fields and associated values.
*
* @param channel streaming channel that receives a call for every key-value pair
* @param key the key
* @param scanCursor cursor to resume from a previous scan, must not be {@literal null}
* @param scanArgs scan arguments
* @return StreamScanCursor scan cursor.
*/
StreamScanCursor hscan(KeyValueStreamingChannel<K, V> channel, K key, ScanCursor scanCursor, ScanArgs scanArgs);
/**
* Incrementally iterate hash fields and associated values.
*
* @param channel streaming channel that receives a call for every key-value pair
* @param key the key
* @param scanCursor cursor to resume from a previous scan, must not be {@literal null}
* @return StreamScanCursor scan cursor.
*/
StreamScanCursor hscan(KeyValueStreamingChannel<K, V> channel, K key, ScanCursor scanCursor);
/**
* Set the string value of a hash field.
*
* @param key the key
* @param field the field type: key
* @param value the value
* @return Boolean integer-reply specifically:
*
* {@literal true} if {@code field} is a new field in the hash and {@code value} was set. {@literal false} if
* {@code field} already exists in the hash and the value was updated.
*/
Boolean hset(K key, K field, V value);
/**
* Set the value of a hash field, only if the field does not exist.
*
* @param key the key
* @param field the field type: key
* @param value the value
* @return Boolean integer-reply specifically:
*
* {@code 1} if {@code field} is a new field in the hash and {@code value} was set. {@code 0} if {@code field}
* already exists in the hash and no operation was performed.
*/
Boolean hsetnx(K key, K field, V value);
/**
* Get the string length of the field value in a hash.
*
* @param key the key
* @param field the field type: key
* @return Long integer-reply the string length of the {@code field} value, or {@code 0} when {@code field} is not present
* in the hash or {@code key} does not exist at all.
*/
Long hstrlen(K key, K field);
/**
* Get all the values in a hash.
*
* @param key the key
* @return List<V> array-reply list of values in the hash, or an empty list when {@code key} does not exist.
*/
List<V> hvals(K key);
/**
* Stream over all the values in a hash.
*
* @param channel streaming channel that receives a call for every value
* @param key the key
*
* @return Long count of the keys.
*/
Long hvals(ValueStreamingChannel<V> channel, K key);
}
- Key
// key의 expire 시간(초) 설정
Boolean expire = redisCommands.expire(key, expiredTime);
// true : 설정 완료
// false : 존재하지 않거나 설정 실패
// key 존재 유무
Long isExist = redisCommands.exists(key);
// 0 : 존재하지 않는 key
package io.lettuce.core.api.sync;
/**
* Synchronous executed commands for Keys (Key manipulation/querying).
*
* @param <K> Key type.
* @param <V> Value type.
* @author Mark Paluch
* @since 4.0
* @generated by io.lettuce.apigenerator.CreateSyncApi
*/
public interface RedisKeyCommands<K, V> {
/**
* Delete one or more keys.
*
* @param keys the keys
* @return Long integer-reply The number of keys that were removed.
*/
Long del(K... keys);
/**
* Unlink one or more keys (non blocking DEL).
*
* @param keys the keys
* @return Long integer-reply The number of keys that were removed.
*/
Long unlink(K... keys);
/**
* Return a serialized version of the value stored at the specified key.
*
* @param key the key
* @return byte[] bulk-string-reply the serialized value.
*/
byte[] dump(K key);
/**
* Determine how many keys exist.
*
* @param keys the keys
* @return Long integer-reply specifically: Number of existing keys
*/
Long exists(K... keys);
/**
* Set a key's time to live in seconds.
*
* @param key the key
* @param seconds the seconds type: long
* @return Boolean integer-reply specifically:
*
* {@literal true} if the timeout was set. {@literal false} if {@code key} does not exist or the timeout could not
* be set.
*/
Boolean expire(K key, long seconds);
/**
* Set the expiration for a key as a UNIX timestamp.
*
* @param key the key
* @param timestamp the timestamp type: posix time
* @return Boolean integer-reply specifically:
*
* {@literal true} if the timeout was set. {@literal false} if {@code key} does not exist or the timeout could not
* be set (see: {@code EXPIRE}).
*/
Boolean expireat(K key, Date timestamp);
/**
* Set the expiration for a key as a UNIX timestamp.
*
* @param key the key
* @param timestamp the timestamp type: posix time
* @return Boolean integer-reply specifically:
*
* {@literal true} if the timeout was set. {@literal false} if {@code key} does not exist or the timeout could not
* be set (see: {@code EXPIRE}).
*/
Boolean expireat(K key, long timestamp);
/**
* Find all keys matching the given pattern.
*
* @param pattern the pattern type: patternkey (pattern)
* @return List<K> array-reply list of keys matching {@code pattern}.
*/
List<K> keys(K pattern);
/**
* Find all keys matching the given pattern.
*
* @param channel the channel
* @param pattern the pattern
* @return Long array-reply list of keys matching {@code pattern}.
*/
Long keys(KeyStreamingChannel<K> channel, K pattern);
/**
* Atomically transfer a key from a Redis instance to another one.
*
* @param host the host
* @param port the port
* @param key the key
* @param db the database
* @param timeout the timeout in milliseconds
* @return String simple-string-reply The command returns OK on success.
*/
String migrate(String host, int port, K key, int db, long timeout);
/**
* Atomically transfer one or more keys from a Redis instance to another one.
*
* @param host the host
* @param port the port
* @param db the database
* @param timeout the timeout in milliseconds
* @param migrateArgs migrate args that allow to configure further options
* @return String simple-string-reply The command returns OK on success.
*/
String migrate(String host, int port, int db, long timeout, MigrateArgs<K> migrateArgs);
/**
* Move a key to another database.
*
* @param key the key
* @param db the db type: long
* @return Boolean integer-reply specifically:
*/
Boolean move(K key, int db);
/**
* returns the kind of internal representation used in order to store the value associated with a key.
*
* @param key the key
* @return String
*/
String objectEncoding(K key);
/**
* returns the number of seconds since the object stored at the specified key is idle (not requested by read or write
* operations).
*
* @param key the key
* @return number of seconds since the object stored at the specified key is idle.
*/
Long objectIdletime(K key);
/**
* returns the number of references of the value associated with the specified key.
*
* @param key the key
* @return Long
*/
Long objectRefcount(K key);
/**
* Remove the expiration from a key.
*
* @param key the key
* @return Boolean integer-reply specifically:
*
* {@literal true} if the timeout was removed. {@literal false} if {@code key} does not exist or does not have an
* associated timeout.
*/
Boolean persist(K key);
/**
* Set a key's time to live in milliseconds.
*
* @param key the key
* @param milliseconds the milliseconds type: long
* @return integer-reply, specifically:
*
* {@literal true} if the timeout was set. {@literal false} if {@code key} does not exist or the timeout could not
* be set.
*/
Boolean pexpire(K key, long milliseconds);
/**
* Set the expiration for a key as a UNIX timestamp specified in milliseconds.
*
* @param key the key
* @param timestamp the milliseconds-timestamp type: posix time
* @return Boolean integer-reply specifically:
*
* {@literal true} if the timeout was set. {@literal false} if {@code key} does not exist or the timeout could not
* be set (see: {@code EXPIRE}).
*/
Boolean pexpireat(K key, Date timestamp);
/**
* Set the expiration for a key as a UNIX timestamp specified in milliseconds.
*
* @param key the key
* @param timestamp the milliseconds-timestamp type: posix time
* @return Boolean integer-reply specifically:
*
* {@literal true} if the timeout was set. {@literal false} if {@code key} does not exist or the timeout could not
* be set (see: {@code EXPIRE}).
*/
Boolean pexpireat(K key, long timestamp);
/**
* Get the time to live for a key in milliseconds.
*
* @param key the key
* @return Long integer-reply TTL in milliseconds, or a negative value in order to signal an error (see the description
* above).
*/
Long pttl(K key);
/**
* Return a random key from the keyspace.
*
* @return V bulk-string-reply the random key, or {@literal null} when the database is empty.
*/
V randomkey();
/**
* Rename a key.
*
* @param key the key
* @param newKey the newkey type: key
* @return String simple-string-reply
*/
String rename(K key, K newKey);
/**
* Rename a key, only if the new key does not exist.
*
* @param key the key
* @param newKey the newkey type: key
* @return Boolean integer-reply specifically:
*
* {@literal true} if {@code key} was renamed to {@code newkey}. {@literal false} if {@code newkey} already exists.
*/
Boolean renamenx(K key, K newKey);
/**
* Create a key using the provided serialized value, previously obtained using DUMP.
*
* @param key the key
* @param ttl the ttl type: long
* @param value the serialized-value type: string
* @return String simple-string-reply The command returns OK on success.
*/
String restore(K key, long ttl, byte[] value);
/**
* Create a key using the provided serialized value, previously obtained using DUMP.
*
* @param key the key
* @param value the serialized-value type: string
* @param args the {@link RestoreArgs}, must not be {@literal null}.
* @return String simple-string-reply The command returns OK on success.
* @since 5.1
*/
String restore(K key, byte[] value, RestoreArgs args);
/**
* Sort the elements in a list, set or sorted set.
*
* @param key the key
* @return List<V> array-reply list of sorted elements.
*/
List<V> sort(K key);
/**
* Sort the elements in a list, set or sorted set.
*
* @param channel streaming channel that receives a call for every value
* @param key the key
* @return Long number of values.
*/
Long sort(ValueStreamingChannel<V> channel, K key);
/**
* Sort the elements in a list, set or sorted set.
*
* @param key the key
* @param sortArgs sort arguments
* @return List<V> array-reply list of sorted elements.
*/
List<V> sort(K key, SortArgs sortArgs);
/**
* Sort the elements in a list, set or sorted set.
*
* @param channel streaming channel that receives a call for every value
* @param key the key
* @param sortArgs sort arguments
* @return Long number of values.
*/
Long sort(ValueStreamingChannel<V> channel, K key, SortArgs sortArgs);
/**
* Sort the elements in a list, set or sorted set.
*
* @param key the key
* @param sortArgs sort arguments
* @param destination the destination key to store sort results
* @return Long number of values.
*/
Long sortStore(K key, SortArgs sortArgs, K destination);
/**
* Touch one or more keys. Touch sets the last accessed time for a key. Non-exsitent keys wont get created.
*
* @param keys the keys
* @return Long integer-reply the number of found keys.
*/
Long touch(K... keys);
/**
* Get the time to live for a key.
*
* @param key the key
* @return Long integer-reply TTL in seconds, or a negative value in order to signal an error (see the description above).
*/
Long ttl(K key);
/**
* Determine the type stored at key.
*
* @param key the key
* @return String simple-string-reply type of {@code key}, or {@code none} when {@code key} does not exist.
*/
String type(K key);
/**
* Incrementally iterate the keys space.
*
* @return KeyScanCursor<K> scan cursor.
*/
KeyScanCursor<K> scan();
/**
* Incrementally iterate the keys space.
*
* @param scanArgs scan arguments
* @return KeyScanCursor<K> scan cursor.
*/
KeyScanCursor<K> scan(ScanArgs scanArgs);
/**
* Incrementally iterate the keys space.
*
* @param scanCursor cursor to resume from a previous scan, must not be {@literal null}
* @param scanArgs scan arguments
* @return KeyScanCursor<K> scan cursor.
*/
KeyScanCursor<K> scan(ScanCursor scanCursor, ScanArgs scanArgs);
/**
* Incrementally iterate the keys space.
*
* @param scanCursor cursor to resume from a previous scan, must not be {@literal null}
* @return KeyScanCursor<K> scan cursor.
*/
KeyScanCursor<K> scan(ScanCursor scanCursor);
/**
* Incrementally iterate the keys space.
*
* @param channel streaming channel that receives a call for every key
* @return StreamScanCursor scan cursor.
*/
StreamScanCursor scan(KeyStreamingChannel<K> channel);
/**
* Incrementally iterate the keys space.
*
* @param channel streaming channel that receives a call for every key
* @param scanArgs scan arguments
* @return StreamScanCursor scan cursor.
*/
StreamScanCursor scan(KeyStreamingChannel<K> channel, ScanArgs scanArgs);
/**
* Incrementally iterate the keys space.
*
* @param channel streaming channel that receives a call for every key
* @param scanCursor cursor to resume from a previous scan, must not be {@literal null}
* @param scanArgs scan arguments
* @return StreamScanCursor scan cursor.
*/
StreamScanCursor scan(KeyStreamingChannel<K> channel, ScanCursor scanCursor, ScanArgs scanArgs);
/**
* Incrementally iterate the keys space.
*
* @param channel streaming channel that receives a call for every key
* @param scanCursor cursor to resume from a previous scan, must not be {@literal null}
* @return StreamScanCursor scan cursor.
*/
StreamScanCursor scan(KeyStreamingChannel<K> channel, ScanCursor scanCursor);
}
RedisConfiguration
@Configuration
public class RedisConfiguration {
private final RedisClient redisClient;
// sping.redis.*는 application.yml에 설정
public RedisConfiguration(@Value("${spring.redis.host}") String redisHost, @Value("${spring.redis.port}") Integer redisPort, @Value("${spring.redis.database}") Integer redisDbNo) {
this.redisClient = RedisClient.create(RedisURI.builder().withHost(redisHost).withPort(redisPort).withDatabase(redisDbNo).build());
}
/**
* elastic cache에서 사용하지 않게 설정
*
* @return ConfigureRedisAction
*/
@Bean
public static ConfigureRedisAction configureRedisAction() {
return ConfigureRedisAction.NO_OP;
}
/**
* redis template 설정
*
* @param lettuceConnectionFactory LettuceConnectionFactory
* @return RedisTemplate
*/
@Bean(name = "commonRedisTemplate")
public RedisTemplate<String, Object> commonRedisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
return redisTemplate;
}
@Bean
public RedisCommands<byte[], byte[]> redisCommands() {
return redisClient.connect(redisCodec()).sync();
}
private RedisCodec<byte[], byte[]> redisCodec() {
return new ByteArrayCodec();
}
}
import io.lettuce.core.api.sync.RedisCommands;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
@SpringBootTest
class RedisConfigurationTest {
@Autowired
RedisTemplate<String, Object> redisTemplate;
@Autowired
RedisCommands<byte[], byte[]> redisCommands;
@Test
void commonRedisTemplate() {
Assertions.assertNotNull(redisTemplate);
}
@Test
void commonRedisTemplateAppendTest() {
// GIVE
String key = "redis:test";
String value = "value";
ValueOperations<String, Object> operations = redisTemplate.opsForValue();
// WHEN
operations.set(key, value);
// THEN
Object o = operations.get(key);
Assertions.assertEquals(value, o);
// CLEAR
redisTemplate.delete(key);
}
@Test
void redisCommands() {
Assertions.assertNotNull(redisCommands);
}
@Test
void redisCommandsAppendTest() {
// GIVE
String key = "redis:test";
String value = "value";
// WHEN
redisCommands.set(key.getBytes(), value.getBytes());
// THEN
byte[] bytesValue = redisCommands.get(key.getBytes());
Assertions.assertEquals(value, new String(bytesValue));
// CLEAR
redisCommands.expire(key.getBytes(), 1);
}
}
[Reference]
https://www.baeldung.com/java-redis-lettuce
lettuce.io/core/release/api/io/lettuce/core/api/sync/RedisSetCommands.html
'redis' 카테고리의 다른 글
[Spring] redis keys 대신 scan (0) | 2022.06.29 |
---|---|
[SpringBoot] Redis Key Expired Event Notification (0) | 2021.04.01 |
댓글