org.idevlab.rjc.sharding
Class ShardedRedis

java.lang.Object
  extended by org.idevlab.rjc.sharding.ShardedRedis
All Implemented Interfaces:
RedisOperations

public class ShardedRedis
extends Object
implements RedisOperations


Constructor Summary
ShardedRedis()
           
ShardedRedis(NodeLocator<? extends RedisOperations> locator)
           
 
Method Summary
 Long append(String key, String value)
          Time complexity O(1).
 Long decr(String key)
          Time complexity O(1)

Decrements the number stored at key by one.

 Long decrBy(String key, int value)
          Time complexity O(1)

Decrements the number stored at key by decrement.

 Long del(String... keys)
          Time complexity O(N) where N is the number of keys that will be removed.
 Boolean exists(String key)
          Time complexity O(1)

Determine if a key exists

 Boolean expire(String key, int seconds)
          Time complexity O(1)

Set a timeout on key.

 Boolean expireAt(String key, long unixTime)
          Time complexity O(1)

Set a timeout on key.

 String get(String key)
          Time complexity O(1)

Get the value of key.

 Long getBit(String key, int offset)
          Time complexity O(1)

Returns the bit value at offset in the string value stored at key.

 String getRange(String key, int start, int end)
          Time complexity O(N) where N is the length of the returned string.
 String getSet(String key, String value)
          Time complexity O(1)

Atomically sets key to value and returns the old value stored at key.

 Boolean hdel(String key, String field)
          Time complexity O(1)

Removes field from the hash stored at key.

 Boolean hexists(String key, String field)
          Time complexity O(1) Returns if field is an existing field in the hash stored at key.
 String hget(String key, String field)
          Time complexity O(1)

Returns the value associated with field in the hash stored at key.

 Map<String,String> hgetAll(String key)
          Time complexity O(N) where N is the size of the hash.
 Long hincrBy(String key, String field, int value)
          Time complexity O(1)

Increments the number stored at field in the hash stored at key by increment.

 Set<String> hkeys(String key)
          Time complexity

O(N) where N is the size of the hash.

 Long hlen(String key)
          Time complexity

O(1)

Returns the number of fields contained in the hash stored at key.

 List<String> hmget(String key, String... fields)
          Time complexity

O(N) where N is the number of fields being requested.

 String hmset(String key, Map<String,String> hash)
          Time complexity

O(N) where N is the number of fields being set.

 Boolean hset(String key, String field, String value)
          Time complexity

O(1)

Sets field in the hash stored at key to value.

 Boolean hsetnx(String key, String field, String value)
          Time complexity

O(1)

Sets field in the hash stored at key to value, only if field does not yet exist.

 List<String> hvals(String key)
          Time complexity

O(N) where N is the size of the hash.

 Long incr(String key)
          Time complexity O(1) Increments the number stored at key by one.
 Long incrBy(String key, int value)
          Time complexity O(1) Increments the number stored at key by increment.
 Set<String> keys(String pattern)
          Time complexity O(N) with N being the number of keys in the database, under the assumption that the key names in the database and the given pattern have limited length.
 String lindex(String key, int index)
          Time complexity

O(N) where N is the number of elements to traverse to get to the element at index.

 Long linsert(String key, Client.LIST_POSITION where, String pivot, String value)
          Time complexity

O(N) where N is the number of elements to traverse before seeing the value pivot.

 Long llen(String key)
          Time complexity

O(1)

Returns the length of the list stored at key.

 String lpop(String key)
          Time complexity

O(1)

Removes and returns the first element of the list stored at key.

 Long lpush(String key, String value)
          Time complexity

O(1)

Inserts value at the head of the list stored at key.

 Long lpushx(String key, String value)
          Time complexity

O(1)

Inserts value at the head of the list stored at key, only if key already exists and holds a list.

 List<String> lrange(String key, int start, int end)
          Time complexity

O(S+N) where S is the start offset and N is the number of elements in the specified range.

 Long lrem(String key, int count, String value)
          Time complexity

O(N) where N is the length of the list.

 String lset(String key, int index, String value)
          Time complexity

O(N) where N is the length of the list.

 String ltrim(String key, int start, int end)
          Time complexity

O(N) where N is the number of elements to be removed by the operation.

 Boolean persist(String key)
          Time complexity O(1)

Remove the existing timeout on key.

 Long publish(String channel, String message)
           
 String rpop(String key)
          Time complexity

O(1)

Removes and returns the last element of the list stored at key.

 Long rpush(String key, String value)
          Time complexity

O(1)

Inserts value at the tail of the list stored at key.

 Long rpushx(String key, String value)
          Time complexity

O(1)

Inserts value at the tail of the list stored at key, only if key already exists and holds a list.

 Boolean sadd(String key, String member)
          Time complexity

O(1)

Add member to the set stored at key.

 Long scard(String key)
          Time complexity

O(1)

Returns the set cardinality (number of elements) of the set stored at key.

 String set(String key, String value)
          Time complexity O(1)

Set key to hold the string value.

 Long setBit(String key, int offset, String value)
          Time complexity O(1)

Sets or clears the bit at offset in the string value stored at key.

 String setex(String key, int seconds, String value)
          Time complexity O(1) Set key to hold the string value and set key to timeout after a given number of seconds.
 void setLocator(NodeLocator<? extends RedisOperations> locator)
           
 Boolean setnx(String key, String value)
          Time complexity O(1)

Set key to hold string value if key does not exist.

 Long setRange(String key, int offset, String value)
          Time complexity O(1), not counting the time taken to copy the new string in place.
 Boolean sismember(String key, String member)
          Time complexity

O(1)

Returns if member is a member of the set stored at key.

 Set<String> smembers(String key)
          Time complexity

O(N) where N is the set cardinality.

 List<String> sort(String key)
          Time complexity O(N*log(N)) where N is the number of elements returned.
 List<String> sort(String key, SortingParams sortingParameters)
          Time complexity O(N*log(N)) where N is the number of elements returned.
 String spop(String key)
          Time complexity

O(1)

Removes and returns a random element from the set value stored at key.

 String srandmember(String key)
          Time complexity

O(1)

Returns a random element from the set value stored at key.

 Boolean srem(String key, String member)
          Time complexity

O(1)

Remove member from the set stored at key.

 Long strlen(String key)
          Time complexity O(1)

Returns the length of the string value stored at key.

 Long ttl(String key)
          Time complexity O(1)

Returns the remaining time to live of a key that has a timeout.

 String type(String key)
          Time complexity O(1)

Returns the string representation of the type of the value stored at key.

 Boolean zadd(String key, Number score, String member)
          Time complexity

O(log(N)) where N is the number of elements in the sorted set.

 Long zcard(String key)
          Time complexity

O(1)

Returns the sorted set cardinality (number of elements) of the sorted set stored at key.

 Long zcount(String key, Number min, Number max)
          Time complexity

O(log(N)+M) with N being the number of elements in the sorted set and M being the number of elements between min and max.

 String zincrby(String key, Number score, String member)
          Time complexity

O(log(N)) where N is the number of elements in the sorted set.

 List<String> zrange(String key, int start, int end)
          Time complexity

O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements returned.

 List<String> zrangeByScore(String key, String min, String max)
          Time complexity

