When should you use Redis?
The applicable scenarios of Redis include but are not limited to:
- Counter : Because Redis operations are atomic, the number of concurrent users' data is counted by atomic increment or decrement, such as the number of likes, favorites, shares, inventory at the time of purchase, total article articles, and comments Wait;
- Ranking List : Redis supports the data structure of collections and ordered collections, and runs in memory, so it can store some data similar to rankings, such as recent, hottest, highest click rate, highest activity, most comments, etc. Articles, products, users, etc.;
- hash table : user fan list, user likes list, user favorite list, user follow list, etc.;
- Automatic sorting: store timestamps, and automatically sort according to the latest dynamic list of users concerned by users as time changes;
- Session Cache : Use Redis for session cache, store the web session in Redis;
- full-page cache FPC : the server-side rendering result can be cached in Redis;
- records user operation information : whether the user likes, whether the user is favorite, whether the user is sharing, etc.
Install Redis extension
1. Install the extension
Open Tencent Cloud console , enter the environment details page, click "Extended Application" on the left, enter the expansion capability details page, and click Redis expansion to install the expansion.
2. Create a Redis instance
If there is no instance in the installation (that is, you have not purchased the Redis database, click New Instance), if there is already an instance, you can skip and go to the next step.
Purchase a Redis database, create an instance, and equip a private network.
After creating the instance, go back to the extension and select the just created (or existing) instance:
Click to finish creating:
The installation is successful when you see the following extensions:
3. Get Redis information
After creating it, check the expansion related information (here we can see the cloud function created together):
Use Redis in cloud functions
The Redis instance can be connected and operated through the Redis client in the cloud function, and it is recommended.
1. Installation dependencies
First enter the Redis cloud function directory, and then execute the command npm init -y
initialize a configuration file.
Subsequently, execute npm install --save redis
to install the corresponding dependencies.
After the installation is complete, the package.json file will appear in the cloud function directory, with the content similar to the following:
{
"name": "redis",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"redis": "^3.0.2"
}
}
2. Call Redis
Next, you can call the Redis database in the code.
Since the Redis extension developed by the cloud is used, the corresponding configuration will be automatically carried in the system operating environment, and you can directly use the corresponding environment variables to link to the Redis database.
'use strict';
const redis = require('redis')
let client = redis.createClient({
host: process.env.HOST,
port: process.env.PORT,
// 需要填写真实的密码
password: 'xxx'
})
exports.main = async (event, context, callback) => {
let res = await new Promise((resolve, reject) => {
client.get('test', function (err, reply) {
if (err) {
resolve({
err
})
}
resolve({
data: reply.toString()
})
})
})
return { res }
}
product description
Cloud Development (Tencent CloudBase, TCB) is a cloud-native integrated development environment and tool platform provided by Tencent Cloud. It provides developers with highly available, automatically and elastically scalable back-end cloud services, including serverless capabilities such as computing, storage, and hosting. , Can be used for cloud integration to develop a variety of end applications (small programs, official accounts, web applications, Flutter clients, etc.) to help developers build and manage back-end services and cloud resources in a unified manner, avoiding cumbersome servers in the application development process With construction and operation and maintenance, developers can focus on the realization of business logic, with lower development thresholds and higher efficiency.
Open cloud development: https://console.cloud.tencent.com/tcb?tdl_anchor=techsite
Product documentation: https://cloud.tencent.com/product/tcb?from=12763
Technical document: https://cloudbase.net?from=10004
Technical exchange group, latest information, follow WeChat public account [Developed by Tencent Cloud Cloud]
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。