org.idevlab.rjc
Interface RedisOperations
- All Known Subinterfaces:
- Session, SingleRedisOperations
- All Known Implementing Classes:
- RedisNode, ShardedRedis
public interface RedisOperations
Common interface for sharded and non-sharded Redis
|
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. |
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. |
set
String set(String key,
String value)
Time complexity
O(1)
Set key to hold the string value. If key already holds a value, it is overwritten, regardless of its type.
- Parameters:
key - the keyvalue - the value
- Returns:
- always OK since SET can't fail
get
String get(String key)
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.
- Parameters:
key - the key
- Returns:
- the value of key, or null when key does not exist.
exists
Boolean exists(String key)
Time complexity
O(1)
Determine if a key exists
- Parameters:
key - the key
- Returns:
- true if a key exists otherwise false
del
Long del(String... keys)
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.
- Parameters:
keys - keys
- Returns:
- The number of keys that were removed.
type
String type(String key)
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.
- Parameters:
key - the key
- Returns:
- type of key, or none when key does not exist.
expire
Boolean expire(String key,
int seconds)
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.
- Parameters:
key - the keyseconds - key timeout
- Returns:
- true if the timeout was set otherwise false
expireAt
Boolean expireAt(String key,
long unixTime)
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.
- Parameters:
key - a keyunixTime - an absolute UNIX timestamp (seconds since January 1, 1970)
- Returns:
- true if the timeout was set otherwise false
ttl
Long ttl(String key)
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.
- Parameters:
key - the key
- Returns:
- TTL in seconds or -1 when key does not exist or does not have a timeout.
getSet
String getSet(String key,
String value)
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.
- Parameters:
key - the keyvalue - the value
- Returns:
- the old value stored at key, or null when key did not exist.
setnx
Boolean setnx(String key,
String value)
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".
- Parameters:
key - the keyvalue - the value
- Returns:
- true if the key was set otherwise false
setex
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.
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.
- Parameters:
key - the keyseconds - ttl in secondsvalue - the value
- Returns:
- Status code reply
decrBy
Long decrBy(String key,
int value)
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.
- Parameters:
key - the keyvalue - decrement value
- Returns:
- the value of key after the decrement
decr
Long decr(String key)
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.
- Parameters:
key - the key
- Returns:
- the value of key after the decrement
incrBy
Long incrBy(String key,
int value)
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.
- Parameters:
key - the keyvalue - increment value
- Returns:
- the value of key after the increment
incr
Long incr(String key)
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.
- Parameters:
key - the value
- Returns:
- the value of key after the increment
append
Long append(String key,
String value)
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.
- Parameters:
key - the keyvalue - the value
- Returns:
- the length of the string after the append operation
getRange
String getRange(String key,
int start,
int end)
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.
- Parameters:
key - the keystart - offset startend - offset end
- Returns:
- substring of the string value stored at key
setRange
Long setRange(String key,
int offset,
String value)
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.
- Parameters:
key - the keyoffset - the offsetvalue - the value
- Returns:
- the length of the string after it was modified by the command.
- Since:
- 2.1.8
strlen
Long strlen(String key)
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.
- Parameters:
key - the key
- Returns:
- the length of the string at key, or 0 when key does not exist.
- Since:
- 2.1.2
keys
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.
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:
- h?llo matches hello, hallo and hxllo
- h*llo matches hllo and heeeello
- h[ae]llo matches hello and hallo, but not hillo
Use \ to escape special characters if you want to match them verbatim.
- Parameters:
pattern - pattern
- Returns:
- list of keys matching pattern.
persist
Boolean persist(String key)
Time complexity
O(1)
Remove the existing timeout on key.
- Parameters:
key - the key
- Returns:
- true if the timeout was removed otherwise false
- Since:
- Redis 2.1.2
hset
Boolean hset(String key,
String field,
String value)
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.
- Parameters:
key - the keyfield - the fieldvalue - 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
String hget(String key,
String field)
Time complexity
O(1)
Returns the value associated with field in the hash stored at key.
- Parameters:
key - the keyfield - the field
- Returns:
- the value associated with field, or null when field is not present in the hash or key does not exist.
hsetnx
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.
If key does not exist, a new key holding a hash is created.
If field already exists, this operation has no effect.
- Parameters:
key - the keyfield - the fieldvalue - 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
String hmset(String key,
Map<String,String> hash)
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.
- Parameters:
key - the keyhash - the hash
- Returns:
- Status code reply
hmget
List<String> hmget(String key,
String... fields)
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.
- Parameters:
key - the keyfields - the fields
- Returns:
- list of values associated with the given fields, in the same order as they are requested.
hincrBy
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.
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.
- Parameters:
key - the keyfield - the fieldvalue - the value
- Returns:
- the value at field after the increment operation
hexists
Boolean hexists(String key,
String field)
Time complexity
O(1)
Returns if field is an existing field in the hash stored at key.
- Parameters:
key - the keyfield - the field
- Returns:
- true if the hash contains field or false if the hash does not contain field, or key does not exist.
hdel
Boolean hdel(String key,
String field)
Time complexity
O(1)
Removes field from the hash stored at key.
- Parameters:
key - the keyfield - 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
Long hlen(String key)
Time complexity
O(1)
Returns the number of fields contained in the hash stored at key.
- Parameters:
key - the key
- Returns:
- number of fields in the hash, or 0 when key does not exist.
hkeys
Set<String> hkeys(String key)
Time complexity
O(N) where N is the size of the hash.
Returns all field names of the hash stored at key.
- Parameters:
key - the key
- Returns:
- list of fields in the hash, or an empty list when key does not exist.
hvals
List<String> hvals(String key)
Time complexity
O(N) where N is the size of the hash.
Returns all values of the hash stored at key.
- Parameters:
key - the key
- Returns:
- list of values in the hash, or an empty list when key does not exist.
hgetAll
Map<String,String> hgetAll(String key)
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.
- 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
Long rpush(String key,
String value)
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.
- Parameters:
key - the keyvalue - the value
- Returns:
- the length of the list after the push operation.
rpushx
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.
In contrary to RPUSH, no operation will be performed when key does not yet exist.
- Parameters:
key - the keyvalue - the value
- Returns:
- the length of the list after the push operation.
lpush
Long lpush(String key,
String value)
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.
- Parameters:
key - the keyvalue - the value
- Returns:
- the length of the list after the push operation.
lpushx
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.
In contrary to LPUSH, no operation will be performed when key does not yet exist.
- Parameters:
key - the keyvalue - the value
- Returns:
- the length of the list after the push operation.
llen
Long llen(String key)
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.
- Parameters:
key - the key
- Returns:
- the length of the list at key.
lrange
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.
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.
- Parameters:
key - the keystart - range startend - range end
- Returns:
- list of elements in the specified range.
ltrim
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.
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.
- Parameters:
key - the keystart - range startend - range end
- Returns:
- Status code reply
lindex
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.
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.
- Parameters:
key - the keyindex - the index
- Returns:
- the requested element, or null when index is out of range.
lset
String lset(String key,
int index,
String value)
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.
- Parameters:
key - the keyindex - the indexvalue - the value
- Returns:
- Status code reply
lrem
Long lrem(String key,
int count,
String value)
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:
- count > 0: Remove elements equal to value moving from head to tail.
- count < 0: Remove elements equal to value moving from tail to head.
- count = 0: Remove all elements equal to value.
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.
- Parameters:
key - the keycount - the countvalue - the value
- Returns:
- the number of removed elements.
lpop
String lpop(String key)
Time complexity
O(1)
Removes and returns the first element of the list stored at key.
- Parameters:
key - the key
- Returns:
- the value of the first element, or null when key does not exist.
rpop
String rpop(String key)
Time complexity
O(1)
Removes and returns the last element of the list stored at key.
- Parameters:
key - the key
- Returns:
- the value of the last element, or nil when key does not exist.
sadd
Boolean sadd(String key,
String member)
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.
- Parameters:
key - the keymember - the member
- Returns:
- true if the element was added or false if the element was already a member of the set
smembers
Set<String> smembers(String key)
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.
- Parameters:
key - the key
- Returns:
- all elements of the set.
srem
Boolean srem(String key,
String member)
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.
- Parameters:
key - the keymember - the member
- Returns:
- true if the element was removed or false if the element was not a member of the set.
spop
String spop(String key)
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.
- Parameters:
key - the key
- Returns:
- the removed element, or null when key does not exist.
scard
Long scard(String key)
Time complexity
O(1)
Returns the set cardinality (number of elements) of the set stored at key.
- Parameters:
key - the key
- Returns:
- the cardinality (number of elements) of the set, or 0 if key does not exist.
sismember
Boolean sismember(String key,
String member)
Time complexity
O(1)
Returns if member is a member of the set stored at key.
- Parameters:
key - the keymember - 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
String srandmember(String key)
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.
- Parameters:
key - the key
- Returns:
- the randomly selected element, or null when key does not exist.
zadd
Boolean zadd(String key,
Number score,
String member)
- 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.
- Parameters:
key - the keyscore - the scoremember - 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
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.
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)
- Parameters:
key - the keystart - range startend - range end
- Returns:
- list of elements in the specified range.
zrem
Boolean zrem(String key,
String member)
- 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.
- Parameters:
key - the keymember - the member
- Returns:
- true if member was removed or false if member is not a member of the sorted set.
zincrby
String zincrby(String key,
Number score,
String member)
- 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.
- Parameters:
key - the keyscore - the scoremember - the member
- Returns:
- he new score of member (a double precision floating point number), represented as string.
zrank
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.
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.
- Parameters:
key - the keymember - 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
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. 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.
- Parameters:
key - the keymember - 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
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.
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.
- Parameters:
key - the keystart - range startend - range end
- Returns:
- list of elements in the specified range
zrangeWithScores
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.
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)
- Parameters:
key - the keystart - range startend - range end
- Returns:
- list of elements in the specified range with their scores.
zrevrangeWithScores
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.
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.
- Parameters:
key - the keystart - range startend - range end
- Returns:
- list of elements in the specified range with their scores
zrevrangeByScore
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.
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.
- Parameters:
key - the keymax - max scoremin - min score
- Returns:
- list of elements in the specified score range (optionally with their scores).
zrevrangeByScore
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.
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.
- Parameters:
key - the keymax - max scoremin - min scoreoffset - limit offsetcount - limit count
- Returns:
- list of elements in the specified score range (optionally with their scores).
zrevrangeByScoreWithScores
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.
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.
- Parameters:
key - the keymax - max scoremin - min score
- Returns:
- list of elements in the specified score range (optionally with their scores).
zrevrangeByScoreWithScores
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.
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.
- Parameters:
key - the keymax - max scoremin - min scoreoffset - limit offsetcount - limit count
- Returns:
- list of elements in the specified score range (optionally with their scores).
zcard
Long zcard(String key)
- Time complexity
O(1)
Returns the sorted set cardinality (number of elements) of the sorted set stored at key.
- Parameters:
key - the key
- Returns:
- the cardinality (number of elements) of the sorted set, or 0 if key does not exist.
zscore
String zscore(String key,
String member)
- 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.
- Parameters:
key - the keymember - the member
- Returns:
- the score of member (a double precision floating point number), represented as string.
sort
List<String> sort(String key)
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.
- Parameters:
key - the key
- Returns:
- list of sorted elements.
sort
List<String> sort(String key,
SortingParams sortingParameters)
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.
- Parameters:
key - a keysortingParameters - sorting parameters
- Returns:
- list of sorted elements.
- See Also:
- SORT command
zcount
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.
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.
- Parameters:
key - the keymin - min valuemax - max value
- Returns:
- the number of elements in the specified score range.
zrangeByScore
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.
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).
- Parameters:
key - the keymin - min scoremax - max score
- Returns:
- list of elements in the specified score range.
zrangeByScore
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.
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).
- Parameters:
key - the keymin - min scoremax - max scoreoffset - limit offsetcount - limit count
- Returns:
- list of elements in the specified score range.
zrangeByScoreWithScores
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.
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).
- Parameters:
key - the keymin - min scoremax - max score
- Returns:
- list of elements in the specified score range with their scores.
zrangeByScoreWithScores
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.
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).
- Parameters:
key - the keymin - min scoremax - max scoreoffset - limit offsetcount - limit count
- Returns:
- list of elements in the specified score range (optionally with their scores).
zremrangeByRank
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.
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.
- Parameters:
key - the keystart - range startend - range end
- Returns:
- the number of elements removed.
zremrangeByScore
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.
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.
- Parameters:
key - the keymin - min scoremax - max score
- Returns:
- the number of elements removed.
linsert
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.
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.
- Parameters:
key - the keywhere - list positionpivot - the pivotvalue - the value
- Returns:
- the length of the list after the insert operation, or -1 when the value pivot was not found.
publish
Long publish(String channel,
String message)
getBit
Long getBit(String key,
int offset)
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.
- Parameters:
key - the keyoffset - offset in the string value stored at key
- Returns:
- the bit value stored at offset.
- Since:
- 2.1.8.
setBit
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.
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.
- Parameters:
key - the keyoffset - the offsetvalue - the value, must be 0 or 1
- Returns:
- the original bit value stored at offset.
- Since:
- 2.1.8.
Copyright © 2011. All Rights Reserved.