O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned.

 List<String> zrangeByScore(String key, String min, String max, int offset, int count)
          Time complexity

O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned.

 List<ElementScore> zrangeByScoreWithScores(String key, String min, String max)
          Time complexity

O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned.

 List<ElementScore> zrangeByScoreWithScores(String key, String min, String max, int offset, int count)
          Time complexity

O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned.

 List<ElementScore> zrangeWithScores(String key, int start, int end)
          Time complexity

O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements returned.

 Long zrank(String key, String member)
          Time complexity

O(log(N))

Returns the rank of member in the sorted set stored at key, with the scores ordered from low to high.

 Boolean zrem(String key, String member)
          Time complexity

O(log(N)) with N being the number of elements in the sorted set.

 Long zremrangeByRank(String key, int start, int end)
          Time complexity

O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements removed by the operation.

 Long zremrangeByScore(String key, String min, String max)
          Time complexity

O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements removed by the operation.

 List<String> zrevrange(String key, int start, int end)
          Time complexity

O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements returned.

 List<String> zrevrangeByScore(String key, String max, String min)
          Time complexity

O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned.

 List<String> zrevrangeByScore(String key, String max, String min, int offset, int count)
          Time complexity

O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned.

 List<ElementScore> zrevrangeByScoreWithScores(String key, String max, String min)
          Time complexity

O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned.

 List<ElementScore> zrevrangeByScoreWithScores(String key, String max, String min, int offset, int count)
          Time complexity

O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned.

 List<ElementScore> zrevrangeWithScores(String key, int start, int end)
          Time complexity

O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements returned.

 Long zrevrank(String key, String member)
          Time complexity

O(log(N))

Returns the rank of member in the sorted set stored at key, with the scores ordered from high to low.

 String zscore(String key, String member)
          Time complexity

O(1)

Returns the score of member in the sorted set at key.

 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ShardedRedis

public ShardedRedis()

ShardedRedis

public ShardedRedis(NodeLocator<? extends RedisOperations> locator)
Method Detail

setLocator

public void setLocator(NodeLocator<? extends RedisOperations> locator)

set

public String set(String key,
                  String value)
Description copied from interface: RedisOperations

Time complexity

O(1)

Set key to hold the string value. If key already holds a value, it is overwritten, regardless of its type.

Specified by:
set in interface RedisOperations
Parameters:
key - the key
value - the value
Returns:
always OK since SET can't fail

get

public String get(String key)
Description copied from interface: RedisOperations

Time complexity

O(1)

Get the value of key. If the key does not exist the special value nil is returned. An error is returned if the value stored at key is not a string, because GET only handles string values.

Specified by:
get in interface RedisOperations
Parameters:
key - the key
Returns:
the value of key, or null when key does not exist.

exists

public Boolean exists(String key)
Description copied from interface: RedisOperations

Time complexity

O(1)

Determine if a key exists

Specified by:
exists in interface RedisOperations
Parameters:
key - the key
Returns:
true if a key exists otherwise false

del

public Long del(String... keys)
Description copied from interface: RedisOperations

Time complexity

O(N) where N is the number of keys that will be removed. When a key to remove holds a value other than a string, the individual complexity for this key is O(M) where M is the number of elements in the list, set, sorted set or hash. Removing a single key that holds a string value is O(1).

Removes the specified keys. A key is ignored if it does not exist.

Specified by:
del in interface RedisOperations
Parameters:
keys - keys
Returns:
The number of keys that were removed.

type

public String type(String key)
Description copied from interface: RedisOperations

Time complexity

O(1)

Returns the string representation of the type of the value stored at key. The different types that can be returned are: string, list, set, zset and hash.

Specified by:
type in interface RedisOperations
Parameters:
key - the key
Returns:
type of key, or none when key does not exist.

expire

public Boolean expire(String key,
                      int seconds)
Description copied from interface: RedisOperations

Time complexity

O(1)

Set a timeout on key. After the timeout has expired, the key will automatically be deleted. A key with an associated timeout is said to be volatile in Redis terminology.

For Redis versions < 2.1.3, existing timeouts cannot be overwritten. So, if key already has an associated timeout, it will do nothing and return 0. Since Redis 2.1.3, you can update the timeout of a key. It is also possible to remove the timeout using the PERSIST command. See the page on key expiry for more information.

Specified by:
expire in interface RedisOperations
Parameters:
key - the key
seconds - key timeout
Returns:
true if the timeout was set otherwise false

expireAt

public Boolean expireAt(String key,
                        long unixTime)
Description copied from interface: RedisOperations

Time complexity

O(1)

Set a timeout on key. After the timeout has expired, the key will automatically be deleted. A key with an associated timeout is said to be volatile in Redis terminology. EXPIREAT has the same effect and semantic as EXPIRE, but instead of specifying the number of seconds representing the TTL (time to live), it takes an absolute UNIX timestamp (seconds since January 1, 1970).

Background

EXPIREAT was introduced in order to convert relative timeouts to absolute timeouts for the AOF persistence mode. Of course, it can be used directly to specify that a given key should expire at a given time in the future.

Specified by:
expireAt in interface RedisOperations
Parameters:
key - a key
unixTime - an absolute UNIX timestamp (seconds since January 1, 1970)
Returns:
true if the timeout was set otherwise false

ttl

public Long ttl(String key)
Description copied from interface: RedisOperations

Time complexity

O(1)

Returns the remaining time to live of a key that has a timeout. This introspection capability allows a Redis client to check how many seconds a given key will continue to be part of the dataset.

Specified by:
ttl in interface RedisOperations
Parameters:
key - the key
Returns:
TTL in seconds or -1 when key does not exist or does not have a timeout.

getSet

public String getSet(String key,
                     String value)
Description copied from interface: RedisOperations

Time complexity

O(1)

Atomically sets key to value and returns the old value stored at key. Returns an error when key exists but does not hold a string value.

Specified by:
getSet in interface RedisOperations
Parameters:
key - the key
value - the value
Returns:
the old value stored at key, or null when key did not exist.

setnx

public Boolean setnx(String key,
                     String value)
Description copied from interface: RedisOperations

Time complexity

O(1)

Set key to hold string value if key does not exist. In that case, it is equal to SET. When key already holds a value, no operation is performed. SETNX is short for "SET if Not eXists".

Specified by:
setnx in interface RedisOperations
Parameters:
key - the key
value - the value
Returns:
true if the key was set otherwise false

setex

public String setex(String key,
                    int seconds,
                    String value)
Description copied from interface: RedisOperations

Time complexity

O(1) Set key to hold the string value and set key to timeout after a given number of seconds. This command is equivalent to executing the following commands:
SET key value
EXPIRE key seconds

SETEX is atomic, and can be reproduced by using the previous two commands inside an MULTI/EXEC block. It is provided as a faster alternative to the given sequence of operations, because this operation is very common when Redis is used as a cache.

An error is returned when seconds is invalid.

Specified by:
setex in interface RedisOperations
Parameters:
key - the key
seconds - ttl in seconds
value - the value
Returns:
Status code reply

decrBy

public Long decrBy(String key,
                   int value)
Description copied from interface: RedisOperations

