Install Redis on Mac
brew install redis
Copy the code
Redis basic use
- Modifying a Configuration File
/usr/local/etc/redis.conf
- Bind address, change it to the current host address
- Port: 6379
- Start the Redis server
(sudo) redis-server /usr/local/etc/redis.conf
Copy the code
- Check the PID of redis-server
ps xua | grep redis
Copy the code
- Start the Redis client
Redis -cli -h 127.0.0.1 -p 6379Copy the code
- Common grammar
keys * // Query all keys
get [key] // Get one
flushall // Delete all
set [key] [value] // Set one
Copy the code
The GO language operates Redis
-
From redis.cn client -go language – select Redigo – view API: godoc.org/github.com/…
-
It is mainly divided into 3 categories:
- Connecting to a Database
- In the API documentation, everything starts with Dial
- Operating database
- The Do() function
- The Send() function can be used with the Flush() and Receive() functions
- Reply assistant
- Equivalent to “type assertion”. Depending on the specific data type used, the call is selected
- Identify the interface{} returned by the DO function as a concrete type
- Connecting to a Database
-
Test cases:
package main
import (
"fmt"
"github.com/gomodule/redigo/redis"
)
func main(a) {
// 1. Connect to the database
conn, err := redis.Dial("tcp".": 6379")
iferr ! =nil {
fmt.Println("redis.Dial err: ", err)
return
}
defer conn.Close()
// 2. Operate the database
reply, err := conn.Do("set"."test"."yaxuan")
// 3. Revert helper class functions -- identified as concrete data types
r, e := redis.String(reply, err)
fmt.Println(r, e)
}
Copy the code