The functionality of Redis is really powerful and can be done as a database and can be cached.
Today, Redis supports the separation of operational space, which makes space and space non-impact.
SELECT Index
Switches to the specified database, and the database index number is specified with a numeric value, with 0 as the starting index value.
The default is to use database number No. 0.
Available versions:
>= 1.0.0
Complexity of Time:
O (1)
return value:
Ok
redis> SET number 0 # 默认使用 0 号数据库OKredis> SELECT 1 # 使用 1 号数据库OKredis[1]> GET number # 已经切换到 1 号数据库,注意 Redis 现在的命令提示符多了个 [1](nil)redis[1]> SET number 1OKredis[1]> GET number"1"redis[1]> SELECT 3 # 再切换到 3 号数据库OKredis[3]> # 提示符从 [1] 改变成了 [3]
The Select index operation selects the database and, if not selected, the default is DB 0. After each connection is established, if not select, the default is for the DB 0 operation.
Example1
The following code establishes a connection, makes a select operation, selects DB 3, and then closes. Then two connections were established for SADD operation. As you can see from the output, two sadd operations are done for DB 0.
Package Mainimport ("Log" "Github.com/garyburd/redigo/redis") const (Redisserver = "10.194.80.35:6379") Func main () {Selectdb (3) Batchsadd () batchSADD2 ()}func selectdb (num int) {option: = Redis. Dialpassword ("123456") C, err: = Redis. Dial ("TCP", redisserver, option) if err! = Nil {log. PRINTLN ("Connect Server failed:", err) return} defer C.close () v, err: = Redis. String (C.do ("select", num)) if err! = Nil {log. Println ("Select failed:", err) return} log. Println ("Select:", v)}func Batchsadd () {option: = Redis. Dialpassword ("123456") C, err: = Redis. Dial ("TCP", redisserver, option) if err! = Nil {log. PRINTLN ("Connect Server failed:", err) return} defer C.close () Setkey: = "MySet" args: = []interface{} {Setkey} args = append (args, "10.1") args = append (args, "10.2") V, err: = Redis. Int64 (C.do ("Sadd", args ...)) If err! = Nil {log. Println ("Sadd failed:", err) return} log. Println ("Sadd:", v)}func batchSADD2 () {option: = Redis. Dialpassword ("123456") C, err: = Redis. Dial ("TCP", redisserver, option) if err! = Nil {log. PRINTLN ("Connect Server failed:", err) return} defer C.close () Setkey: = "MySet" args: = []interface{} {Setkey} args = append (args, "10.3") args = append (args, "10.4") v, err: = Redis. Int64 (C.do ("Sadd", args ...)) If err! = Nil {log. Println ("Sadd failed:", err) return} log. Println ("Sadd:", V)}
/*
Select DB 3
First Sadd to DB 0
Second Sadd to DB 0
*/
Output Result:
127.0.0.1:6379> smembers MySet
1) "10.1"
2) "10.4"
3) "10.2"
4) "10.3"
127.0.0.1:6379>
127.0.0.1:6379>
127.0.0.1:6379> Select 3
Ok
127.0.0.1:6379[3]> smembers MySet
(empty list or set)
127.0.0.1:6379[3]>
Example2
To establish a connection, select 3 Selects DB 3, then Sadd, and then establishes a connection, directly sadd. From the results can be seen, the first Sadd operation on the DB 3 operation, the second sadd to the DB 0 operation.
Package Mainimport ("Log" "Github.com/garyburd/redigo/redis") const (Redisserver = "10.194.80.35:6379") Func main () {Batchsadd () batchSADD2 ()}func selectdb (num int) {option: = Redis. Dialpassword ("123456") C, err: = Redis. Dial ("TCP", redisserver, option) if err! = Nil {log. PRINTLN ("Connect Server failed:", err) return} defer C.close () v, err: = Redis. String (C.do ("select", num)) if err! = Nil {log. Println ("Select failed:", err) return} log. Println ("Select:", v)}func Batchsadd () {option: = Redis. Dialpassword ("123456") C, err: = Redis. Dial ("TCP", redisserver, option) if err! = Nil {log. PRINTLN ("Connect Server failed:", err) return} defer C.close () ret, err: = Redis. String (C.do ("select", 3)) if err! = Nil {log. Println ("Select failed:", err) return} log. Println ("Select:", ret) Setkey: = "MySet" args: = []interface{}{setkey} args = append (args, "10.1") args = append (args, "10.2") V, err: = Redis. Int64 (C.do ("Sadd", args ...)) If err! = Nil {log. Println ("Sadd failed:", err) return} log. Println ("Sadd:", v)}func batchSADD2 () {option: = Redis. Dialpassword ("123456") C, err: = Redis. Dial ("TCP", redisserver, option) if err! = Nil {log. PRINTLN ("Connect Server failed:", err) return} defer C.close () Setkey: = "MySet" args: = []interface{} {Setkey} args = append (args, "10.5") args = append (args, "10.6") v, err: = Redis. Int64 (C.do ("Sadd", args ...)) If err! = Nil {log. Println ("Sadd failed:", err) return} log. Println ("Sadd:", V)}
/*
Firt Sadd to DB 3
Second Sadd to DB 0
*/
Output Result:
127.0.0.1:6379> smembers MySet
1) "10.6"
2) "10.1"
3) "10.5"
4) "10.4"
5) "10.2"
6) "10.3"
127.0.0.1:6379> Select 3
Ok
127.0.0.1:6379[3]> smembers MySet
1) "10.1"
2) "10.2"
127.0.0.1:6379[3]>
Reference
Http://redisdoc.com/connection/select.html
Redis Select Database