Skip to content

informat.redis Redis ​

Overview ​

Use the informat.redis object for Redis-related operations

opsForValue ​

Get value operation object

javascript
const ops = informat.redis.opsForValue();

Return Value

Returns a value operation object.

Example

javascript
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

javascript
const ops = informat.redis.opsForList();

Return Value

Returns a list operation object.

Example

javascript
const ops = informat.redis.opsForList();

opsForGeo ​

Get geo operation object

javascript
const ops = informat.redis.opsForGeo();

Return Value

Returns a geo operation object.

Example

javascript
const ops = informat.redis.opsForGeo();

redisTemplate ​

Get Redis operation template

javascript
const redisTemplate = informat.redis.redisTemplate();

Return Value

Returns a Redis operation template object.

Example

javascript
const redisTemplate = informat.redis.redisTemplate();

delete ​

Delete the value of a specified key

javascript
informat.redis.delete(key);
ParameterTypeDescription
keyStringThe key to delete

Return Value

Returns the result of the delete operation, true for success, false for failure.

Example

javascript
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

javascript
informat.redis.deleteAll(keys);
ParameterTypeDescription
keysArray<String>The group of keys to delete

Return Value

Returns the number of keys deleted, type is Number

Example

javascript
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

javascript
ops.set(key, value, timeout, redisTimeoutUnit);
ParameterTypeDescription
keyStringThe key to set
valueStringThe value to set
timeoutIntegerOptional, valid time
redisTimeoutUnitRedisTimeoutUnitOptional, time unit

RedisTimeoutUnit Options

  • NANOSECONDS Nanoseconds
  • MICROSECONDS Microseconds
  • MILLISECONDS Milliseconds
  • SECONDS Seconds
  • MINUTES Minutes
  • HOURS Hours
  • DAYS Days

Return Value

No return value.

Example

javascript
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

javascript
ops.setIfAbsent(key, value, timeout, redisTimeoutUnit);
ParameterTypeDescription
keyStringThe key to set
valueStringThe value to set
timeoutIntegerOptional, valid time
redisTimeoutUnitRedisTimeoutUnitOptional, time unit

Return Value

Returns a boolean indicating whether the value was set successfully.

Example

javascript
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

javascript
ops.setIfPresent(key, value, timeout, redisTimeoutUnit);
ParameterTypeDescription
keyStringThe key to set
valueStringThe value to set
timeoutIntegerOptional, timeout
redisTimeoutUnitRedisTimeoutUnitOptional, time unit

Return Value

Returns a boolean indicating whether the value was set successfully.

Example

javascript
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

javascript
ops.multiSet(keyValuePairs);
ParameterTypeDescription
keyValuePairsObjectThe key-value pairs object to set

Return Value

No return value.

Example

javascript
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

javascript
ops.multiSetIfAbsent(keyValuePairs);
ParameterTypeDescription
keyValuePairsObjectThe key-value pairs object to set

Return Value

Returns a boolean indicating whether the values were set successfully.

Example

javascript
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

javascript
ops.setRange(key, value, offset);
ParameterTypeDescription
keyStringThe key to set
valueStringThe substring to set
offsetIntegerThe starting position of the substring

Return Value

No return value.

Example

javascript
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

javascript
ops.get(key);
ParameterTypeDescription
keyStringThe key to get

Return Value

Returns the value corresponding to the key, or null if the key does not exist.

Example

javascript
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

javascript
ops.getAndExpire(key, timeout, redisTimeoutUnit);
ParameterTypeDescription
keyStringThe key to get
timeoutIntegerExpiration time
redisTimeoutUnitRedisTimeoutUnitTime unit

Return Value

Returns the value corresponding to the key, or null if the key does not exist.

Example

javascript
const ops = informat.redis.opsForValue();
const value = ops.getAndExpire("key1", 60, "SECONDS"); // Get the value of key1 and set it to expire in 60 seconds

Note:

  • 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

javascript
ops.getAndPersist(key);
ParameterTypeDescription
keyStringThe key to get

Return Value

Returns the value corresponding to the key, or null if the key does not exist.

Example

javascript
const ops = informat.redis.opsForValue();
const value = ops.getAndPersist("key1"); // Get the value of key1 and remove its expiration time

Note:

  • 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

javascript
ops.getAndSet(key, newValue);
ParameterTypeDescription
keyStringThe key to get
newValueStringThe new value

Return Value

Returns the previous value corresponding to the key, or null if the key does not exist.

Example

javascript
const ops = informat.redis.opsForValue();
const oldValue = ops.getAndSet("key1", "newValue"); // Get the old value of key1 and set the new value to newValue

Note:

  • 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

javascript
ops.multiGet(keys);
ParameterTypeDescription
keysArray<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

javascript
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

javascript
ops.increment(key);
ParameterTypeDescription
keyStringThe key to increment

Return Value

Returns the value after increment.

Example

javascript
const ops = informat.redis.opsForValue();
const newValue = ops.increment("key1"); // Increment the value of key1 by 1

Note:

  • 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

javascript
ops.decrement(key);
ParameterTypeDescription
keyStringThe key to decrement

Return Value

Returns the value after decrement.

Example

javascript
const ops = informat.redis.opsForValue();
const newValue = ops.decrement("key1"); // Decrement the value of key1 by 1

Note:

  • 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

javascript
ops.append(key, value);
ParameterTypeDescription
keyStringThe key to append to
valueStringThe string to append

Return Value

