Object built-in object

Object Redis

Redis database client object

Used to create and manage Redis database, creation method:

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

Inheritance

Member attributes

onsuberror

Function, query and set error handling function, call back when there is an error in sub or the network is interrupted, 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 parameters sent

Return result:

  • Value , returns the result returned by the server

set

Associate the string value value with the key, if the key already holds other values, SET overwrites 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 for the key in milliseconds; 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 for the key in milliseconds; 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 for the key in milliseconds; 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 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 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

Use the value parameter to overwrite the string value stored in the given key, starting from the offset 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 start byte offset of the query
  • end : Integer, specify the end byte offset of the query

Return result:

  • Buffer , intercept the substring

strlen

Returns the length of the string value stored in key. When the key is not 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, return 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, negative values ​​can be used, -1 means the last byte, and -2 means the penultimate byte, and so on
  • end : Integer, specify the end byte to be calculated, negative values ​​can be used, -1 means the last byte, and -2 means the penultimate byte, and so on

Return result:

  • Integer , the number of bits set to 1

get

Return the string value associated with the key, if the key does not exist, return the special value Null

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 the key

mget

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

1
NArray Redis.mget(Array keys);

Call parameters:

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

Return result:

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

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

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

Call parameters:

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

Return result:

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

getset

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

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

Call parameters:

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

Return result:

  • Buffer , return the old value of the given key

decr

Subtract the decrement from the value stored in the 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 increment to the value stored in the key

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

For the string value stored in key, set or clear the bit at the specified offset (bit)

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 bit offset for modification
  • value : Integer, specifies the parameter to be set or cleared, can be 0 or 1

Return result:

  • Integer , the bit where the offset was originally stored

getBit

Get the bit at the specified offset for the string value stored in key

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

Call parameters:

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

Return result:

  • Integer , the bit at the specified offset of the string value (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 in key

1
String Redis.type(Buffer key);

Call parameters:

  • key : Buffer , specify the key to be queried

Return result:

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

keys

Find all keys that match the given pattern

1
NArray Redis.keys(String pattern);

Call parameters:

  • pattern : String, specify the query pattern

Return result:

  • NArray , a list of keys matching the given pattern

del

Delete one or more given keys, non-existent keys will be ignored

1
Integer Redis.del(Array keys);

Call parameters:

  • keys : Array, specify the key array to be deleted

Return result:

  • Integer , the number of deleted keys

Delete one or more given keys, non-existent keys will be ignored

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

Call parameters:

  • keys : ..., specify the key list to be deleted

Return result:

  • Integer , the number of deleted keys

expire

Set the time to live 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 for the key in milliseconds

Return result:

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

ttl

Return the remaining survival time of the given key

1
Long Redis.ttl(Buffer key);

Call parameters:

  • key : Buffer , specify the key to be queried

Return result:

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

persist

Remove the lifetime of a given key, and convert this key from "volatile" (with a lifetime key) to "persistent" (a key without lifetime 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 destination 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 destination key to be renamed

Return result:

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

sub

Subscribe to the information of a given channel, and automatically call func when a message occurs. Func contains two parameters, channel and message in turn. The same function on 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 set of channels, and automatically call the corresponding callback function when a message occurs. The same function of the same channel will only be called back once

1
Redis.sub(Object map);

Call parameters:

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

unsub

Unsubscribe all callbacks of 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 unsubscription

Unsubscribe all callbacks for a given channel

1
Redis.unsub(Array channels);

Call parameters:

  • channels : Array, specify the unsubscribed channel array

Unsubscribe the specified callback function of a given set of channels

1
Redis.unsub(Object map);

Call parameters:

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

psub

Subscribe to a group of channel information according to the template, and automatically call func when a message occurs. Func contains three parameters, which are channel, message and pattern in turn. The same function of the same template 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 a message occurs. The same function on the same channel will only be called back once

1
Redis.psub(Object map);

Call parameters:

  • map : Object, specify 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 of channels with a given template

1
Redis.unpsub(String pattern);

Call parameters:

  • pattern : String, specify the channel template to unsubscribe

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 channel template to unsubscribe
  • func : Function, specify the callback function for unsubscription

Unsubscribe all callbacks of a set of channels with a given template

1
Redis.unpsub(Array patterns);

Call parameters:

  • patterns : Array, specify the array of channel templates to be posted

The specified callback function for unsubscribed channels of a set of templates

1
Redis.unpsub(Object map);

Call parameters:

  • map : Object, specify 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 the 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 message to be published

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. The database can only be operated by calling its method

1
RedisHash Redis.getHash(Buffer key);

Call parameters:

  • key : Buffer , specify the key to be obtained

Return result:

  • RedisHash , returns a 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. The database can only be operated by calling its method

1
RedisList Redis.getList(Buffer key);

Call parameters:

  • key : Buffer , specify the key to be obtained

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. The database can only be operated by calling its method

1
RedisSet Redis.getSet(Buffer key);

Call parameters:

  • key : Buffer , specify the key to be obtained

Return result:

  • RedisSet , return a 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. The database can only be operated by calling its method

1
RedisSortedSet Redis.getSortedSet(Buffer key);

Call parameters:

  • key : Buffer , specify the key to be obtained

Return result:

  • RedisSortedSet , returns a 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 serialized value, 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 for the key in milliseconds; 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 re-implemented according to its own characteristics

1
String Redis.toString();

Return result:

  • String , returns the 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