tencent cloud

Client Overview
Last updated: 2025-03-11 20:05:00
Client Overview
Last updated: 2025-03-11 20:05:00
Through the new redis.Client method, you can create a Client instance. The parameter for this method is the address of the target redis.

Constructor

new Client(url: string): Client

Parameters

Parameter
Type
Description
url
string
The address of the target redis server, for example, redis://<user>:<password>@<host>:<port>/<db_number>

Methodology

Methodology
Return Type
Description
get(key)
string
Obtain the value of the specified key.
string
Set the value of the specified key.
number
Delete existing keys.
number
Insert one or more values at the head of the list.
number
Insert one or more values at the tail of the list.
lPop(key)
string
Remove and obtain the first element of the list.
rPop(key)
string
Remove and obtain the last element of the list.
string[]
Obtain elements within the specified range from the list.
string
Obtain elements from the list by index.
lLen(key)
number
Obtain the length of the list.
string
Set the value of elements in the list by index.
number
Remove elements from the list.
number
Set the fields and values in the hash table key.
string
Obtain the value stored in the specified field of the hash table.
number
Delete one or more hash table fields.
hLen(key)
number
Obtain the number of fields in the hash table.
number
Add one or more members to the set.
number
Remove one or more members from the set.
boolean
Determine whether the member element is a member of the set key.
string[]
Return all members in the set.
string
Randomly return an element in the set.
sPop(key)
string
Randomly remove and return an element in the set.

Example

Establish a connection with Redis and perform operations.
import redis from "pts/redis";

let client = new redis.Client("redis://:<password>@<host>:6379/0");

export default function main() {
let resp = client.set("key", "hello, world", 0);
console.log(`redis set ${resp}`); // OK
let val = client.get("key");
console.log(`redis get ${val}`); // hello, world
let cnt = client.del("key");
console.log(`redis del ${cnt}`); // 1

let lpushResp = client.lPush("list", "foo");
console.log(`redis lpush ${lpushResp}`); // OK
let lpopResp = client.lPop("list");
console.log(`redis lpop ${lpopResp}`); // foo
let listLen = client.lLen("list");
console.log(`redis llen ${listLen}`); // 0

let hashSetResp = client.hSet("hash", "k", 1); // [k1, v1, k2, v2, ...]
console.log(`redis hset ${hashSetResp}`); // 1
let hashGetResp = client.hGet("hash", "k");
console.log(`redis hget ${hashGetResp}`); // 1
let hashDelResp = client.hDel("hash", "k");
console.log(`redis hdel ${hashDelResp}`); // 1

let setAddResp = client.sAdd("set", "hello");
console.log(`redis sadd ${setAddResp}`); // 1
let setPopResp = client.sPop("set");
console.log(`redis spop ${setPopResp}`); // hello
}

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback