Golang operates redis

I am using the golang client https://github.com/go-redis/redis, so the installation method is as follows:

go get github.com/go-redis/redis/v8

Then import this package in the code:


"github.com/go-redis/redis/v8"

A redis client can be created through the redis.NewClient function, which receives a redis.Options object parameter, through which we can configure redis-related properties, such as redis server address, database name, database password, etc.
Here is an example of a connection:

Golang operates redis
func RedisClient1() *redis.Client{
    var ctx = context.Background()
    rdb := redis.NewClient(&redis.Options{
        Addr:     "localhost:6379",
        Password: "", // no password set
        DB:       1,  // use default DB
    })
    _, err := rdb.Ping(ctx).Result()
    if err != nil {
        fmt.Println(err)
    }
    return rdb
}

String operations of redis are:

set(key, value): assign the value value to the string named key in the database
get(key): Returns the value of the string named key in the database
getset(key, value): assign the last value to the string named key
mget(key1, key2,..., key N): Returns the value of multiple strings in the library
setnx(key, value): add string, name is key, value is value
setex(key, time, value): add string to the library, set the expiration time time
mset(key N, value N): batch set the value of multiple strings
msetnx(key N, value N): if all strings named key i do not exist
incr(key): increment the string named key by 1
incrby(key, integer): the string named key adds integer
decr(key): subtract 1 from the string named key
decrby(key, integer): the string named key reduces the integer
append(key, value): append value to the value of the string named key
substr(key, start, end): Returns the substring of the value of the string named key

In go-redis, we can directly find the corresponding operation method, directly on the code:

// String operations
func stringOperation(client *redis.Client) {
    // The third parameter is the expiration time, if it is 0, it means there is no expiration time.
    err := client.Set("name", "xys", 0).Err()
    if err != nil {
        panic(err)
    }
    val, err := client.Get("name").Result()
    if err != nil {
        panic(err)
    }
    fmt.Println("name", val)

    // Set the expiration time here.
    err = client.Set("age", "20", 1 * time.Second).Err()
    if err != nil {
        panic(err)
    }
    client.Incr("age") // auto increment
    client.Incr("age") // auto increment
    client.Decr("age") // Decrement
    val, err = client.Get("age").Result()
    if err != nil {
        panic(err)
    }
    fmt.Println("age", val) // the value of age is 21
    // Because the expiration time of key "age" is one second, the key will be automatically deleted after one second.
    time.Sleep(1 * time.Second)
    val, err = client.Get("age").Result()
    if err != nil {
        // Because the key "age" has expired, there will be a redis: nil error.
        fmt.Printf("error: %v\n", err)
    }
    fmt.Println("age", val)
}

Leave a Reply

Your email address will not be published. Required fields are marked *

en_USEnglish