Object built-in object

Object Redis

Redis database client object

Used to create and manage the Redis database, the method of creation:

1 2
var db = require("db"); var test = new db.openRedis("redis-server");

Inheritance

Member attribute

onsuberror

Function, query and set the error handling function, callback when the sub has an error or network interruption, when the callback occurs, all sub of this object will be aborted

1
Function Redis.onsuberror;

Member function

command

Redis basic command method

1 2
Value Redis.command(String cmd, ...args);

Call parameters:

  • cmd : String, specify the command sent
  • args : ..., specify the sent parameters

Return result:

  • Value , return the result returned by the server

set

Associate the string value value to the key, if the key already holds other values, SET will overwrite the old value, regardless of the type

1 2 3
Redis.set(Buffer key, Buffer value, Long ttl = 0);

Call parameters:

  • key : Buffer , specify the key to be associated
  • value : Buffer , specify the data to be associated
  • ttl : Long, set the survival time in milliseconds as the key; if ttl is 0, then do not set the survival time

setNX

Set the value of key to value if and only if the key does not exist. If the given key already exists, SETNX does nothing.

1 2 3
Redis.setNX(Buffer key, Buffer value, Long ttl = 0);

Call parameters:

  • key : Buffer , specify the key to be associated
  • value : Buffer , specify the data to be associated
  • ttl : Long, set the survival time in milliseconds as the key; if ttl is 0, then do not set the survival time

setXX

Set the value of key to value, and set the key only when the key already exists.

1 2 3
Redis.setXX(Buffer key, Buffer value, Long ttl = 0);

Call parameters:

  • key : Buffer , specify the key to be associated
  • value : Buffer , specify the data to be associated
  • ttl : Long, set the survival time in milliseconds as the key; if ttl is 0, then do not set the survival time

mset

Set one or more key-value pairs at the same time. If a given key already exists, MSET will overwrite the original old value with the new value

1
Redis.mset(Object kvs);

Call parameters:

  • kvs : Object, specify the key/value object to be set

Set one or more key-value pairs at the same time. If a given key already exists, MSET will overwrite the original old value with the new value

1
Redis.mset(...kvs);

Call parameters:

  • kvs : ..., specify the key/value list to be set

msetNX

Set one or more key-value pairs at the same time if and only if all the given keys do not exist

1
Redis.msetNX(Object kvs);

Call parameters:

  • kvs : Object, specify the key/value object to be set

Set one or more key-value pairs at the same time if and only if all the given keys do not exist

1
Redis.msetNX(...kvs);

Call parameters:

  • kvs : ..., specify the key/value list to be set

append

If the key already exists and is a string, the append command appends the value to the end of the original value of the key. If the key does not exist, append simply sets the given key to value

1 2
Integer Redis.append(Buffer key, Buffer value);

Call parameters:

  • key : Buffer , specify the key to be appended
  • value : Buffer , specify the data to be appended

Return result:

  • Integer , after appending value, the length of the string in key

setRange

Overwrite the string value stored in the given key with the value parameter, starting from the offset

1 2 3
Integer Redis.setRange(Buffer key, Integer offset, Buffer value);

Call parameters:

  • key : Buffer , specify the key to be modified
  • offset : Integer, specify the modified byte offset
  • value : Buffer , specify the data to be overwritten

Return result:

  • Integer , after being modified, the length of the string

getRange

Returns the substring of the string value in key. The interception range of the string is determined by the two offsets of start and end (including start and end)

1 2 3
Buffer Redis.getRange(Buffer key, Integer start, Integer end);

Call parameters:

  • key : Buffer , specify the key to be queried
  • start : Integer, specify the starting byte offset of the query
  • end : Integer, specify the end byte offset of the query

Return result:

  • Buffer , intercepted substring

strlen

Returns the length of the string value stored by key. When the key does not store a string value, an error is returned

1
Integer Redis.strlen(Buffer key);

Call parameters:

  • key : Buffer , specify the key to be calculated

Return result:

  • Integer , the length of the string value. When the key does not exist, it returns 0

bitcount

Count the number of bits set to 1 in a given string

1 2 3
Integer Redis.bitcount(Buffer key, Integer start = 0, Integer end = -1);

Call parameters:

  • key : Buffer , specify the key to be calculated
  • start : Integer, specify the starting byte to be calculated, you can use a negative value, -1 means the last byte, -2 means the penultimate byte, and so on
  • end : Integer, specify the end byte to be calculated, you can use a negative value, -1 means the last byte, -2 means the penultimate byte, and so on

Return result:

  • Integer , the number of bits set to 1

get

Returns the string value associated with key, or returns the special value Null if key does not exist

1
Buffer Redis.get(Buffer key);

Call parameters:

  • key : Buffer , specify the key to be associated

Return result:

  • Buffer , when the key does not exist, return Null, otherwise, return the value of key

mget

Returns all (one or more) values ​​of the given key. If a key does not exist in the given key, the key returns the special value nil.

1
NArray Redis.mget(Array keys);

Call parameters:

  • keys : Array, specifies the key array to be queried

