1、快速开始
1)第一步连接redis 使用说明:Connect to Redis | Docs
2)使用命令行工具redis-cli和Redis server保持通 redis-cli -h 127.0.0.1 -p 6379
3)RedisInsight-官方可视化工
python语言链接库:redis-py
pip install redis
pip install hiredis
r = redis.Redis(host='localhost', port=6379, decode_responses=True)
存储string格式
r.set('foo', 'bar') # True r.get('foo') # bar
存储一个字典r.hset('user-session:123', mapping={ 'name': 'John', "surname": 'Smith', "company": 'Redis', "age": 29 }) # True r.hgetall('user-session:123') # {'surname': 'Smith', 'name': 'John', 'company': 'Redis', 'age': '29'}
redis集群模式from redis.cluster import RedisCluster rc = RedisCluster(host='localhost', port=16379) print(rc.get_nodes()) # [[host=127.0.0.1,port=16379,name=127.0.0.1:16379,server_type=primary,redis_connection=Redis<ConnectionPool<Connection<host=127.0.0.1,port=16379,db=0>>>], ... rc.set('foo', 'bar') # True rc.get('foo') # b'bar'
通过tsl连接redisimport redis r = redis.Redis( host="my-redis.cloud.redislabs.com", port=6379, username="default", # use your Redis user. More info https://redis.io/docs/latest/operate/oss_and_stack/management/security/acl/ password="secret", # use your Redis password ssl=True, ssl_certfile="./redis_user.crt", ssl_keyfile="./redis_user_private.key", ssl_ca_certs="./redis_ca.pem", ) r.set('foo', 'bar') # True r.get('foo') # b'bar'
go的使用方式:go mod init github.com/my/repo go get github.com/redis/go-redis/v9
连接redis server:import ( "context" "fmt" "github.com/redis/go-redis/v9" ) func main() { client := redis.NewClient(&redis.Options{ Addr: "localhost:6379", Password: "", // no password set DB: 0, // use default DB }) }
另一种方式:opt, err := redis.ParseURL("redis://<user>:<pass>@localhost:6379/<db>") if err != nil { panic(err) } client := redis.NewClient(opt)
存储string格式:ctx := context.Background() err := client.Set(ctx, "foo", "bar", 0).Err() if err != nil { panic(err) } val, err := client.Get(ctx, "foo").Result() if err != nil { panic(err) } fmt.Println("foo", val)
存储map数据session := map[string]string{"name": "John", "surname": "Smith", "company": "Redis", "age": "29"} for k, v := range session { err := client.HSet(ctx, "user-session:123", k, v).Err() if err != nil { panic(err) } } userSession := client.HGetAll(ctx, "user-session:123").Val() fmt.Println(userSession)
应用redis集群:client := redis.NewClusterClient(&redis.ClusterOptions{ Addrs: []string{":16379", ":16380", ":16381", ":16382", ":16383", ":16384"}, // To route commands by latency or randomly, enable one of the following. //RouteByLatency: true, //RouteRandomly: true, })
用tsl连接redis:// Load client cert cert, err := tls.LoadX509KeyPair("redis_user.crt", "redis_user_private.key") if err != nil { log.Fatal(err) } // Load CA cert caCert, err := os.ReadFile("redis_ca.pem") if err != nil { log.Fatal(err) } caCertPool := x509.NewCertPool() caCertPool.AppendCertsFromPEM(caCert) client := redis.NewClient(&redis.Options{ Addr: "my-redis.cloud.redislabs.com:6379", Username: "default", // use your Redis user. More info https://redis.io/docs/latest/operate/oss_and_stack/management/security/acl/ Password: "secret", // use your Redis password TLSConfig: &tls.Config{ MinVersion: tls.VersionTLS12, Certificates: []tls.Certificate{cert}, RootCAs: caCertPool, }, }) //send SET command err = client.Set(ctx, "foo", "bar", 0).Err() if err != nil { panic(err) } //send GET command and print the value val, err := client.Get(ctx, "foo").Result() if err != nil { panic(err) } fmt.Println("foo", val)