当前位置:网站首页>TCB series learning articles - using redis extension in cloud functions
TCB series learning articles - using redis extension in cloud functions
2022-06-24 16:54:00 【F face】
When should I use it Redis?
Redis The applicable scenarios include but are not limited to :
- Counter : because Redis The operation is atomic , Count the data of high concurrent users by atomic increment or decrement , Like the number of likes 、 Number of collections 、 Number of shares 、 The amount of stock in a rush purchase 、 Total number of articles 、 Number of comments, etc. ;
- Ranking List :Redis Data structures that support sets and ordered sets , And it runs in memory , So you can store some data similar to leaderboard , Like recently 、 Hottest 、 The highest click through rate 、 The most active 、 Articles with the most comments and so on 、 goods 、 The user etc. ;
- Hashtable : User fan list 、 User likes list 、 User collection list 、 User follow list, etc ;
- Automatic sorting : Storage time stamp , Over time , Automatically sort according to the latest dynamic list of users ;
- Session cache : Use Redis Do session caching , take web session Store in Redis in ;
- Full page caching FPC: The server-side rendering results can be cached in Redis in ;
- Record user operation information : Whether the user likes it or not 、 Is the user favorite 、 Whether users share, etc .
install Redis expand
1、 Install the extension
Open Tencent cloud console , Go to the environment details page , Click on the left 「 Expansion management 」, Go to the expansion management details page , And click the Redis expand , Installation expansion .
2、 establish Redis example
If there are no instances in the installation ( I haven't bought yet Redis database , Click new instance ), If there are already instances, you can skip , Go to the next step .
Buy Redis database , Create examples , Set up a private network .
After creating the instance, go back to the extension and select just created ( Or what we have ) Example :
Click Finish to create :
The installation is successful when you see the following extensions :
3、 obtain Redis Information
After the creation, view the development related information ( Here we can see the cloud functions created together ):
Use... In cloud functions Redis
In the cloud function, you can use Redis Client connections and operations Redis example , Recommended redis (opens new window).
1、 Installation dependency
First go to Redis In the cloud function directory of , And then execute the command npm init -y Initialize a configuration file .
And then , perform npm install --save redis To install the corresponding dependencies .
After installation , Cloud function directory will appear package.json file , The content is 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 it in code Redis Database .
Because of the use of cloud development Redis expand , The corresponding configuration will be brought automatically in the system running environment , You can directly use the corresponding environment variables to link Redis database .
'use strict';
const redis = require('redis')
let client = redis.createClient({
host: process.env.HOST,
port: process.env.PORT,
// You need to fill in a real password
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 }
}边栏推荐
- How to pop up an alarm through the national standard gb28181 protocol video platform easygbs for mobile detection / perimeter intrusion detection video recording
- proxy pattern
- Scuffle on China's low code development platform -- make it clear that low code
- 中金证券靠谱吗?是否合法?开股票账户安全吗?
- AI structured intelligent security video monitoring technology, supporting the protective umbrella of the reserve / wild animals
- [tke] nodelocaldnschache is used in IPVS forwarding mode
- [tke] enable CPU static management strategy
- 05. Tencent cloud IOT device side learning -- mqtt protocol client implementation
- Markdown syntax -- Formula
- A tutorial on how the zblog system obtains user related information based on user ID
猜你喜欢

Ps\ai and other design software pondering notes
![[leetcode108] convert an ordered array into a binary search tree (medium order traversal)](/img/e1/0fac59a531040d74fd7531e2840eb5.jpg)
[leetcode108] convert an ordered array into a binary search tree (medium order traversal)

A survey on dynamic neural networks for natural language processing, University of California

MySQL learning -- table structure of SQL test questions

Applet - use of template
![[go] concurrent programming channel](/img/6a/d62678467bbc6dfb6a50ae42bacc96.jpg)
[go] concurrent programming channel

Problems encountered in the work of product manager

Cognition and difference of service number, subscription number, applet and enterprise number (enterprise wechat)

Applet wxss

A survey of training on graphs: taxonomy, methods, and Applications
随机推荐
How Tencent cloud es achieves cross cluster data copy & lt through reindex; Lower & gt;
05. Tencent cloud IOT device side learning -- mqtt protocol client implementation
What is the reason for the worse website SEO ranking?
Virtual machine virtual disk recovery case tutorial
liver failure! My friend made a programming navigation website!
Tencent released "warehouse express" and issued "ID card" for each commodity!
Solution to the problem that kibana's map cannot render longitude and latitude coordinate data
Tencent blue whale container management platform was officially released!
Collect tke logs through daemonset CRD
Video structured intelligent analysis platform easycvr video recording plan function optimization / regularly delete expired videos
Serial of H3CNE experiment column - VLAN configuration experiment, access and trunk
Introduction to koa (III) koa routing
Building a cross public chain platform to solve DAPP development problems
[play with Tencent cloud] & lt; trtc-room> Applet component usage
An error is reported during SVN uploading -svn sqlite[s13]
Factory mode
One article combs multi task learning (mmoe/ple/dupn/essm, etc.)
Automatically refresh CDN cache in real time based on cos log
[tke] whether to configure SNAT when the container accesses services outside the node
Principle analysis of robot hardware in the loop system