Home > Backend Development > Golang > How to connect redis to golang

How to connect redis to golang

WBOY
Release: 2023-05-10 10:57:06
Original
828 people have browsed it

Redis is an open source memory-based key-value data storage system that supports a variety of data structures and backup mechanisms. It is widely used in cache, message queues, real-time counters, session management and other fields. Golang is an open source programming language with the characteristics of high performance, strong typing, simplicity and readability, and concurrency security. It has gradually become a popular language in cloud computing, network programming, distributed systems and other fields. This article will introduce how to connect Redis in Golang and perform data reading and writing operations.

  1. Install Redis and Go Redis client

First you need to install Redis and start the Redis service. You can use the following command to install under the Ubuntu system:

sudo apt-get update
sudo apt-get install redis-server

After the installation is complete, you can use the following command to start the Redis service :

redis-server

At the same time, you need to use the Redis client in the Golang application to connect to the Redis service. The Go Redis client is an open source software package written by Gary Burd that provides support for basic Redis commands. It can be installed using the following command:

go get github.com/go-redis/redis

  1. Connect to Redis

Connect in the Go application The Redis service needs to specify the address, port number and password of the Redis server (if any). You can use the following code to connect to the Redis service:

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

func main() {
client := redis.NewClient(&redis.Options {

  Addr:     "localhost:6379",
  Password: "", // no password set
  DB:       0,  // use default DB
Copy after login

})

pong, err := client.Ping().Result()
fmt.Println(pong, err)
}

A Redis client object is created here, the address of the Redis server is specified as localhost, the port number is 6379, the password is empty, and the default DB is used. Then call the Ping() method to test the connection and output the connection result.

In actual applications, the address, port number and password of the Redis server need to be specified according to the actual situation.

  1. Data read and write operations

Next, you can use the Redis client object to perform data read and write operations. The following are some common Redis commands and their implementation in Go:

3.1 Setting values

You can use the Set() method to set key-value pairs:

err := client.Set("key", "value", 0).Err()
if err != nil {

panic(err)
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

}

The first parameter is the key name , the second parameter is the key value, the third parameter is the expiration time, 0 means no expiration.

3.2 Get the value

You can use the Get() method to get the key-value pair:

val, err := client.Get("key").Result()
if err != nil {

panic(err)
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

}
fmt.Println("key", val)

Among them, the first parameter is the key name and the return value is the key value , returns nil if the key does not exist.

3.3 Self-increment

You can use the Incr() method to self-increment:

err := client.Incr("key").Err()
if err != nil {

panic(err)
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

}

Among them, the first parameter is the key name, which means to auto-increment the key value.

3.4 List operation

You can use the LPush() method to insert elements into the head of the list:

err := client.LPush("mylist", "value1", "value2 ").Err()
if err != nil {

panic(err)
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

}

You can use the LRange() method to get the list elements:

vals, err := client.LRange("mylist", 0, -1).Result()
if err != nil {

panic(err)
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

}
for _, val := range vals {

fmt.Println(val)
Copy after login
Copy after login

}

The first parameter is the list name, the second parameter is the starting index, the third parameter is the ending index, and the return value is the element list.

3.5 Collection operation

You can use the SAdd() method to add elements to the collection:

err := client.SAdd("myset", "value1", "value2 ").Err()
if err != nil {

panic(err)
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

}

You can use the SMembers() method to get the set elements:

vals, err := client.SMembers("myset").Result()
if err != nil {

panic(err)
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login
Copy after login

}
for _, val := range vals {

fmt.Println(val)
Copy after login
Copy after login

}

The first parameter is the collection name, and the return value is the element list.

  1. Close the connection

Before ending the program, the Redis client connection needs to be closed in time.

err := client.Close()
if err != nil {
panic(err)
}

Through the above steps, you can connect in Golang Redis also performs data reading and writing operations. At the same time, the Go Redis client also provides richer operational support, please see the official documentation for details.

The above is the detailed content of How to connect redis to golang. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template