Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Cloud Development Connect to the Redis database


May 22, 2021 Mini Program Cloud Development Advanced



Redis is an open source, high-performance key-value-based NoSQL database that supports many types of data structures, such as strings, hashes, lists, collections, sorted sets, and so on, and the operation of the data is atomic. Redis runs in memory, so it has extremely high read and write speeds, while also supporting data persistence and saving in-memory data on disk.

First, Redis and private networks

1, Redis scenario

  • Counter: Because redis operations are atomic, the data count of highly and common users is done by increasing or decreasing atomicity, such as likes, favorites, shares, inventory at the time of purchase, total number of articles, number of comments, etc

  • Leaderboards: Redis supports the data structure of collections and ordered collections and runs in memory, so you can store some leaderboard-like data, such as recent, hottest, most click-through, most active, most reviewed, etc.;

  • Hash table: user fan list, user like list, user favorite list, user watch list, etc.

  • Automatic sorting: store timestamps, automatically sorted according to the latest list of users' concerns over time;

  • Session cache: Use Redis for session caching and store web sessions in Redis.

  • Full-page cache FPC: The results of the rendering on the service side can be cached in Redis;

  • Record user action information: whether the user likes, whether the user is favorite, whether the user shares, etc.

2, create Redis

After creating a private network in the Shanghai Availability Zone (as in the last section), we can purchase the Redis service of Tencent Cloud in the Shanghai Availability Zone, and the network type will find the private network you created and the corresponding subnet.

Cloud Development Connect to the Redis database

In the Tencent cloud web cloud development console, find the cloud functions that need to be configured, such as the function name redis, click on the upper right corner to edit into the configuration interface, in the function configuration interface, modify the network configuration to Redis is located in the same private network subnet.

Cloud Development Connect to the Redis database

Second, the use of ioredis operation redis

In order to connect and operate Redis instances, we need a Redis client that recommends ioredis (similarly, node_redis, tedis, etc.). Use the developer tool to open package.json in the cloud function directory, add the latest version of the ioredis dependency, right-click the cloud function directory to choose to open the input command npm install installation dependency in the terminal:

  1. "dependencies": {
  2. "wx-server-sdk":"latest",
  3. "ioredis":"latest"
  4. }

Then enter the following code in the index.js, which involves redis multiple command lines, where the zadd command is to add an ordered collection to the redis, zscore command returns the corresponding score value of the ordered collection element, zrevrank command returns the ranking of ordered collection elements (Redis has a variety of data structures, different data structure data additions and deletions have corresponding commands, not much is introduced here):

  1. const cloud = require('wx-server-sdk')
  2. cloud.init({
  3. env: cloud.DYNAMIC_CURRENT_ENV,
  4. })
  5. const Redis = require('ioredis')
  6. const redis = await new Redis({
  7. port: 6379,
  8. host: '10.168.0.11',
  9. family: 4, // 4 (IPv4) 或 6 (IPv6)
  10. password: 'cloudbase2020',//redis的密码
  11. db: 0,
  12. })
  13. exports.main = async (event, context) => {
  14. await redis.zadd('Score',145,'user1')
  15. await redis.zadd('Score',134,'user2')
  16. await redis.zadd('Score',117,'user3')
  17. await redis.zadd('Score',147,'user4')
  18. await redis.zadd('Score',125,'user5')
  19. const score = await redis.zscore('Score','user3')
  20. console.log('用户3的分数',score)
  21. const rank = await redis.zrevrank('Score','user5')
  22. console.log('用户5的排名',rank)
  23. return {'用户3的分数':score,'用户5的排名':rank}
  24. }

Redis data types and data storage

There are five types of data commonly used by Redis: strings, hashes, lists, collections, and sorted sets, while JavaScript and cloud development databases have data types such as Strings, Numbers, Boolean, Array, objects, and Objects. W hat should we do when we want to store complex data types such as cloud databases or JavaScript arrays and objects to Redis? Let's just briefly discuss the correlation between Redis and JavaScript and the cloud development database.

Redis commonly uses data types

String Strings

