ObjectRedis
Redis database client object
Used to create and manage Redis database, creation method:
1
2var db = require("db");
var test = new db.openRedis("redis-server");
inheritance relationship
member properties
onsuberror
Function, query and set the error handling function. When the sub has an error or the network is interrupted, it will be called back. When the callback occurs, all subs of this object will be terminated.
1Function Redis.onsuberror;
member function
command
redis basic command method
1
2Value Redis.command(String cmd,
...args);
Call parameters:
- cmd: String, specifies the command to send
- args: ..., specify the parameters to be sent
Return results:
- Value, returns the result returned by the server
set
Associate the string value value to the key. If the key already holds another value, SET overwrites the old value, regardless of the type.
1
2
3Redis.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 the survival time is not set
setNX
Set the value of key to value if and only if key does not exist. If the given key already exists, SETNX takes no action.
1
2
3Redis.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 the survival time is not set
setXX
Set the value of key to value, and set the key only if the key already exists.
1
2
3Redis.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 the survival time is not set
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.
1Redis.mset(Object kvs);
Call parameters:
- kvs: Object, specifies 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.
1Redis.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 given keys do not exist
1Redis.msetNX(Object kvs);
Call parameters:
- kvs: Object, specifies the key/value object to be set
Set one or more key-value pairs at the same time if and only if all given keys do not exist
1Redis.msetNX(...kvs);
Call parameters:
- kvs: ..., specify the key/value list to be set
append
If key already exists and is a string, the append command appends value to the end of key's original value. If key does not exist, append simply sets the given key to value.
1
2Integer Redis.append(Buffer key,
Buffer value);
Call parameters:
Return results:
- 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 offset offset
1
2
3Integer Redis.setRange(Buffer key,
Integer offset,
Buffer value);
Call parameters:
- key:Buffer, specify the key to be modified
- offset: Integer, specifies the modified byte offset
- value:Buffer, specify the data to be overwritten
Return results:
- Integer, the length of the string after being modified
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
3Buffer Redis.getRange(Buffer key,
Integer start,
Integer end);
Call parameters:
- key:Buffer, specify the key to be queried
- start: Integer, specifies the starting byte offset of the query
- end: Integer, specifies the end byte offset of the query
Return results:
- Buffer, intercept the substring obtained
strlen
Returns the length of the string value stored in key. When key stores a value other than a string value, an error is returned.
1Integer Redis.strlen(Buffer key);
Call parameters:
- key:Buffer, specify the key to be calculated
Return results:
- Integer, the length of the string value. When key does not exist, return 0
bitcount
Counts the number of bits set to 1 in the given string
1
2
3Integer Redis.bitcount(Buffer key,
Integer start = 0,
Integer end = -1);
Call parameters:
- key:Buffer, specify the key to be calculated
- start: Integer, specifies the starting byte to be calculated, you can use negative values, -1 represents the last byte, and -2 represents the second to last byte, and so on.
- end: Integer, specifies the end byte to be calculated, you can use negative values, -1 represents the last byte, and -2 represents the penultimate byte, and so on.
Return results:
- Integer, the number of bits set to 1
get
Returns the string value associated with key. If key does not exist, returns the special value Null.
1Buffer Redis.get(Buffer key);
Call parameters:
- key:Buffer, specify the key to be associated
Return results:
- Buffer, when key does not exist, return Null, otherwise, return the value of key
mget
Returns all (one or more) values for the given key. If a key does not exist in the given key, then this key returns the special value nil.
1NArray Redis.mget(Array keys);
Call parameters:
- keys: Array, specifies the key array to be queried
Return results:
- NArray, a list containing all values for a given key
Returns all (one or more) values for the given key. If a key does not exist in the given key, then this key returns the special value nil.
1NArray Redis.mget(...keys);
Call parameters:
- keys: ..., specify the key list to be queried
Return results:
- NArray, a list containing all values for a given key
getset
Sets the value of the given key to value and returns the old value of the key
1
2Buffer Redis.getset(Buffer key,
Buffer value);
Call parameters:
Return results:
- Buffer, returns the old value of the given key
decr
Subtract the decrement from the value stored in key
1
2Long Redis.decr(Buffer key,
Long num = 1);
Call parameters:
- key:Buffer, specify the key to be modified
- num: Long, specifies the value to be subtracted
Return results:
- Long, after subtracting num, the value of key
incr
Add the increment to the value stored in key
1
2Long Redis.incr(Buffer key,
Long num = 1);
Call parameters:
- key:Buffer, specify the key to be modified
- num: Long, specifies the value to be added
Return results:
- 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.
1
2
3Integer Redis.setBit(Buffer key,
Integer offset,
Integer value);
Call parameters:
- key:Buffer, specify the key to be modified
- offset: Integer, specifies the modified bit offset
- value: Integer, specifies the parameter to be set or cleared, which can be 0 or 1
Return results:
- Integer, specifies the bit where the offset was originally stored
getBit
For the string value stored in key, obtain the bit at the specified offset
1
2Integer 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 results:
- Integer, the string value specifies the bit at the offset
exists
Check if the given key exists
1Boolean Redis.exists(Buffer key);
Call parameters:
- key:Buffer, specify the key to be associated
Return results:
- Boolean, if the key exists, return True, otherwise return False
type
Returns the type of value stored in key
1String Redis.type(Buffer key);
Call parameters:
- key:Buffer, specify the key to be queried
Return results:
- String, returns the type of value stored in key. Possible values are none (key does not exist) string (string) list (list) set (set) zset (ordered set)hash(hash table)
keys
Find all keys matching the given pattern
1NArray Redis.keys(String pattern);
Call parameters:
- pattern: String, specify query mode
Return results:
- NArray, a list of keys matching the given pattern
del
Delete one or more given keys. Non-existing keys will be ignored.
1Integer Redis.del(Array keys);
Call parameters:
- keys: Array, specifies the key array to be deleted
Return results:
- Integer, the number of deleted keys
Delete one or more given keys. Non-existing keys will be ignored.
1Integer Redis.del(...keys);
Call parameters:
- keys: ..., specify the key list to be deleted
Return results:
- Integer, the number of deleted keys
expire
Set the lifetime for a given key. When the key expires, it will be automatically deleted.
1
2Boolean 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 results:
- Boolean, if the key exists, return True, otherwise return False
ttl
Returns the remaining survival time of the given key
1Long Redis.ttl(Buffer key);
Call parameters:
- key:Buffer, specify the key to be queried
Return results:
- Long, returns the remaining survival time of the key in milliseconds. 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" (key with lifetime) to "persistent" (a key without lifetime and never expires)
1Boolean Redis.persist(Buffer key);
Call parameters:
- key:Buffer, specify the key to be set
Return results:
- Boolean, if the key exists, return True, otherwise return False
rename
Rename key to newkey. When key is the same as newkey, or key does not exist, an error is returned.
1
2Redis.rename(Buffer key,
Buffer newkey);
Call parameters:
renameNX
If and only if newkey does not exist, rename the key to newkey. When the key does not exist, return an error.
1
2Boolean Redis.renameNX(Buffer key,
Buffer newkey);
Call parameters:
Return results:
- Boolean, when the modification is successful, return True, if newkey already exists, return 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 order. The same function on the same channel will only be called back once.
1
2Redis.sub(Buffer channel,
Function func);
Call parameters:
- channel:Buffer, specify the subscribed channel name
- 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 on the same channel will only be called once.
1Redis.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 value of the attribute will be used as the callback function
unsub
Unsubscribe all callbacks for the given channel
1Redis.unsub(Buffer channel);
Call parameters:
- channel:Buffer, specify the channel name to unsubscribe from
The specified callback function to unsubscribe from the given channel
1
2Redis.unsub(Buffer channel,
Function func);
Call parameters:
- channel:Buffer, specify the channel name to unsubscribe from
- func: Function, specifies the callback function for unsubscription
Unsubscribe all callbacks for a given set of channels
1Redis.unsub(Array channels);
Call parameters:
- channels: Array, specifies the channel array to unsubscribe from
Specified callback function to unsubscribe from a given set of channels
1Redis.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 value of the attribute will be used as 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, channel, message and pattern in order. The same function in the same template will only be called back once.
1
2Redis.psub(String pattern,
Function func);
Call parameters:
- pattern: String, specifies 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.
1Redis.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 from the channel of the given template
1Redis.unpsub(String pattern);
Call parameters:
- pattern: String, specify the unsubscribe channel template
Specified callback function to unsubscribe from the channel of the given template
1
2Redis.unpsub(String pattern,
Function func);
Call parameters:
- pattern: String, specify the unsubscribe channel template
- func: Function, specifies the callback function for unsubscription
Unsubscribe all callbacks for a set of channels with a given template
1Redis.unpsub(Array patterns);
Call parameters:
- patterns: Array, specifies the published channel template array
Specified callback function to unsubscribe from a group of template channels
1Redis.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 information message to the specified channel channel
1
2Integer Redis.pub(Buffer channel,
Buffer message);
Call parameters:
Return results:
- Integer, the number of clients receiving this message
getHash
Get the Hash object of the specified key. This object is the client containing the specified key. Only by calling its method will the database be operated.
1RedisHash Redis.getHash(Buffer key);
Call parameters:
- key:Buffer, specify the key to be obtained
Return results:
- RedisHash, returns a Hash object containing the specified key
getList
Get the List object of the specified key. This object is the client containing the specified key. Only by calling its method will the database be operated.
1RedisList Redis.getList(Buffer key);
Call parameters:
- key:Buffer, specify the key to be obtained
Return results:
- RedisList, returns a List object containing the specified key
getSet
Get the Set object of the specified key. This object is the client containing the specified key. Only by calling its method will the database be operated.
1RedisSet Redis.getSet(Buffer key);
Call parameters:
- key:Buffer, specify the key to be obtained
Return results:
- RedisSet, returns a Set object containing the specified key
getSortedSet
Get the SortedSet object of the specified key. This object is the client containing the specified key. Only by calling its method will the database be operated.
1RedisSortedSet Redis.getSortedSet(Buffer key);
Call parameters:
- key:Buffer, specify the key to be obtained
Return results:
- RedisSortedSet, returns the SortedSet object containing the specified key
dump
Serializes the given key and returns the serialized value. Use the restore command to deserialize this value into a Redis key.
1Buffer Redis.dump(Buffer key);
Call parameters:
- key:Buffer, specify the key to be serialized
Return results:
- Buffer, returns the value after serialization, if the key does not exist, then returns null
restore
Deserialize the given serialized value and associate it with the given key
1
2
3Redis.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 the survival time is not set
close
Close the current database connection or transaction
1Redis.close();
toString
Returns the string representation of the object. Generally, "[Native Object]" is returned. The object can be re-implemented according to its own characteristics.
1String Redis.toString();
Return results:
- String, returns the string representation of the object
toJSON
Returns a JSON format representation of the object, generally returning a collection of readable properties defined by the object.
1Value Redis.toJSON(String key = "");
Call parameters:
- key: String, not used
Return results:
- Value, returns a value containing JSON serializable