Time complexity

O(1)

Decrements the number stored at key by decrement. If the key does not exist or contains a value of the wrong type, it is set to 0 before performing the operation. This operation is limited to 64 bit signed integers.

See INCR for extra information on increment/decrement operations.

Specified by:
decrBy in interface RedisOperations
Parameters:
key - the key
value - decrement value
Returns:
the value of key after the decrement

decr

public Long decr(String key)
Description copied from interface: RedisOperations

Time complexity

O(1)

Decrements the number stored at key by one. If the key does not exist or contains a value of the wrong type, it is set to 0 before performing the operation. This operation is limited to 64 bit signed integers.

See INCR for extra information on increment/decrement operations.

Specified by:
decr in interface RedisOperations
Parameters:
key - the key
Returns:
the value of key after the decrement

incrBy

public Long incrBy(String key,
                   int value)
Description copied from interface: RedisOperations

Time complexity

O(1) Increments the number stored at key by increment. If the key does not exist or contains a value of the wrong type, it is set to 0 before performing the operation. This operation is limited to 64 bit signed integers. See INCR for extra information on increment/decrement operations.

Specified by:
incrBy in interface RedisOperations
Parameters:
key - the key
value - increment value
Returns:
the value of key after the increment

incr

public Long incr(String key)
Description copied from interface: RedisOperations

Time complexity

O(1) Increments the number stored at key by one. If the key does not exist or contains a value of the wrong type, it is set to 0 before performing the operation. This operation is limited to 64 bit signed integers.

Note: this is a string operation because Redis does not have a dedicated integer type. The the string stored at the key is interpreted as a base-10 64 bit signed integer to execute the operation. Redis stores integers in their integer representation, so for string values that actually hold an integer, there is no overhead for storing the string representation of the integer.

Specified by:
incr in interface RedisOperations
Parameters:
key - the value
Returns:
the value of key after the increment

append

public Long append(String key,
                   String value)
Description copied from interface: RedisOperations

Time complexity

O(1). The amortized time complexity is O(1) assuming the appended value is small and the already present value is of any size, since the dynamic string library used by Redis will double the free space available on every reallocation.

If key already exists and is a string, this command appends the value at the end of the string. If key does not exist it is created and set as an empty string, so APPEND will be similar to SET in this special case.

Specified by:
append in interface RedisOperations
Parameters:
key - the key
value - the value
Returns:
the length of the string after the append operation

getRange

public String getRange(String key,
                       int start,
                       int end)
Description copied from interface: RedisOperations

Time complexity

O(N) where N is the length of the returned string. The complexity is ultimately determined by the returned length, but because creating a substring from an existing string is very cheap, it can be considered O(1) for small strings.

Warning: this command was renamed to GETRANGE, it is called SUBSTR in Redis versions <= 2.0.

Returns the substring of the string value stored at key, determined by the offsets start and end (both are inclusive). Negative offsets can be used in order to provide an offset starting from the end of the string. So -1 means the last character, -2 the penultimate and so forth. The function handles out of range requests by limiting the resulting range to the actual length of the string.

Specified by:
getRange in interface RedisOperations
Parameters:
key - the key
start - offset start
end - offset end
Returns:
substring of the string value stored at key

setRange

public Long setRange(String key,
                     int offset,
                     String value)
Description copied from interface: RedisOperations

Time complexity

O(1), not counting the time taken to copy the new string in place. Usually, this string is very small so the amortized complexity is O(1). Otherwise, complexity is O(M) with M being the length of the value argument.

Overwrites part of the string stored at key, starting at the specified offset, for the entire length of value. If the offset is larger than the current length of the string at key, the string is padded with zero-bytes to make offset fit. Non-existing keys are considered as empty strings, so this command will make sure it holds a string large enough to be able to set value at offset.

Note that the maximum offset that you can set is 229 -1 (536870911), as Redis Strings are limited to 512 megabytes. If you need to grow beyond this size, you can use multiple keys.

Warning: When setting the last possible byte and the string value stored at key does not yet hold a string value, or holds a small string value, Redis needs to allocate all intermediate memory which can block the server for some time. On a 2010 Macbook Pro, setting byte number 536870911 (512MB allocation) takes ~300ms, setting byte number 134217728 (128MB allocation) takes ~80ms, setting bit number 33554432 (32MB allocation) takes ~30ms and setting bit number 8388608 (8MB allocation) takes ~8ms. Note that once this first allocation is done, subsequent calls to SETRANGE for the same key will not have the allocation overhead.

Patterns

Thanks to SETRANGE and the analogous GETRANGE commands, you can use Redis strings as a linear array with O(1) random access. This is a very fast and efficient storage in many real world use cases.

Specified by:
setRange in interface RedisOperations
Parameters:
key - the key
offset - the offset
value - the value
Returns:
the length of the string after it was modified by the command.

strlen

public Long strlen(String key)
Description copied from interface: RedisOperations

Time complexity

O(1)

Returns the length of the string value stored at key. An error is returned when key holds a non-string value.

Specified by:
strlen in interface RedisOperations
Parameters:
key - the key
Returns:
the length of the string at key, or 0 when key does not exist.

keys

public Set<String> keys(String pattern)
Description copied from interface: RedisOperations

Time complexity

O(N) with N being the number of keys in the database, under the assumption that the key names in the database and the given pattern have limited length.

Returns all keys matching pattern.

While the time complexity for this operation is O(N), the constant times are fairly low. For example, Redis running on an entry level laptop can scan a 1 million key database in 40 milliseconds.

Warning: consider KEYS as a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout. Don't use KEYS in your regular application code. If you're looking for a way to find keys in a subset of your keyspace, consider using sets.

Supported glob-style patterns:

Use \ to escape special characters if you want to match them verbatim.

Specified by:
keys in interface RedisOperations
Parameters:
pattern - pattern
Returns:
list of keys matching pattern.

persist

public Boolean persist(String key)
Description copied from interface: RedisOperations

Time complexity

O(1)

Remove the existing timeout on key.

Specified by:
persist in interface RedisOperations
Parameters:
key - the key
Returns:
true if the timeout was removed otherwise false

hset

public Boolean hset(String key,
                    String field,
                    String value)
Description copied from interface: RedisOperations

Time complexity

O(1)

Sets field in the hash stored at key to value. If key does not exist, a new key holding a hash is created. If field already exists in the hash, it is overwritten.

Specified by:
hset in interface RedisOperations
Parameters:
key - the key
field - the field
value - the value
Returns:
true if field is a new field in the hash and value was set or false if field already exists in the hash and the value was updated.

hget

public String hget(String key,
                   String field)
Description copied from interface: RedisOperations

Time complexity

O(1)

Returns the value associated with field in the hash stored at key.

Specified by:
hget in interface RedisOperations
Parameters:
key - the key
field - the field
Returns:
the value associated with field, or null when field is not present in the hash or key does not exist.

hsetnx

public Boolean hsetnx(String key,
                      String field,
                      String value)
Description copied from interface: RedisOperations

Time complexity

O(1)

Sets field in the hash stored at key to value, only if field does not yet exist. If key does not exist, a new key holding a hash is created. If field already exists, this operation has no effect.