Redis string is binary security, in the transmission of data, to ensure the information security of binary data, that is, not tampered with, deciphered, etc. , will not encode, serialize and so on. S tring storage is structured key:value can be used to store JavaScript strings, numeric types, and often for nodes or Web pages that store HTML. Of course, it can also be used to store pictures, etc., although a key storage limit is 512M, but it is generally not recommended to store the value too long (for example, do not exceed 1024 bytes, otherwise the memory cost and key ratio cost is too high) is not recommended too short (just recommended).

We can also set expiration times for the values of strings, and if the values are integers (Redis does not have a specific integer type, so the key stored values are interpreted as decal 64-bit signed integers when executing atomic operation commands) the values can be similar to those of a cloud development database, such INCR storage which adds 1 to the string store (represents commodity inventory) atoms, and DECRBY storage 30 which reduces the inventory by 30.

We can use dependencies such as ioredis, node-redis in cloud functions, set one or more keys through redis.set key value redis.mset key1 value1 key2 value2 and get the key values that already exist in the redis database through redis.get key or redis.mget key1 key2 the string is structured as follows in redis:

  1. SecretId "AKIDpZ9Wp1pyhFdqrioDF5dmDkMoQ7oVF2shUOE" //用于存储一些key、token等数据
  2. openId "oUL-m5FuRmuVmxvbYOGuXbuEDsn8" //可以存储云开发经常用到的openID
  3. storage 1017 //表示商品库存为1017,执行原子操作命令会被解释为十进制有符号(正负)整数

For string string commands, there are SET, GET, MSET, MGET, INCR, DECR, INCRBY, DECRBY and other commands, you can read the Redis technical documentation.

Hash hash table Hashes

Redis' hash hash table Hashes is a string-type map of field and value, ideal for objects that store JavaScript, and is therefore a data type that is used very frequently. There is no upper limit to the key value pairs that can be stored for each hash in Redis (unless the amount of memory is not allowed).

When we use JavaScript to create an object or to get/pass in data into a cloud development database, we involve the following data styles (the data from the following article), so how do we store that data to Redis?

  1. {
  2. "title": "为什么狗会如此亲近人类?",
  3. "id": 9717547,
  4. "url": "https://daily.zhihu.com/story/9717547",
  5. "image": "https://pic4.zhimg.com/v2-60f220ee6c5bf035d0eaf2dd4736342b.jpg",
  6. "body": "<p>让狗从凶猛的野兽变成忠实的爱宠...</p>"
  7. }

We can use the hmset command HMSET key field value of the HMSET key field value we set the key value post-${id} the properties and values in the object are written as follows:

  1. hmset post-9717547 title "为什么狗会如此亲近人类?" id 9717547 url "https://daily.zhihu.com/story/9717547" image "https://pic4.zhimg.com/v2-60f220ee6c5bf035d0eaf2dd4736342b.jpg" body "<p>让狗从凶猛的野兽变成忠实的爱宠...</p>"

And when we want to get the value of the hash table and to add or censor the data in the hash table, the corresponding action commands are as follows (just list the section, more please review the technical documentation):

  1. //HGETALL以列表形式返回哈希表的字段及字段值
  2. hgetall post-9717547
  3. //HMGET命令返回哈希表中一个或多个给定字段的值,比如获取2个key title和id的值;HGET是只返回一个
  4. hmget post-9717547 title id
  5. hget post-9717547 body
  6. //HMSET同时将多个键值对设置到哈希表中,比如我们同时设置两个键值对,HSET是只设置一个;如果key相同就会覆盖
  7. hmset post-9717547 author "李东bbsky" city "深圳"
  8. hset post-9717547 position "杂役"

There are also hdels that delete hash table fields, hexists that see if fields exist, hintbys that add increments (which can be positive or negative) to the integer atoms of a specified field, hlens that get the number of fields, keys that get all fields, and so on. In summary, with hash tables, we can use them to store simple objects (no nested and nested arrays).

List List