Return result:

  • NArray , a list containing all the values ​​of the given key

Returns all (one or more) values ​​of the given key. If a key does not exist in the given key, the key returns the special value nil.

1
NArray Redis.mget(...keys);

Call parameters:

  • keys : ..., specify the list of keys to be queried

Return result:

  • NArray , a list containing all the values ​​of the given key

getset

Set the value of the given key to value and return the old value of the key

1 2
Buffer Redis.getset(Buffer key, Buffer value);

Call parameters:

  • key : Buffer , specify the key to be modified
  • value : Buffer , specify the modified value

Return result:

  • Buffer , returns the old value of the given key

decr

Decrease the value stored by key

1 2
Long Redis.decr(Buffer key, Long num = 1);

Call parameters:

  • key : Buffer , specify the key to be modified
  • num : Long, specify the value to be subtracted

Return result:

  • Long , after subtracting num, the value of key

incr

Add the value stored in key to the increment

1 2
Long Redis.incr(Buffer key, Long num = 1);

Call parameters:

  • key : Buffer , specify the key to be modified
  • num : Long, specify the value to be added

Return result:

  • Long , after adding num, the value of key

setBit

Set or clear the bit at the specified offset for the string value stored in key

1 2 3
Integer Redis.setBit(Buffer key, Integer offset, Integer value);

Call parameters:

  • key : Buffer , specify the key to be modified
  • offset : Integer, specify the modified bit offset
  • value : Integer, specify the parameter to set or clear, can be 0 or 1

Return result:

  • Integer , specify the bit where the offset was originally stored

getBit

For the string value stored in key, get the bit at the specified offset

1 2
Integer Redis.getBit(Buffer key, Integer offset);

Call parameters:

  • key : Buffer , specify the key to be queried
  • offset : Integer, specifies the bit offset of the query

Return result:

  • Integer , the string value specifies the bit at the offset (bit)

exists

Check if the given key exists

1
Boolean Redis.exists(Buffer key);

Call parameters:

  • key : Buffer , specify the key to be associated

Return result:

  • Boolean , if the key exists, return True, otherwise return False

type

Returns the type of value stored by key

1
String Redis.type(Buffer key);

Call parameters:

  • key : Buffer , specify the key to be queried

Return result:

  • String , returns the type of value stored by key, possible values ​​are none (key does not exist) string (string) list (list) set (collection) zset (ordered set) hash (hash table)

keys

Find all keys matching the given pattern

1
NArray Redis.keys(String pattern);

Call parameters:

  • pattern : String, specify the query mode

Return result:

  • NArray , a list of keys matching the given pattern

del

Delete the given key or keys, non-existent keys will be ignored

1
Integer Redis.del(Array keys);

Call parameters:

  • keys : Array, specifies the key array to delete

Return result:

  • Integer , the number of deleted keys

Delete the given key or keys, non-existent keys will be ignored

1
Integer Redis.del(...keys);

Call parameters:

  • keys : ..., specify the list of keys to delete

Return result:

  • Integer , the number of deleted keys

expire

Set the survival time for a given key, when the key expires, it will be automatically deleted

1 2
Boolean Redis.expire(Buffer key, Long ttl);

Call parameters:

  • key : Buffer , specify the key to be set
  • ttl : Long, set the survival time in milliseconds as the key

Return result:

  • Boolean , if the key exists, return True, otherwise return False

ttl

Returns the remaining time to live for a given key

1
Long Redis.ttl(Buffer key);

Call parameters:

  • key : Buffer , specify the key to be queried

Return result:

  • Long , in milliseconds, returns the remaining time to live of the key. When the key does not exist, returns -2. When the key exists but the remaining time is not set, returns -1

persist

Remove the survival time of a given key, and convert this key from "volatile" (with survival time key) to "persistent" (a key with no survival time and never expires)

1
Boolean Redis.persist(Buffer key);

Call parameters:

  • key : Buffer , specify the key to be set

Return result:

  • Boolean , if the key exists, return True, otherwise return False

rename

Rename the key to newkey, when the key and newkey are the same, or the key does not exist, an error is returned

1 2
Redis.rename(Buffer key, Buffer newkey);

Call parameters:

  • key : Buffer , specify the key to be renamed
  • newkey : Buffer , specify the target key to be renamed

renameNX

If and only if newkey does not exist, rename the key to newkey, and return an error when the key does not exist

1 2
Boolean Redis.renameNX(Buffer key, Buffer newkey);

Call parameters:

  • key : Buffer , specify the key to be renamed
  • newkey : Buffer , specify the target key to be renamed

Return result:

  • Boolean , True if the modification is successful, False if newkey already exists

sub

Subscribe to the information of a given channel, and call func automatically when the message occurs. func contains two parameters, channel and message, and the same function of the same channel will only be called back once.

1 2
Redis.sub(Buffer channel, Function func);

Call parameters:

  • channel : Buffer , specify the name of the subscribed channel
  • func : Function, specify the callback function

Subscribe to the information of a given group of channels, and automatically call the corresponding callback function when the message occurs, the same function of the same channel will only be called back once