Specified by:
hsetnx in interface RedisOperations
Parameters:
key - the key
field - the field
value - the value
Returns:
true if field is a new field in the hash and value was set or false if field already exists in the hash and no operation was performed

hmset

public String hmset(String key,
                    Map<String,String> hash)
Description copied from interface: RedisOperations

Time complexity

O(N) where N is the number of fields being set.

Sets the specified fields to their respective values in the hash stored at key. This command overwrites any existing fields in the hash. If key does not exist, a new key holding a hash is created.

Specified by:
hmset in interface RedisOperations
Parameters:
key - the key
hash - the hash
Returns:
Status code reply

hmget

public List<String> hmget(String key,
                          String... fields)
Description copied from interface: RedisOperations

Time complexity

O(N) where N is the number of fields being requested.

Returns the values associated with the specified fields in the hash stored at key.

For every field that does not exist in the hash, a nil value is returned. Because a non-existing keys are treated as empty hashes, running HMGET against a non-existing key will return a list of nil values.

Specified by:
hmget in interface RedisOperations
Parameters:
key - the key
fields - the fields
Returns:
list of values associated with the given fields, in the same order as they are requested.

hincrBy

public Long hincrBy(String key,
                    String field,
                    int value)
Description copied from interface: RedisOperations

Time complexity

O(1)

Increments the number stored at field in the hash stored at key by increment. If key does not exist, a new key holding a hash is created. If field does not exist or holds a string that cannot be interpreted as integer, the value is set to 0 before the operation is performed.

The range of values supported by HINCRBY is limited to 64 bit signed integers.

Specified by:
hincrBy in interface RedisOperations
Parameters:
key - the key
field - the field
value - the value
Returns:
the value at field after the increment operation

hexists

public Boolean hexists(String key,
                       String field)
Description copied from interface: RedisOperations

Time complexity

O(1) Returns if field is an existing field in the hash stored at key.

Specified by:
hexists in interface RedisOperations
Parameters:
key - the key
field - the field
Returns:
true if the hash contains field or false if the hash does not contain field, or key does not exist.

hdel

public Boolean hdel(String key,
                    String field)
Description copied from interface: RedisOperations

Time complexity

O(1)

Removes field from the hash stored at key.

Specified by:
hdel in interface RedisOperations
Parameters:
key - the key
field - the field
Returns:
true if field was present in the hash and is now removed or false if field does not exist in the hash, or key does not exist.

hlen

public Long hlen(String key)
Description copied from interface: RedisOperations

Time complexity

O(1)

Returns the number of fields contained in the hash stored at key.

Specified by:
hlen in interface RedisOperations
Parameters:
key - the key
Returns:
number of fields in the hash, or 0 when key does not exist.

hkeys

public Set<String> hkeys(String key)
Description copied from interface: RedisOperations

Time complexity

O(N) where N is the size of the hash.

Returns all field names of the hash stored at key.

Specified by:
hkeys in interface RedisOperations
Parameters:
key - the key
Returns:
list of fields in the hash, or an empty list when key does not exist.

hvals

public List<String> hvals(String key)
Description copied from interface: RedisOperations

Time complexity

O(N) where N is the size of the hash.

Returns all values of the hash stored at key.

Specified by:
hvals in interface RedisOperations
Parameters:
key - the key
Returns:
list of values in the hash, or an empty list when key does not exist.

hgetAll

public Map<String,String> hgetAll(String key)
Description copied from interface: RedisOperations

Time complexity

O(N) where N is the size of the hash.

Returns all fields and values of the hash stored at key. In the returned value, every field name is followed by its value, so the length of the reply is twice the size of the hash.

Specified by:
hgetAll in interface RedisOperations
Parameters:
key - the key
Returns:
map of fields and their values stored in the hash, or an empty map when key does not exist

rpush

public Long rpush(String key,
                  String value)
Description copied from interface: RedisOperations

Time complexity

O(1)

Inserts value at the tail of the list stored at key. If key does not exist, it is created as empty list before performing the push operation. When key holds a value that is not a list, an error is returned.

Specified by:
rpush in interface RedisOperations
Parameters:
key - the key
value - the value
Returns:
the length of the list after the push operation.

rpushx

public Long rpushx(String key,
                   String value)
Description copied from interface: RedisOperations

Time complexity

O(1)

Inserts value at the tail of the list stored at key, only if key already exists and holds a list. In contrary to RPUSH, no operation will be performed when key does not yet exist.

Specified by:
rpushx in interface RedisOperations
Parameters:
key - the key
value - the value
Returns:
the length of the list after the push operation.

lpush

public Long lpush(String key,
                  String value)
Description copied from interface: RedisOperations

Time complexity

O(1)

Inserts value at the head of the list stored at key. If key does not exist, it is created as empty list before performing the push operation. When key holds a value that is not a list, an error is returned.

Specified by:
lpush in interface RedisOperations
Parameters:
key - the key
value - the value
Returns:
the length of the list after the push operation.

lpushx

public Long lpushx(String key,
                   String value)
Description copied from interface: RedisOperations

Time complexity

O(1)

Inserts value at the head of the list stored at key, only if key already exists and holds a list. In contrary to LPUSH, no operation will be performed when key does not yet exist.

Specified by:
lpushx in interface RedisOperations
Parameters:
key - the key
value - the value
Returns:
the length of the list after the push operation.

llen

public Long llen(String key)
Description copied from interface: RedisOperations

Time complexity

O(1)

Returns the length of the list stored at key. If key does not exist, it is interpreted as an empty list and 0 is returned. An error is returned when the value stored at key is not a list.

Specified by:
llen in interface RedisOperations
Parameters:
key - the key
Returns:
the length of the list at key.

lrange

public List<String> lrange(String key,
                           int start,
                           int end)
Description copied from interface: RedisOperations

Time complexity

O(S+N) where S is the start offset and N is the number of elements in the specified range.

Returns the specified elements of the list stored at key. The offsets start and end are zero-based indexes, with 0 being the first element of the list (the head of the list), 1 being the next element and so on.

These offsets can also be negative numbers indicating offsets starting at the end of the list. For example, -1 is the last element of the list, -2 the penultimate, and so on.

Consistency with range functions in various programming languages

Note that if you have a list of numbers from 0 to 100, LRANGE list 0 10 will return 11 elements, that is, the rightmost item is included. This may or may not be consistent with behavior of range-related functions in your programming language of choice (think Ruby's Range.new, Array#slice or Python's range() function).

Out-of-range indexes

Out of range indexes will not produce an error. If start is larger than the end of the list, or start > end, an empty list is returned. If end is larger than the actual end of the list, Redis will treat it like the last element of the list.

Specified by:
lrange in interface RedisOperations
Parameters:
key - the key
start - range start
end - range end
Returns:
list of elements in the specified range.

ltrim

public String ltrim(String key,
                    int start,
                    int end)
Description copied from interface: RedisOperations

Time complexity

O(N) where N is the number of elements to be removed by the operation.

Trim an existing list so that it will contain only the specified range of elements specified. Both start and stop are zero-based indexes, where 0 is the first element of the list (the head), 1 the next element and so on.

For example: LTRIM foobar 0 2 will modify the list stored at foobar so that only the first three elements of the list will remain.