Redis's list type can be used to store multiple ordered strings, the values in the list are repeatable, a bit like An array of JavaScript (or a lot of different oh), the main scenario is the latest user updates, the latest blogs, the latest friends circle updates. In Redis, you can insert push and pop at both ends of the list, get a list of elements for a specified range, specify elements under the index substation, and so on, to act as a stack and queue.

  1. //rpush在列表的尾部(右边)添加一个或多个值,类似于数组方法里的push;lpush在列表的头部(左边)添加一个或多个值,类似于数组方法里的unshift
  2. rpush code "Python" "JavaScript" "Java" "C++" "Golang" "Dart" "C" "C#"
  3. //rpop移除并返回列表最后一个元素,类似于数组方法里的pop;lpop移除并返回列表第一个元素,类似于数组方法里的shift
  4. rpop code
  5. //llen返回列表的长度,有点类似于数组的属性length
  6. llen code
  7. //lindex通过索引获取列表中的元素,有点类似于数组的array[n]获取数组第n+1位的元素
  8. lindex code 3
  9. //lrange返回列表中指定区间内的元素,有点类似于数组方法里的slice
  10. lrange code 2 5
  11. //linsert key before|after pivot value,在列表的元素前或者后插入元素。当指定元素不存在于列表中时,不执行任何操作,如下方式是把SQL插入到Dart前,数组的slice方法可以在指定位置插入元素
  12. linsert code before "Dart" "SQL"
  13. //lset通过索引来设置元素的值,有点类似于数组的array[n]=""
  14. lset code 4 "Go"

Collection Sets

Redis's collection is an out-of-order collection of string types, and the elements in the collection are out of order and unique, with no duplicate data. Redis supports additions and deletions of elements within a collection, as well as intersections, intersections, differential sets, and cross-collection moving elements of multiple collections, making it ideal for common tagging of social systems, e-commerce systems, video apps, etc., such as the people you are most interested in, topics, projects, etc., and websites and apps that recommend different content based on the user's interests.

  1. //sadd 将一个或多个成员元素加入到集合中,已经存在于集合的成员元素将被忽略
  2. sadd cloudbase "云函数" "云数据库" "云存储" "云接入" "云应用" "云调用"
  3. //smembers返回集合中的所有成员
  4. smembers cloudbase
  5. //scard返回集合中元素的数量
  6. scard cloudbase
  7. //srandmember返回集合中一个或多个随机数,spop移除集合中的指定的一个或多个随机元素,移除后会返回移除的元素
  8. srandmember cloudbase 2
  9. spop cloudbase
  10. //sismember判断元素是否在集合中,在则返回1,不在返回0
  11. sismember cloudbase "云调用"

Redis handles commands across collections such as sunion storage and sunionstore sinter sinterstore sdiff sdiffstore smove and so on.

Ordered collection Sorted sets

Redis's ordered collection is the same as a collection of string type elements and elements are not duplicated, but the difference is that the ordered collection has one more sort property score, that is, each storage element consists of two values, one element value, and one sort value. T he elements of an ordered collection are unique, but the score can be repeated. Orderly collection is especially suitable for ranking system, such as like ranking, sales, play most, best score, score ranking and so on.

Now that we've written the article's reading volume and the article's id into Theris's ordered collection, we can easily sort the article by some requirements:

  1. //zadd命令用于将一个或多个元素及分数值加入到有序集中。如果元素已经存在,会更新这个元素的分数值,并通过重新插入这个元素,来保证该元素在正确的位置上。
  2. zadd read:rank 9932 post-323 3211 post-123 1234 post-77 987 post-33 532 post-21
  3. //zrange把元素按分数递增来排序,0为第一位,-1为最后一位,0,-1会把所有元素都排序;而1,3则是取排序的第2、4位;zrevrange则是递减
  4. zrange read:rank 0 -1 withscores
  5. zrange read:rank 1 3 withscores
  6. zrevrange read:rank 1 3 withscores
  7. //zcount显示分数score在 min 和 max 之间的元素的数量
  8. zcount read:rank 1000 3000
  9. //zrank返回有序集合指定元素的排名(排名以0为底),按分数值递增(从小到大)顺序排列;zrevrank是从大到小
  10. zrank read:rank post-323
  11. zrevrank read:rank post-987

As with connecting MySQL, it is recommended that when redis is used in cloud functions, the same Redis instance of addition and deletion and other operations are concentrated in a cloud function, which will reduce the probability of cold start of cloud functions and reduce the number of database connections occupied, and the processing of addition and deletion will be centralized into a cloud function, we can use to the cloud function routing tcb-router, which will be introduced later.