informat.redis Redis
Overview
Use the informat.redis object for Redis-related operations
opsForValue
Get value operation object
const ops = informat.redis.opsForValue();Return Value
Returns a value operation object.
Example
const ops = informat.redis.opsForValue();Note:
- This method requires no parameters and returns an object that can perform value operations.
opsForList
Get list operation object
const ops = informat.redis.opsForList();Return Value
Returns a list operation object.
Example
const ops = informat.redis.opsForList();opsForGeo
Get geo operation object
const ops = informat.redis.opsForGeo();Return Value
Returns a geo operation object.
Example
const ops = informat.redis.opsForGeo();redisTemplate
Get Redis operation template
const redisTemplate = informat.redis.redisTemplate();Return Value
Returns a Redis operation template object.
Example
const redisTemplate = informat.redis.redisTemplate();delete
Delete the value of a specified key
informat.redis.delete(key);| Parameter | Type | Description |
|---|---|---|
| key | String | The key to delete |
Return Value
Returns the result of the delete operation, true for success, false for failure.
Example
informat.redis.delete("key1");Note:
- The parameter key must be of string type. This method is suitable for deleting a single key.
deleteAll
Delete all values of specified keys
informat.redis.deleteAll(keys);| Parameter | Type | Description |
|---|---|---|
| keys | Array<String> | The group of keys to delete |
Return Value
Returns the number of keys deleted, type is Number
Example
informat.redis.deleteAll(["key1", "key2", "key3"]);Note:
- The parameter keys must be of array type, and array elements must be of string type. This method is suitable for batch deleting keys.
set
Set the value of a specified key
ops.set(key, value, timeout, redisTimeoutUnit);| Parameter | Type | Description |
|---|---|---|
| key | String | The key to set |
| value | String | The value to set |
| timeout | Integer | Optional, valid time |
| redisTimeoutUnit | RedisTimeoutUnit | Optional, time unit |
RedisTimeoutUnit Options
- NANOSECONDS Nanoseconds
- MICROSECONDS Microseconds
- MILLISECONDS Milliseconds
- SECONDS Seconds
- MINUTES Minutes
- HOURS Hours
- DAYS Days
Return Value
No return value.
Example
const ops = informat.redis.opsForValue();
ops.set("key1", "value1", 5, "MINUTES");Note:
- Parameters key and value must both be of string type. This method is suitable for setting a single key-value pair and can be used for caching or persisting data.
setIfAbsent
Set the value of a specified key only if the key does not exist
ops.setIfAbsent(key, value, timeout, redisTimeoutUnit);| Parameter | Type | Description |
|---|---|---|
| key | String | The key to set |
| value | String | The value to set |
| timeout | Integer | Optional, valid time |
| redisTimeoutUnit | RedisTimeoutUnit | Optional, time unit |
Return Value
Returns a boolean indicating whether the value was set successfully.
Example
const ops = informat.redis.opsForValue();
const success = ops.setIfAbsent("key1", "value1", 5, "MINUTES");Note:
- Parameters key and value must both be of string type. This method is suitable for setting a value only when the key does not exist and can be used to implement distributed locks.
setIfPresent
Set the value of a specified key only if the key exists
ops.setIfPresent(key, value, timeout, redisTimeoutUnit);| Parameter | Type | Description |
|---|---|---|
| key | String | The key to set |
| value | String | The value to set |
| timeout | Integer | Optional, timeout |
| redisTimeoutUnit | RedisTimeoutUnit | Optional, time unit |
Return Value
Returns a boolean indicating whether the value was set successfully.
Example
const ops = informat.redis.opsForValue();
const success = ops.setIfPresent("key1", "value1", "5", "MINUTES");Note:
- Parameters key and value must both be of string type. This method is suitable for setting a value only when the key exists and can be used to update existing key-value pairs.
multiSet
Batch set values for specified keys
ops.multiSet(keyValuePairs);| Parameter | Type | Description |
|---|---|---|
| keyValuePairs | Object | The key-value pairs object to set |
Return Value
No return value.
Example
const ops = informat.redis.opsForValue();
ops.multiSet({ key1: "value1", key2: "value2", key3: "value3" });Note:
- Parameter keyValuePairs must be an object type, and both keys and values of the object must be string types. This method is suitable for batch setting data scenarios, which can reduce communication with Redis and improve system performance.
multiSetIfAbsent
Batch set values for specified keys only if all keys do not exist
ops.multiSetIfAbsent(keyValuePairs);| Parameter | Type | Description |
|---|---|---|
| keyValuePairs | Object | The key-value pairs object to set |
Return Value
Returns a boolean indicating whether the values were set successfully.
Example
const ops = informat.redis.opsForValue();
const success = ops.multiSetIfAbsent({ key1: "value1", key2: "value2", key3: "value3" });Note:
- Parameter keyValuePairs must be an object type, and both keys and values of the object must be string types. This method is suitable for batch setting values only when all keys do not exist and can be used to implement distributed locks.
setRange
Set a substring of the value for a specified key
ops.setRange(key, value, offset);| Parameter | Type | Description |
|---|---|---|
| key | String | The key to set |
| value | String | The substring to set |
| offset | Integer | The starting position of the substring |
Return Value
No return value.
Example
const ops = informat.redis.opsForValue();
ops.setRange("key1", "value1", 2);Note:
- Parameters key and value must be string types, and offset must be a numeric type. This method is suitable for updating part of a string's content.
get
Get the value of a specified key
ops.get(key);| Parameter | Type | Description |
|---|---|---|
| key | String | The key to get |
Return Value
Returns the value corresponding to the key, or null if the key does not exist.
Example
const ops = informat.redis.opsForValue();
const value = ops.get("key1");Note:
- Parameter key must be a string type. This method is suitable for getting a single key-value pair and can be used to read cached data or persisted data.
getAndExpire
Get the value of a specified key and set an expiration time
ops.getAndExpire(key, timeout, redisTimeoutUnit);| Parameter | Type | Description |
|---|---|---|
| key | String | The key to get |
| timeout | Integer | Expiration time |
| redisTimeoutUnit | RedisTimeoutUnit | Time unit |
Return Value
Returns the value corresponding to the key, or null if the key does not exist.
Example
const ops = informat.redis.opsForValue();
const value = ops.getAndExpire("key1", 60, "SECONDS"); // Get the value of key1 and set it to expire in 60 secondsNote:
- Parameter key must be a string type, and timeout must be a numeric type. This method is suitable for scenarios where you need to get the value and set the expiration time simultaneously.
getAndPersist
Get the value of a specified key and remove the expiration time
ops.getAndPersist(key);| Parameter | Type | Description |
|---|---|---|
| key | String | The key to get |
Return Value
Returns the value corresponding to the key, or null if the key does not exist.
Example
const ops = informat.redis.opsForValue();
const value = ops.getAndPersist("key1"); // Get the value of key1 and remove its expiration timeNote:
- Parameter key must be a string type. This method is suitable for scenarios where you need to get the value and remove the expiration time simultaneously.
getAndSet
Get the value of a specified key and set a new value
ops.getAndSet(key, newValue);| Parameter | Type | Description |
|---|---|---|
| key | String | The key to get |
| newValue | String | The new value |
Return Value
Returns the previous value corresponding to the key, or null if the key does not exist.
Example
const ops = informat.redis.opsForValue();
const oldValue = ops.getAndSet("key1", "newValue"); // Get the old value of key1 and set the new value to newValueNote:
- Both parameters key and newValue must be string types. This method is suitable for scenarios where you need to get the old value and set a new value.
multiGet
Batch get values for specified keys
ops.multiGet(keys);| Parameter | Type | Description |
|---|---|---|
| keys | Array<String> | The group of keys to get |
Return Value
Returns an array of values corresponding to the keys, or null for keys that do not exist.
Example
const ops = informat.redis.opsForValue();
const values = ops.multiGet(["key1", "key2", "key3"]);Note:
- Parameter keys must be an array type, and array elements must be string types. This method is suitable for batch data retrieval scenarios, which can reduce communication with Redis and improve system performance.
increment
Increment the value of a specified key by 1
ops.increment(key);| Parameter | Type | Description |
|---|---|---|
| key | String | The key to increment |
Return Value
Returns the value after increment.
Example
const ops = informat.redis.opsForValue();
const newValue = ops.increment("key1"); // Increment the value of key1 by 1Note:
- Parameter key must be a string type. This method is suitable for incrementing numeric key-value pairs.
decrement
Decrement the value of a specified key by 1
ops.decrement(key);| Parameter | Type | Description |
|---|---|---|
| key | String | The key to decrement |
Return Value
Returns the value after decrement.
Example
const ops = informat.redis.opsForValue();
const newValue = ops.decrement("key1"); // Decrement the value of key1 by 1Note:
- Parameter key must be a string type. This method is suitable for decrementing numeric key-value pairs.
append
Append a string to the value of a specified key
ops.append(key, value);| Parameter | Type | Description |
|---|---|---|
| key | String | The key to append to |
| value | String | The string to append |
Return Value
Returns the length of the value after appending.
Example
const ops = informat.redis.opsForValue();
const newLength = ops.append("key1", "appendValue"); // Append string appendValue to the value of key1Note:
- Both parameters key and value must be string types. This method is suitable for appending operations on string type key-value pairs.
size
Get the size (in bytes) of the value for a specified key
ops.size(key);| Parameter | Type | Description |
|---|---|---|
| key | String | The key for which to get the size |
Return Value
Returns the size (in bytes) of the value corresponding to the key, or 0 if the key does not exist.
Example
const ops = informat.redis.opsForValue();
const size = ops.size("myKey");Note:
- Parameter key must be a string type. This method is suitable for scenarios where you need to get the size of the stored value.
setBit
Set the value of a specific bit in the value of a specified key
ops.setBit(key, offset, value);| Parameter | Type | Description |
|---|---|---|
| key | String | The key for which to set the bit value |
| offset | Integer | The offset of the bit to set (starting from 0) |
| value | Integer | The bit value to set (0 or 1) |
Return Value
Returns the previous value of the bit before setting.
Example
const ops = informat.redis.opsForValue();
const previousValue = ops.setBit("myKey", 5, 1);Note:
- Parameter key must be a string type, offset must be a numeric type, and value must be 0 or 1. This method is suitable for bit operation scenarios.
getBit
Get the value of a specific bit in the value of a specified key
ops.getBit(key, offset);| Parameter | Type | Description |
|---|---|---|
| key | String | The key for which to get the bit value |
| offset | Integer | The offset of the bit to get (starting from 0) |
Return Value
Returns the value of a specific bit in the value of the specified key (0 or 1).
Example
const ops = informat.redis.opsForValue();
const bitValue = ops.getBit("myKey", 5);Note:
- Parameter key must be a string type, and offset must be a numeric type. This method is suitable for bit operation scenarios.
opsForList
Provides a collection of methods for Redis list operations.
range
Get elements within a specified range from a list.
opsForList.range(key, start, end);| Parameter | Type | Description |
|---|---|---|
| key | String | The list key |
| start | Integer | Start position |
| end | Integer | End position |
Return Value
Returns the list of elements within the specified range.
Example
const ops = informat.redis.opsForList();
const elements = ops.range("myList", 0, -1);Note:
- Parameter
keymust be a string type, andstartandendmust be numeric types.startandendcan be negative numbers, indicating counting from the end of the list.
trim
Trim the list, keeping only elements within the specified range.
opsForList.trim(key, start, end);| Parameter | Type | Description |
|---|---|---|
| key | String | The list key |
| start | Integer | Start position |
| end | Integer | End position |
Return Value
No return value.
Example
const ops = informat.redis.opsForList();
ops.trim("myList", 1, 3);Note:
- Parameter
keymust be a string type, andstartandendmust be numeric types.startandendcan be negative numbers, indicating counting from the end of the list.
size
Get the length of the list.
opsForList.size(key);| Parameter | Type | Description |
|---|---|---|
| key | String | The list key |
Return Value
Returns the length of the list.
Example
const ops = informat.redis.opsForList();
const length = ops.size("myList");Note:
- Parameter
keymust be a string type.
leftPush
Insert an element at the head of the list.
opsForList.leftPush(key, value);| Parameter | Type | Description |
|---|---|---|
| key | String | The list key |
| value | Object | The element to insert |
Return Value
Returns the length of the list after insertion.
Example
const ops = informat.redis.opsForList();
const length = ops.leftPush("myList", "element");Note:
- Parameter
keymust be a string type, andvaluecan be any type.
leftPushAll
Insert multiple elements at the head of the list.
opsForList.leftPushAll(key, values);| Parameter | Type | Description |
|---|---|---|
| key | String | The list key |
| values | Array<Object> | The array of elements to insert |
Return Value
Returns the length of the list after insertion.
Example
const ops = informat.redis.opsForList();
const length = ops.leftPushAll("myList", ["element1", "element2"]);Note:
- Parameter
keymust be a string type,valuesmust be an array type, and array elements can be of any type.
leftPushIfPresent
Insert an element at the head of the list, only if the list exists.
opsForList.leftPushIfPresent(key, value);| Parameter | Type | Description |
|---|---|---|
| key | String | The list key |
| value | Object | The element to insert |
Return Value
Returns the length of the list after insertion, or 0 if the list does not exist.
Example
const ops = informat.redis.opsForList();
const length = ops.leftPushIfPresent("myList", "element");Note:
- Parameter
keymust be a string type,valuecan be of any type.
rightPush
Insert an element at the tail of the list.
opsForList.rightPush(key, value);| Parameter | Type | Description |
|---|---|---|
| key | String | The list key |
| value | Object | The element to insert |
Return Value
Returns the length of the list after insertion.
Example
const ops = informat.redis.opsForList();
const length = ops.rightPush("myList", "element");Note:
- Parameter
keymust be a string type,valuecan be of any type.
rightPushAll
Insert all specified values at the tail of the list stored at key
opsForList.rightPushAll(key, values);| Parameter | Type | Description |
|---|---|---|
| key | String | The key corresponding to the list |
| values | Array<String> | The array of values to insert |
Return Value
Returns the length of the list after the insertion operation.
Example
const opsForList = informat.redis.opsForList();
const length = opsForList.rightPushAll("myList", ["value1", "value2", "value3"]);Note:
- Parameter key must be a string type, values must be an array type, and array elements must be string types.
rightPushIfPresent
Insert the value at the tail of the list only if the list exists
opsForList.rightPushIfPresent(key, value);| Parameter | Type | Description |
|---|---|---|
| key | String | The key corresponding to the list |
| value | String | The value to insert |
Return Value
Returns the length of the list after the insertion operation, or 0 if the list does not exist.
Example
const opsForList = informat.redis.opsForList();
const length = opsForList.rightPushIfPresent("myList", "value1");Note:
- Parameters key and value must be string types.
set
Set the value of the element at index in the list key to value
opsForList.set(key, index, value);| Parameter | Type | Description |
|---|---|---|
| key | String | The key corresponding to the list |
| index | Integer | The index in the list |
| value | String | The value to set |
Return Value
No return value.
Example
const opsForList = informat.redis.opsForList();
opsForList.set("myList", 1, "newValue");Note:
- Parameter key must be a string type, index must be a numeric type, and value must be a string type.
index
Get the value of the element at index in the list key
opsForList.index(key, index);| Parameter | Type | Description |
|---|---|---|
| key | String | The key corresponding to the list |
| index | Integer | The index in the list |
Return Value
Returns the value of the element at index in the list, or null if index is out of range.
Example
const opsForList = informat.redis.opsForList();
const value = opsForList.index("myList", 1);Note:
- Parameter key must be a string type, index must be a numeric type.
indexOf
Get the index of a specified value in the list
opsForList.indexOf(key, value);| Parameter | Type | Description |
|---|---|---|
| key | String | The key corresponding to the list |
| value | String | The value to search for |
Return Value
Returns the index of the specified value in the list, or -1 if the value does not exist.
Example
const opsForList = informat.redis.opsForList();
const index = opsForList.indexOf("myList", "value1");Note:
- Parameters key and value must be string types.
leftPop
Remove and return the head element of the list key
opsForList.leftPop(key);| Parameter | Type | Description |
|---|---|---|
| key | String | The key corresponding to the list |
Return Value
Returns the head element of the list, or null if the list is empty.
Example
const opsForList = informat.redis.opsForList();
const value = opsForList.leftPop("myList");Note:
- Parameter key must be a string type.
rightPop
Remove and return the tail element of the list key
opsForList.rightPop(key);| Parameter | Type | Description |
|---|---|---|
| key | String | The key corresponding to the list |
Return Value
Returns the tail element of the list, or null if the list is empty.
Example
const opsForList = informat.redis.opsForList();
const value = opsForList.rightPop("myList");Note:
- Parameter key must be a string type.
rightPopAndLeftPush
Pop a value from the tail of the list sourceKey and push that value to the head of the list destinationKey
opsForList.rightPopAndLeftPush(sourceKey, destinationKey);| Parameter | Type | Description |
|---|---|---|
| sourceKey | String | The key corresponding to the source list |
| destinationKey | String | The key corresponding to the destination list |
Return Value
Returns the value popped from the source list, or null if the source list is empty.
Example
const opsForList = informat.redis.opsForList();
const value = opsForList.rightPopAndLeftPush("sourceList", "destinationList");Note:
- Parameters sourceKey and destinationKey must be string types.