start and end can also be negative numbers indicating offsets from the end of the list, where -1 is the last element of the list, -2 the penultimate element and so on.

Out of range indexes will not produce an error: if start is larger than the end of the list, or start > end, the result will be an empty list (which causes key to be removed). If end is larger than the end of the list, Redis will treat it like the last element of the list.

A common use of LTRIM is together with LPUSH/RPUSH. For example:

LPUSH mylist someelement LTRIM mylist 0 99

This pair of commands will push a new element on the list, while making sure that the list will not grow larger than 100 elements. This is very useful when using Redis to store logs for example. It is important to note that when used in this way LTRIM is an O(1) operation because in the average case just one element is removed from the tail of the list.

Specified by:
ltrim in interface RedisOperations
Parameters:
key - the key
start - range start
end - range end
Returns:
Status code reply

lindex

public String lindex(String key,
                     int index)
Description copied from interface: RedisOperations

Time complexity

O(N) where N is the number of elements to traverse to get to the element at index. This makes asking for the first or the last element of the list O(1).

Returns the element at index index in the list stored at key. The index is zero-based, so 0 means the first element, 1 the second element and so on. Negative indices can be used to designate elements starting at the tail of the list. Here, -1 means the last element, -2 means the penultimate and so forth.

When the value at key is not a list, an error is returned.

Specified by:
lindex in interface RedisOperations
Parameters:
key - the key
index - the index
Returns:
the requested element, or null when index is out of range.

lset

public String lset(String key,
                   int index,
                   String value)
Description copied from interface: RedisOperations

Time complexity

O(N) where N is the length of the list. Setting either the first or the last element of the list is O(1).

Sets the list element at index to value. For more information on the index argument, see LINDEX.

An error is returned for out of range indexes.

Specified by:
lset in interface RedisOperations
Parameters:
key - the key
index - the index
value - the value
Returns:
Status code reply

lrem

public Long lrem(String key,
                 int count,
                 String value)
Description copied from interface: RedisOperations

Time complexity

O(N) where N is the length of the list.

Removes the first count occurrences of elements equal to value from the list stored at key. The count argument influences the operation in the following ways:

For example, LREM list -2 "hello" will remove the last two occurrences of "hello" in the list stored at list.

Note that non-existing keys are treated like empty lists, so when key does not exist, the command will always return 0.

Specified by:
lrem in interface RedisOperations
Parameters:
key - the key
count - the count
value - the value
Returns:
the number of removed elements.

lpop

public String lpop(String key)
Description copied from interface: RedisOperations

Time complexity

O(1)

Removes and returns the first element of the list stored at key.

Specified by:
lpop in interface RedisOperations
Parameters:
key - the key
Returns:
the value of the first element, or null when key does not exist.

rpop

public String rpop(String key)
Description copied from interface: RedisOperations

Time complexity

O(1)

Removes and returns the last element of the list stored at key.

Specified by:
rpop in interface RedisOperations
Parameters:
key - the key
Returns:
the value of the last element, or nil when key does not exist.

sadd

public Boolean sadd(String key,
                    String member)
Description copied from interface: RedisOperations

Time complexity

O(1)

Add member to the set stored at key. If member is already a member of this set, no operation is performed. If key does not exist, a new set is created with member as its sole member.

An error is returned when the value stored at key is not a set.

Specified by:
sadd in interface RedisOperations
Parameters:
key - the key
member - the member
Returns:
true if the element was added or false if the element was already a member of the set

smembers

public Set<String> smembers(String key)
Description copied from interface: RedisOperations

Time complexity

O(N) where N is the set cardinality.

Returns all the members of the set value stored at key.

This has the same effect as running SINTER with one argument key.

Specified by:
smembers in interface RedisOperations
Parameters:
key - the key
Returns:
all elements of the set.

srem

public Boolean srem(String key,
                    String member)
Description copied from interface: RedisOperations

Time complexity

O(1)

Remove member from the set stored at key. If member is not a member of this set, no operation is performed.

An error is returned when the value stored at key is not a set.

Specified by:
srem in interface RedisOperations
Parameters:
key - the key
member - the member
Returns:
true if the element was removed or false if the element was not a member of the set.

spop

public String spop(String key)
Description copied from interface: RedisOperations

Time complexity

O(1)

Removes and returns a random element from the set value stored at key.

This operation is similar to SRANDMEMBER, that returns a random element from a set but does not remove it.

Specified by:
spop in interface RedisOperations
Parameters:
key - the key
Returns:
the removed element, or null when key does not exist.

scard

public Long scard(String key)
Description copied from interface: RedisOperations

Time complexity

O(1)

Returns the set cardinality (number of elements) of the set stored at key.

Specified by:
scard in interface RedisOperations
Parameters:
key - the key
Returns:
the cardinality (number of elements) of the set, or 0 if key does not exist.

sismember

public Boolean sismember(String key,
                         String member)
Description copied from interface: RedisOperations

Time complexity

O(1)

Returns if member is a member of the set stored at key.

Specified by:
sismember in interface RedisOperations
Parameters:
key - the key
member - the member
Returns:
if the element is a member of the set or false if the element is not a member of the set, or if key does not exist.

srandmember

public String srandmember(String key)
Description copied from interface: RedisOperations

Time complexity

O(1)

Returns a random element from the set value stored at key.

This operation is similar to SPOP, that also removes the randomly selected element.

Specified by:
srandmember in interface RedisOperations
Parameters:
key - the key
Returns:
the randomly selected element, or null when key does not exist.

zadd

public Boolean zadd(String key,
                    Number score,
                    String member)
Description copied from interface: RedisOperations
Time complexity

O(log(N)) where N is the number of elements in the sorted set.

Adds the member with the specified score to the sorted set stored at key. If member is already a member of the sorted set, the score is updated and the element reinserted at the right position to ensure the correct ordering. If key does not exist, a new sorted set with the specified member as sole member is created. If the key exists but does not hold a sorted set, an error is returned.

The score value should be the string representation of a numeric value, and accepts double precision floating point numbers.

For an introduction to sorted sets, see the data types page on sorted sets.

Specified by:
zadd in interface RedisOperations
Parameters:
key - the key
score - the score
member - the member
Returns:
true if the element was added or false if the element was already a member of the sorted set and the score was updated.

zrange

public List<String> zrange(String key,
                           int start,
                           int end)
Description copied from interface: RedisOperations
Time complexity

O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements returned.

Returns the specified range of elements in the sorted set stored at key. The elements are considered to be ordered from the lowest to the highest score.

See ZREVRANGE when you need the elements ordered from highest to lowest score.

Both start and stop are zero-based indexes, where 0 is the first element, 1 is the next element and so on. They can also be negative numbers indicating offsets from the end of the sorted set, with -1 being the last element of the sorted set, -2 the penultimate element and so on.

Out of range indexes will not produce an error. If start is larger than the largest index in the sorted set, or start > stop, an empty list is returned. If stop is larger than the end of the sorted set Redis will treat it like it is the last element of the sorted set.

