Preface
Redis is a high-performance NoSQL database that can handle key-value data types. It supports a variety of data operations, such as strings, hash tables, lists, sets, etc., and is a very popular database.
As an emerging programming language, Golang also has high performance and can be used in conjunction with Redis to improve the overall performance of the system. This article will introduce how to use Redis in Golang and explain the installation process of Redis 3 in detail.
Redis 3 Installation
First you need to download the Redis compressed package from the Redis official website. You can find the download link for Redis on the official website: http://redis.io/download. Find the link to the Redis 3 version on the download page, then copy the link address, and execute the wget command in the Linux terminal to download:
wget http://download.redis.io/releases/redis-3.2.12.tar.gz
After the download is complete, Decompress the Redis compressed package to the specified location (here is /usr/local/redis
):
tar xzf redis-3.2.12.tar.gz -C /usr/local/redis
Enter the decompressed Directory:
cd /usr/local/redis/redis-3.2.12
Execute the make command to compile:
make
After compilation is completed, execute the make install command to install Redis:
make install
After the installation is completed, confirm whether Redis has been installed normally and execute the following command:
redis-server --version
If the version information of Redis can be displayed, it means that the installation of Redis has been completed.
Golang uses Redis
Using Redis in Golang requires installing the corresponding client package. You can use the go get command to install:
go get github.com/go-redis/redis
Connecting to Redis in Golang is very simple, use the # in the client package installed in the first step. ##NewClient Function:
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 }) pong, err := client.Ping().Result() fmt.Println(pong, err) }
func main() { // 初始化客户端 client := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password set DB: 0, // use default DB }) // 向 Redis 中写入数据 err := client.Set("key", "value", 0).Err() if err != nil { panic(err) } // 从 Redis 中读取数据 val, err := client.Get("key").Result() if err != nil { panic(err) } fmt.Println("key", val) // 删除 Redis 中的数据 err = client.Del("key").Err() if err != nil { panic(err) } // 获取 Redis 中所有的 key 列表 keys, err := client.Keys("*").Result() if err != nil { panic(err) } fmt.Println("keys", keys) }
The above is the detailed content of golang install redis 3. For more information, please follow other related articles on the PHP Chinese website!