1
Redis.sub(Object map);

Call parameters:

  • map : Object, specifies the channel mapping relationship, the object attribute name will be used as the channel name, and the attribute value will be used as the callback function

unsub

Unsubscribe all callbacks for a given channel

1
Redis.unsub(Buffer channel);

Call parameters:

  • channel : Buffer , specify the name of the unsubscribed channel

Unsubscribe the specified callback function of the given channel

1 2
Redis.unsub(Buffer channel, Function func);

Call parameters:

  • channel : Buffer , specify the name of the unsubscribed channel
  • func : Function, specify the callback function for unsubscribe

Unsubscribe all callbacks for a given set of channels

1
Redis.unsub(Array channels);

Call parameters:

  • channels : Array, specifies the unsubscribed channel array

Unsubscribe the specified callback function for a given set of channels

1
Redis.unsub(Object map);

Call parameters:

  • map : Object, specifies the channel mapping relationship, the object attribute name will be used as the channel name, and the attribute value will be used as the callback function

psub

Subscribe to a group of channel information according to the template. When a message occurs, func is automatically called. Func contains three parameters, which are channel, message, and pattern. The same template and the same function will only be called back once.

1 2
Redis.psub(String pattern, Function func);

Call parameters:

  • pattern : String, specify the subscribed channel template
  • func : Function, specify the callback function

Subscribe to the information of a given set of channel templates, and automatically call the corresponding func when the message occurs, the same function of the same channel will only be called back once

1
Redis.psub(Object map);

Call parameters:

  • map : Object, specifies the channel mapping relationship, the object attribute name will be used as the channel template, and the attribute value will be used as the callback function

unpsub

Unsubscribe all callbacks for channels of a given template

1
Redis.unpsub(String pattern);

Call parameters:

  • pattern : String, specify the unsubscribed channel template

The specified callback function for unsubscribing the channel of the given template

1 2
Redis.unpsub(String pattern, Function func);

Call parameters:

  • pattern : String, specify the unsubscribed channel template
  • func : Function, specify the callback function for unsubscribe

Unsubscribe all callbacks for a group of channels with a given template

1
Redis.unpsub(Array patterns);

Call parameters:

  • patterns : Array, array of specified channel templates

The specified callback function for unsubscribing a set of template channels

1
Redis.unpsub(Object map);

Call parameters:

  • map : Object, specifies the channel mapping relationship, the object attribute name will be used as the channel template, and the attribute value will be used as the callback function

pub

Send message to the specified channel channel

1 2
Integer Redis.pub(Buffer channel, Buffer message);

Call parameters:

  • channel : Buffer , specify the published channel
  • message : Buffer , specify the published message

Return result:

  • Integer , the number of clients receiving this message

getHash

Get the Hash object of the specified key. This object is the client that contains the specified key. Only when its method is called, it will operate the database.

1
RedisHash Redis.getHash(Buffer key);

Call parameters:

  • key : Buffer , specify the key to get

Return result:

  • RedisHash , returns the Hash object containing the specified key

getList

Get the List object of the specified key. This object is the client that contains the specified key. Only when its method is called, it will operate the database.

1
RedisList Redis.getList(Buffer key);

Call parameters:

  • key : Buffer , specify the key to get

Return result:

  • RedisList , returns a List object containing the specified key

getSet

Get the Set object of the specified key. This object is the client that contains the specified key. Only when its method is called, it will operate the database.

1
RedisSet Redis.getSet(Buffer key);

Call parameters:

  • key : Buffer , specify the key to get

Return result:

  • RedisSet , returns the Set object containing the specified key

getSortedSet

Get the SortedSet object of the specified key. This object is the client that contains the specified key. Only when its method is called, it will operate the database.

1
RedisSortedSet Redis.getSortedSet(Buffer key);

Call parameters:

  • key : Buffer , specify the key to get

Return result:

  • RedisSortedSet , returns SortedSet object containing the specified key

dump

Serialize the given key and return the serialized value. Use the restore command to deserialize this value into a Redis key

1
Buffer Redis.dump(Buffer key);

Call parameters:

  • key : Buffer , specify the key to be serialized

Return result:

  • Buffer , return the value after serialization, if the key does not exist, then return null

restore

Deserialize the given serialized value and associate it with the given key

1 2 3
Redis.restore(Buffer key, Buffer data, Long ttl = 0);

Call parameters:

  • key : Buffer , specify the key to be deserialized
  • data : Buffer , specify the data to be deserialized
  • ttl : Long, set the survival time in milliseconds as the key; if ttl is 0, then do not set the survival time

close

Close the current database connection or transaction

1
Redis.close();

toString

Returns the string representation of the object, generally returns "[Native Object]", the object can be reimplemented according to its own characteristics

1
String Redis.toString();

Return result:

  • String , returns a string representation of the object

toJSON

Returns the JSON format representation of the object, generally returns a collection of readable attributes defined by the object

1
Value Redis.toJSON(String key = "");

Call parameters:

  • key : String, unused

Return result:

  • Value , returns a value containing JSON serializable