It is possible to pass the WITHSCORES option in order to return the scores of the elements together with the elements. The returned list will contain value1,score1,...,valueN,scoreN instead of value1,...,valueN. Client libraries are free to return a more appropriate data type (suggestion: an array with (value, score)

Specified by:
zrange in interface RedisOperations
Parameters:
key - the key
start - range start
end - range end
Returns:
list of elements in the specified range.

zrem

public Boolean zrem(String key,
                    String member)
Description copied from interface: RedisOperations
Time complexity

O(log(N)) with N being the number of elements in the sorted set.

Removes the member from the sorted set stored at key. If member is not a member of the sorted set, no operation is performed.

An error is returned when key exists and does not hold a sorted set.

Specified by:
zrem in interface RedisOperations
Parameters:
key - the key
member - the member
Returns:
true if member was removed or false if member is not a member of the sorted set.

zincrby

public String zincrby(String key,
                      Number score,
                      String member)
Description copied from interface: RedisOperations
Time complexity

O(log(N)) where N is the number of elements in the sorted set.

Increments the score of member in the sorted set stored at key by increment. If member does not exist in the sorted set, it is added with increment as its score (as if its previous score was 0.0). If key does not exist, a new sorted set with the specified member as its sole member is created.

An error is returned when key exists but does not hold a sorted set.

The score value should be the string representation of a numeric value, and accepts double precision floating point numbers. It is possible to provide a negative value to decrement the score.

Specified by:
zincrby in interface RedisOperations
Parameters:
key - the key
score - the score
member - the member
Returns:
he new score of member (a double precision floating point number), represented as string.

zrank

public Long zrank(String key,
                  String member)
Description copied from interface: RedisOperations
Time complexity

O(log(N))

Returns the rank of member in the sorted set stored at key, with the scores ordered from low to high. The rank (or index) is 0-based, which means that the member with the lowest score has rank 0.

Use ZREVRANK to get the rank of an element with the scores ordered from high to low.

Specified by:
zrank in interface RedisOperations
Parameters:
key - the key
member - the member
Returns:
  • If member exists in the sorted set, the rank of member.
  • If member does not exist in the sorted set or key does not exist, null.

zrevrank

public Long zrevrank(String key,
                     String member)
Description copied from interface: RedisOperations
Time complexity

O(log(N))

Returns the rank of member in the sorted set stored at key, with the scores ordered from high to low. The rank (or index) is 0-based, which means that the member with the highest score has rank 0.

Use ZRANK to get the rank of an element with the scores ordered from low to high.

Specified by:
zrevrank in interface RedisOperations
Parameters:
key - the key
member - the member
Returns:
  • If member exists in the sorted set, the rank of member.
  • If member does not exist in the sorted set or key does not exist, Bulk reply: null.

    zrevrange

    public List<String> zrevrange(String key,
                                  int start,
                                  int end)
    Description copied from interface: RedisOperations
    Time complexity

    O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements returned.

    Returns the specified range of elements in the sorted set stored at key. The elements are considered to be ordered from the highest to the lowest score.

    Apart from the reversed ordering, ZREVRANGE is similar to ZRANGE.

    Specified by:
    zrevrange in interface RedisOperations
    Parameters:
    key - the key
    start - range start
    end - range end
    Returns:
    list of elements in the specified range

    zrangeWithScores

    public List<ElementScore> zrangeWithScores(String key,
                                               int start,
                                               int end)
    Description copied from interface: RedisOperations
    Time complexity

    O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements returned.

    Returns the specified range of elements in the sorted set stored at key. The elements are considered to be ordered from the lowest to the highest score.

    See ZREVRANGE when you need the elements ordered from highest to lowest score.

    Both start and stop are zero-based indexes, where 0 is the first element, 1 is the next element and so on. They can also be negative numbers indicating offsets from the end of the sorted set, with -1 being the last element of the sorted set, -2 the penultimate element and so on.

    Out of range indexes will not produce an error. If start is larger than the largest index in the sorted set, or start > stop, an empty list is returned. If stop is larger than the end of the sorted set Redis will treat it like it is the last element of the sorted set.

    It is possible to pass the WITHSCORES option in order to return the scores of the elements together with the elements. The returned list will contain value1,score1,...,valueN,scoreN instead of value1,...,valueN. Client libraries are free to return a more appropriate data type (suggestion: an array with (value, score)

    Specified by:
    zrangeWithScores in interface RedisOperations
    Parameters:
    key - the key
    start - range start
    end - range end
    Returns:
    list of elements in the specified range with their scores.

    zrevrangeWithScores

    public List<ElementScore> zrevrangeWithScores(String key,
                                                  int start,
                                                  int end)
    Description copied from interface: RedisOperations
    Time complexity

    O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements returned.

    Returns the specified range of elements in the sorted set stored at key. The elements are considered to be ordered from the highest to the lowest score.

    Apart from the reversed ordering, ZREVRANGE is similar to ZRANGE.

    Specified by:
    zrevrangeWithScores in interface RedisOperations
    Parameters:
    key - the key
    start - range start
    end - range end
    Returns:
    list of elements in the specified range with their scores

    zrevrangeByScore

    public List<String> zrevrangeByScore(String key,
                                         String max,
                                         String min)
    Description copied from interface: RedisOperations
    Time complexity

    O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned. If M is constant (e.g. always asking for the first 10 elements with LIMIT), you can consider it O(log(N)).

    Returns all the elements in the sorted set at key with a score between max and min (including elements with score equal to max or min). In contrary to the default ordering of sorted sets, for this command the elements are considered to be ordered from high to low scores.

    The elements having the same score are returned in reverse lexicographical order.

    Apart from the reversed ordering, ZREVRANGEBYSCORE is similar to ZRANGEBYSCORE.

    Specified by:
    zrevrangeByScore in interface RedisOperations
    Parameters:
    key - the key
    max - max score
    min - min score
    Returns:
    list of elements in the specified score range (optionally with their scores).

    zrevrangeByScore

    public List<String> zrevrangeByScore(String key,
                                         String max,
                                         String min,
                                         int offset,
                                         int count)
    Description copied from interface: RedisOperations
    Time complexity

    O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned. If M is constant (e.g. always asking for the first 10 elements with LIMIT), you can consider it O(log(N)).

    Returns all the elements in the sorted set at key with a score between max and min (including elements with score equal to max or min). In contrary to the default ordering of sorted sets, for this command the elements are considered to be ordered from high to low scores.

    The elements having the same score are returned in reverse lexicographical order.

    Apart from the reversed ordering, ZREVRANGEBYSCORE is similar to ZRANGEBYSCORE.

    Specified by:
    zrevrangeByScore in interface RedisOperations
    Parameters:
    key - the key
    max - max score
    min - min score
    offset - limit offset
    count - limit count
    Returns:
    list of elements in the specified score range (optionally with their scores).

    zrevrangeByScoreWithScores

    public List<ElementScore> zrevrangeByScoreWithScores(String key,
                                                         String max,
                                                         String min)
    Description copied from interface: RedisOperations
    Time complexity

    O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned. If M is constant (e.g. always asking for the first 10 elements with LIMIT), you can consider it O(log(N)).

    Returns all the elements in the sorted set at key with a score between max and min (including elements with score equal to max or min). In contrary to the default ordering of sorted sets, for this command the elements are considered to be ordered from high to low scores.

    The elements having the same score are returned in reverse lexicographical order.

    Apart from the reversed ordering, ZREVRANGEBYSCORE is similar to ZRANGEBYSCORE.

    Specified by:
    zrevrangeByScoreWithScores in interface RedisOperations
    Parameters:
    key - the key
    max - max score
    min - min score
    Returns:
    list of elements in the specified score range (optionally with their scores).

    zrevrangeByScoreWithScores

    public List<ElementScore> zrevrangeByScoreWithScores(String key,
                                                         String max,
                                                         String min,
                                                         int offset,
                                                         int count)
    Description copied from interface: RedisOperations
    Time complexity

    O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned. If M is constant (e.g. always asking for the first 10 elements with LIMIT), you can consider it O(log(N)).

    Returns all the elements in the sorted set at key with a score between max and min (including elements with score equal to max or min). In contrary to the default ordering of sorted sets, for this command the elements are considered to be ordered from high to low scores.

    The elements having the same score are returned in reverse lexicographical order.

    Apart from the reversed ordering, ZREVRANGEBYSCORE is similar to ZRANGEBYSCORE.

    Specified by:
    zrevrangeByScoreWithScores in interface RedisOperations
    Parameters:
    key - the key
    max - max score
    min - min score
    offset - limit offset
    count - limit count
    Returns:
    list of elements in the specified score range (optionally with their scores).

    zcard

    public Long zcard(String key)
    Description copied from interface: RedisOperations
    Time complexity

    O(1)

    Returns the sorted set cardinality (number of elements) of the sorted set stored at key.

    Specified by:
    zcard in interface RedisOperations
    Parameters:
    key - the key
    Returns:
    the cardinality (number of elements) of the sorted set, or 0 if key does not exist.

    zscore

    public String zscore(String key,
                         String member)
    Description copied from interface: RedisOperations
    Time complexity

    O(1)

    Returns the score of member in the sorted set at key.

    If member does not exist in the sorted set, or key does not exist, nil is returned.

    Specified by:
    zscore in interface RedisOperations
    Parameters:
    key - the key
    member - the member
    Returns:
    the score of member (a double precision floating point number), represented as string.

    sort

    public List<String> sort(String key)
    Description copied from interface: RedisOperations

    Time complexity

    O(N*log(N)) where N is the number of elements returned. When the elements are not sorted, complexity is O(N).

    Returns the elements contained in the list, set or sorted set at key. By default, sorting is numeric and elements are compared by their value interpreted as double precision floating point number.

    Specified by:
    sort in interface RedisOperations
    Parameters:
    key - the key
    Returns:
    list of sorted elements.

    sort

    public List<String> sort(String key,
                             SortingParams sortingParameters)
    Description copied from interface: RedisOperations

    Time complexity

    O(N*log(N)) where N is the number of elements returned. When the elements are not sorted, complexity is O(N).

    Returns the elements contained in the list, set or sorted set at key. By default, sorting is numeric and elements are compared by their value interpreted as double precision floating point number.

    Specified by:
    sort in interface RedisOperations
    Parameters:
    key - a key
    sortingParameters - sorting parameters
    Returns:
    list of sorted elements.
    See Also:
    SORT command

    zcount

    public Long zcount(String key,
                       Number min,
                       Number max)
    Description copied from interface: RedisOperations
    Time complexity

    O(log(N)+M) with N being the number of elements in the sorted set and M being the number of elements between min and max.

    Returns the number of elements in the sorted set at key with a score between min and max.

    The min and max arguments have the same semantic as described for ZRANGEBYSCORE.

    Specified by:
    zcount in interface RedisOperations
    Parameters:
    key - the key
    min - min value
    max - max value
    Returns:
    the number of elements in the specified score range.

    zrangeByScore

    public List<String> zrangeByScore(String key,
                                      String min,
                                      String max)
    Description copied from interface: RedisOperations
    Time complexity

    O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned. If M is constant (e.g. always asking for the first 10 elements with LIMIT), you can consider it O(log(N)).

    Returns all the elements in the sorted set at key with a score between min and max (including elements with score equal to min or max). The elements are considered to be ordered from low to high scores.

    The elements having the same score are returned in lexicographical order (this follows from a property of the sorted set implementation in Redis and does not involve further computation).

    The optional LIMIT argument can be used to only get a range of the matching elements (similar to SELECT LIMIT offset, count in SQL). Keep in mind that if offset is large, the sorted set needs to be traversed for offset elements before getting to the elements to return, which can add up to O(N) time complexity.

    The optional WITHSCORES argument makes the command return both the element and its score, instead of the element alone. This option is available since Redis 2.0.

    Exclusive intervals and infinity

    min and max can be -inf and +inf, so that you are not required to know the highest or lowest score in the sorted set to get all elements from or up to a certain score.

    By default, the interval specified by min and max is closed (inclusive). It is possible to specify an open interval (exclusive) by prefixing the score with the character (. For example:

    ZRANGEBYSCORE zset (1 5

    Will return all elements with 1 < score <= 5 while:

    ZRANGEBYSCORE zset (5 (10

    Will return all the elements with 5 < score < 10 (5 and 10 excluded).

    Specified by:
    zrangeByScore in interface RedisOperations
    Parameters:
    key - the key
    min - min score
    max - max score
    Returns:
    list of elements in the specified score range.

    zrangeByScore

    public List<String> zrangeByScore(String key,
                                      String min,
                                      String max,
                                      int offset,
                                      int count)
    Description copied from interface: RedisOperations
    Time complexity

    O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned. If M is constant (e.g. always asking for the first 10 elements with LIMIT), you can consider it O(log(N)).

    Returns all the elements in the sorted set at key with a score between min and max (including elements with score equal to min or max). The elements are considered to be ordered from low to high scores.

    The elements having the same score are returned in lexicographical order (this follows from a property of the sorted set implementation in Redis and does not involve further computation).

    The optional LIMIT argument can be used to only get a range of the matching elements (similar to SELECT LIMIT offset, count in SQL). Keep in mind that if offset is large, the sorted set needs to be traversed for offset elements before getting to the elements to return, which can add up to O(N) time complexity.

    The optional WITHSCORES argument makes the command return both the element and its score, instead of the element alone. This option is available since Redis 2.0.

    Exclusive intervals and infinity

    min and max can be -inf and +inf, so that you are not required to know the highest or lowest score in the sorted set to get all elements from or up to a certain score.

    By default, the interval specified by min and max is closed (inclusive). It is possible to specify an open interval (exclusive) by prefixing the score with the character (. For example:

    ZRANGEBYSCORE zset (1 5

    Will return all elements with 1 < score <= 5 while:

    ZRANGEBYSCORE zset (5 (10

    Will return all the elements with 5 < score < 10 (5 and 10 excluded).

    Specified by:
    zrangeByScore in interface RedisOperations
    Parameters:
    key - the key
    min - min score
    max - max score
    offset - limit offset
    count - limit count
    Returns:
    list of elements in the specified score range.

    zrangeByScoreWithScores

    public List<ElementScore> zrangeByScoreWithScores(String key,
                                                      String min,
                                                      String max)
    Description copied from interface: RedisOperations
    Time complexity

    O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned. If M is constant (e.g. always asking for the first 10 elements with LIMIT), you can consider it O(log(N)).

    Returns all the elements in the sorted set at key with a score between min and max (including elements with score equal to min or max). The elements are considered to be ordered from low to high scores.

    The elements having the same score are returned in lexicographical order (this follows from a property of the sorted set implementation in Redis and does not involve further computation).

    The optional LIMIT argument can be used to only get a range of the matching elements (similar to SELECT LIMIT offset, count in SQL). Keep in mind that if offset is large, the sorted set needs to be traversed for offset elements before getting to the elements to return, which can add up to O(N) time complexity.

    The optional WITHSCORES argument makes the command return both the element and its score, instead of the element alone. This option is available since Redis 2.0.

    Exclusive intervals and infinity

    min and max can be -inf and +inf, so that you are not required to know the highest or lowest score in the sorted set to get all elements from or up to a certain score.

    By default, the interval specified by min and max is closed (inclusive). It is possible to specify an open interval (exclusive) by prefixing the score with the character (. For example:

    ZRANGEBYSCORE zset (1 5

    Will return all elements with 1 < score <= 5 while:

    ZRANGEBYSCORE zset (5 (10

    Will return all the elements with 5 < score < 10 (5 and 10 excluded).

    Specified by:
    zrangeByScoreWithScores in interface RedisOperations
    Parameters:
    key - the key
    min - min score
    max - max score
    Returns:
    list of elements in the specified score range with their scores.

    zrangeByScoreWithScores

    public List<ElementScore> zrangeByScoreWithScores(String key,
                                                      String min,
                                                      String max,
                                                      int offset,
                                                      int count)
    Description copied from interface: RedisOperations
    Time complexity

    O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements being returned. If M is constant (e.g. always asking for the first 10 elements with LIMIT), you can consider it O(log(N)).

    Returns all the elements in the sorted set at key with a score between min and max (including elements with score equal to min or max). The elements are considered to be ordered from low to high scores.

    The elements having the same score are returned in lexicographical order (this follows from a property of the sorted set implementation in Redis and does not involve further computation).

    The optional LIMIT argument can be used to only get a range of the matching elements (similar to SELECT LIMIT offset, count in SQL). Keep in mind that if offset is large, the sorted set needs to be traversed for offset elements before getting to the elements to return, which can add up to O(N) time complexity.

    The optional WITHSCORES argument makes the command return both the element and its score, instead of the element alone. This option is available since Redis 2.0.

    Exclusive intervals and infinity

    min and max can be -inf and +inf, so that you are not required to know the highest or lowest score in the sorted set to get all elements from or up to a certain score.

    By default, the interval specified by min and max is closed (inclusive). It is possible to specify an open interval (exclusive) by prefixing the score with the character (. For example:

    ZRANGEBYSCORE zset (1 5

    Will return all elements with 1 < score <= 5 while:

    ZRANGEBYSCORE zset (5 (10

    Will return all the elements with 5 < score < 10 (5 and 10 excluded).

    Specified by:
    zrangeByScoreWithScores in interface RedisOperations
    Parameters:
    key - the key
    min - min score
    max - max score
    offset - limit offset
    count - limit count
    Returns:
    list of elements in the specified score range (optionally with their scores).

    zremrangeByRank

    public Long zremrangeByRank(String key,
                                int start,
                                int end)
    Description copied from interface: RedisOperations
    Time complexity

    O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements removed by the operation.

    Removes all elements in the sorted set stored at key with rank between start and stop. Both start and stop are 0-based indexes with 0 being the element with the lowest score. These indexes can be negative numbers, where they indicate offsets starting at the element with the highest score. For example: -1 is the element with the highest score, -2 the element with the second highest score and so forth.

    Specified by:
    zremrangeByRank in interface RedisOperations
    Parameters:
    key - the key
    start - range start
    end - range end
    Returns:
    the number of elements removed.

    zremrangeByScore

    public Long zremrangeByScore(String key,
                                 String min,
                                 String max)
    Description copied from interface: RedisOperations
    Time complexity

    O(log(N)+M) with N being the number of elements in the sorted set and M the number of elements removed by the operation.

    Removes all elements in the sorted set stored at key with a score between min and max (inclusive).

    Since version 2.1.6, min and max can be exclusive, following the syntax of ZRANGEBYSCORE.

    Specified by:
    zremrangeByScore in interface RedisOperations
    Parameters:
    key - the key
    min - min score
    max - max score
    Returns:
    the number of elements removed.

    linsert

    public Long linsert(String key,
                        Client.LIST_POSITION where,
                        String pivot,
                        String value)
    Description copied from interface: RedisOperations

    Time complexity

    O(N) where N is the number of elements to traverse before seeing the value pivot. This means that inserting somewhere on the left end on the list (head) can be considered O(1) and inserting somewhere on the right end (tail) is O(N).

    Inserts value in the list stored at key either before or after the reference value pivot.

    When key does not exist, it is considered an empty list and no operation is performed.

    An error is returned when key exists but does not hold a list value.

    Specified by:
    linsert in interface RedisOperations
    Parameters:
    key - the key
    where - list position
    pivot - the pivot
    value - the value
    Returns:
    the length of the list after the insert operation, or -1 when the value pivot was not found.

    publish

    public Long publish(String channel,
                        String message)
    Specified by:
    publish in interface RedisOperations

    getBit

    public Long getBit(String key,
                       int offset)
    Description copied from interface: RedisOperations

    Time complexity

    O(1)

    Returns the bit value at offset in the string value stored at key. When offset is beyond the string length, the string is assumed to be a contiguous space with 0 bits. When key does not exist it is assumed to be an empty string, so offset is always out of range and the value is also assumed to be a contiguous space with 0 bits.

    Specified by:
    getBit in interface RedisOperations
    Parameters:
    key - the key
    offset - offset in the string value stored at key
    Returns:
    the bit value stored at offset.

    setBit

    public Long setBit(String key,
                       int offset,
                       String value)
    Description copied from interface: RedisOperations

    Time complexity

    O(1)

    Sets or clears the bit at offset in the string value stored at key.

    The bit is either set or cleared depending on value, which can be either 0 or 1. When key does not exist, a new string value is created. The string is grown to make sure it can hold a bit at offset. The offset argument is required to be greater than or equal to 0, and smaller than 232 (this limits bitmaps to 512MB). When the string at key is grown, added bits are set to 0.

    Warning: When setting the last possible bit (offset equal to 232 -1) and the string value stored at key does not yet hold a string value, or holds a small string value, Redis needs to allocate all intermediate memory which can block the server for some time. On a 2010 Macbook Pro, setting bit number 232 -1 (512MB allocation) takes ~300ms, setting bit number 230 -1 (128MB allocation) takes ~80ms, setting bit number 228 -1 (32MB allocation) takes ~30ms and setting bit number 226 -1 (8MB allocation) takes ~8ms. Note that once this first allocation is done, subsequent calls to SETBIT for the same key will not have the allocation overhead.

    Specified by:
    setBit in interface RedisOperations
    Parameters:
    key - the key
    offset - the offset
    value - the value, must be 0 or 1
    Returns:
    the original bit value stored at offset.


    Copyright © 2011. All Rights Reserved.