Returns the length of the value after appending.

Example

javascript
const ops = informat.redis.opsForValue();
const newLength = ops.append("key1", "appendValue"); // Append string appendValue to the value of key1

Note:

  • 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

javascript
ops.size(key);
ParameterTypeDescription
keyStringThe 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

javascript
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

javascript
ops.setBit(key, offset, value);
ParameterTypeDescription
keyStringThe key for which to set the bit value
offsetIntegerThe offset of the bit to set (starting from 0)
valueIntegerThe bit value to set (0 or 1)

Return Value

Returns the previous value of the bit before setting.

Example

javascript
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

javascript
ops.getBit(key, offset);
ParameterTypeDescription
keyStringThe key for which to get the bit value
offsetIntegerThe 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

javascript
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.

javascript
opsForList.range(key, start, end);
ParameterTypeDescription
keyStringThe list key
startIntegerStart position
endIntegerEnd position

Return Value

Returns the list of elements within the specified range.

Example

javascript
const ops = informat.redis.opsForList();
const elements = ops.range("myList", 0, -1);

Note:

  • Parameter key must be a string type, and start and end must be numeric types. start and end can be negative numbers, indicating counting from the end of the list.

trim ​

Trim the list, keeping only elements within the specified range.

javascript
opsForList.trim(key, start, end);
ParameterTypeDescription
keyStringThe list key
startIntegerStart position
endIntegerEnd position

Return Value

No return value.

Example

javascript
const ops = informat.redis.opsForList();
ops.trim("myList", 1, 3);

Note:

  • Parameter key must be a string type, and start and end must be numeric types. start and end can be negative numbers, indicating counting from the end of the list.

size ​

Get the length of the list.

javascript
opsForList.size(key);
ParameterTypeDescription
keyStringThe list key

Return Value

Returns the length of the list.

Example

javascript
const ops = informat.redis.opsForList();
const length = ops.size("myList");

Note:

  • Parameter key must be a string type.

leftPush ​

Insert an element at the head of the list.

javascript
opsForList.leftPush(key, value);
ParameterTypeDescription
keyStringThe list key
valueObjectThe element to insert

Return Value

Returns the length of the list after insertion.

Example

javascript
const ops = informat.redis.opsForList();
const length = ops.leftPush("myList", "element");

Note:

  • Parameter key must be a string type, and value can be any type.

leftPushAll ​

Insert multiple elements at the head of the list.

javascript
opsForList.leftPushAll(key, values);
ParameterTypeDescription
keyStringThe list key
valuesArray<Object>The array of elements to insert

Return Value

Returns the length of the list after insertion.

Example

javascript
const ops = informat.redis.opsForList();
const length = ops.leftPushAll("myList", ["element1", "element2"]);

Note:

  • Parameter key must be a string type, values must 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.

javascript
opsForList.leftPushIfPresent(key, value);
ParameterTypeDescription
keyStringThe list key
valueObjectThe element to insert

Return Value

Returns the length of the list after insertion, or 0 if the list does not exist.

Example

javascript
const ops = informat.redis.opsForList();
const length = ops.leftPushIfPresent("myList", "element");

Note:

  • Parameter key must be a string type, value can be of any type.

rightPush ​

Insert an element at the tail of the list.

javascript
opsForList.rightPush(key, value);
ParameterTypeDescription
keyStringThe list key
valueObjectThe element to insert

Return Value

Returns the length of the list after insertion.

Example

javascript
const ops = informat.redis.opsForList();
const length = ops.rightPush("myList", "element");

Note:

  • Parameter key must be a string type, value can be of any type.

rightPushAll ​

Insert all specified values at the tail of the list stored at key

javascript
opsForList.rightPushAll(key, values);
ParameterTypeDescription
keyStringThe key corresponding to the list
valuesArray<String>The array of values to insert

Return Value

Returns the length of the list after the insertion operation.

Example

javascript
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

javascript
opsForList.rightPushIfPresent(key, value);
ParameterTypeDescription
keyStringThe key corresponding to the list
valueStringThe value to insert

Return Value

Returns the length of the list after the insertion operation, or 0 if the list does not exist.

Example

javascript
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

javascript
opsForList.set(key, index, value);
ParameterTypeDescription
keyStringThe key corresponding to the list
indexIntegerThe index in the list
valueStringThe value to set

Return Value

No return value.

Example

javascript
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

javascript
opsForList.index(key, index);
ParameterTypeDescription
keyStringThe key corresponding to the list
indexIntegerThe 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

javascript
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

javascript
opsForList.indexOf(key, value);
ParameterTypeDescription
keyStringThe key corresponding to the list
valueStringThe 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

javascript
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

javascript
opsForList.leftPop(key);
ParameterTypeDescription
keyStringThe key corresponding to the list

Return Value

Returns the head element of the list, or null if the list is empty.

Example

javascript
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

javascript
opsForList.rightPop(key);
ParameterTypeDescription
keyStringThe key corresponding to the list

Return Value

Returns the tail element of the list, or null if the list is empty.

Example

javascript
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

javascript
opsForList.rightPopAndLeftPush(sourceKey, destinationKey);
ParameterTypeDescription
sourceKeyStringThe key corresponding to the source list
destinationKeyStringThe 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

javascript
const opsForList = informat.redis.opsForList();
const value = opsForList.rightPopAndLeftPush("sourceList", "destinationList");

Note:

  • Parameters sourceKey and destinationKey must be string types.