Go--Redis Quick Start Guide

Keywords: Go Redis Back-end Cache


1, Basic introduction, installation and principle of Redis

1. Basic introduction:

① Redis is a NoSQL database (non relational database) 💨 Official website

② Redis (remote dictionary server), which means remote dictionary server, can reach 15w qps on a single machine. It is suitable for caching and persistence.

③ Redis is a high-performance (key/value) distributed memory database. It is a NoSQL database that runs based on memory and supports persistence. It has also become a data structure server

2. Redis installation

Go directly to the official website to download the latest version to a sufficient disk location, and then you can use Redis.

For ease of use, you can first configure the system path for the Redis package, so that cmd can start the Redis service without being in the Redis directory, and then register the Redis service as a windows system service.

3. Principle of redis

1. Schematic diagram

Redis has 16 databases by default. Initially, database No. 0 is used by default, and the numbers are 0... 15. You can use the select keyword to switch the database numbers, but the data of each data does not affect each other.

2. Five data types of redis

String, Hash, List, Set, zset (sorted set).

2, Basic use of Redis

1. Detailed explanation of native Redis command


  • string introduction

    String is the most basic data type in Redis. A key corresponds to a value. String type is binary safe. In addition to ordinary characters, it can also store pictures and other data. The maximum string value in Redis is 512M

  • string-crud

    • set [if it exists, it is equivalent to modifying; if it does not exist, it is equivalent to adding] / get/del

    • mset [set one or more key value pairs at the same time]

    • mget [get multiple key values at the same time]

    • demonstration


  • Basic introduction to Hash

    Redis Hash is a mapping table of field and value of string type. Hash is especially suitable for storing objects.

  • Hash-crud

    • hset/hget/hgetall/hdel

    • hmset/hmget/hlen [count the number of elements in the hash] / hexists key felt [check whether the localized field exists in the hash table key]

    • demonstration


  • List basic introduction

    List is the simplest list of strings, sorted in insertion order. You can add elements at the head or tail.

    List is essentially a linked list, which is ordered, and the values of elements can be repeated.

    If the List data is completely removed, the corresponding key will also disappear, or the List name will be deleted directly

  • List-crud

    • lpush/rpush/lrange/lpop/rpop/del/llen key

      lpush: indicates that one or more elements are added sequentially from the list header

      rpush: indicates that one or more elements are added successively from the end of the list

      lrange: indicates to traverse the list. index starts from 0, - 1 indicates the last element, - 2 indicates the penultimate element, and so on

      lpop: take an element from the left and delete it from the list

      rpop: take an element from the right and delete it from the list

      del: delete

    • demonstration


  • Set basic introduction

    • Redis Set is an unordered Set of string type.
    • The bottom layer is the HashTable data structure. Set stores many string elements, which are unordered and cannot be repeated
  • Set-crud

    • sadd/smembers/sismember/srem [delete specified value]
    • demonstration

More Redis commands

2. Use of go redis

① go connect to Redis

//initRedisClient initializes the redis connection
func initRedisClient() (err error) {
	rds = redis.NewClient(&redis.Options{
		DB:       0,//Select redis database 0-15,
		Addr:     "localhost:6379",//Port number of Redis service listening
		Password: "",
	result, err := rds.Ping().Result()
	if err != nil {
		return err
	return nil

② go operation Redis

//example1 demonstrates the use of set/get of type string
func example1(addData interface{}) (err error) {
	err = rds.Set("score", addData, 0).Err()
	if err != nil {
		return err
	result, err := rds.Get("score").Result()
	if err != nil {
		return err
	fmt.Println("Add data:",result)
	return nil
//Example 2 demonstrates the use of zset (ordered set)
func example2() (err error) {
	//key of zset
	zset:= "scoreRank"
	//ZAdd add multiple data
	scores := []redis.Z{
			Score: 91,
			Member: "Liu Haibin",
			Score: 99,
			Member: "Bao Wenjie",
			Score: 90,
			Member: "Tie Lin Wang",
			Score: 97,
			Member: "Mou Guozhu",
	result1, err := rds.ZAdd(zset, scores...).Result()
	if err != nil {
		return err
	fmt.Println("zadd: ",result1)
	//Modify the score and add it based on the original score
	result2, err := rds.ZIncrBy(zset, 1, "Bao Wenjie").Result()
	if err != nil {
		return err
	fmt.Println("Successfully modified:",result2)
	//Take the three with the highest scores
	result3, err := rds.ZRevRangeWithScores(zset,0,2).Result()
	if err != nil {
		return err
	fmt.Println("The three with the highest scores:",result3)
	//Take the three lowest scores
	result4, err := rds.ZRangeWithScores(zset,0,2).Result()
	if err != nil {
		return err
	fmt.Println("The three lowest scores:",result4)
	//Take data of 95 ~ 100 points
	zRangeBy := redis.ZRangeBy{
		Min: "95",
		Max: "100",
	result5, err := rds.ZRangeByScoreWithScores(zset, zRangeBy).Result()
	if err != nil {
		return err
	fmt.Println("95~100:: ",result5)
	return nil

Posted by bltesar on Fri, 03 Dec 2021 15:16:46 -0800