Skip to content

Commit 0259d3a

Browse files
committed
commit the test code
1 parent 75307ee commit 0259d3a

File tree

6 files changed

+113
-36
lines changed

6 files changed

+113
-36
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
## The way to learn Golang
2+
### this is the code I practiced

redis_test/conn.go

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,49 @@
11
package redistest
22

33
import (
4-
"github.com/garyburd/redigo/redis"
4+
"github.com/garyburd/redigo/redis"
55
)
66

7-
var conn redis.Conn
7+
//var conn redis.Conn
8+
func newPool() *redis.Pool {
9+
return &redis.Pool{
10+
MaxIdle: 80,
11+
MaxActive: 20000,
12+
Dial: func() (redis.Conn, error) {
13+
c, err := redis.Dial("tcp", "localhost:6379")
14+
if err != nil {
15+
panic(err.Error())
16+
}
17+
return c, err
18+
},
19+
}
20+
}
21+
22+
var pool = newPool()
823

9-
func Init(redisIp string,redisPort string)error{
10-
var err error
11-
conn,err = redis.Dial(redisIp,redisPort)
12-
return err
24+
/*
25+
func Init()error{
26+
pool = newPool()
1327
}
28+
*/
1429

15-
func DoGet(userId string)string{
16-
str,_ := redis.String(conn.Do("HGET",userId,"ad1"))
17-
return str
30+
func getConn() redis.Conn {
31+
c := pool.Get()
32+
return c
1833
}
1934

20-
func PipeGet(userId string)string{
21-
conn.Send("HGET",userId,"ad1")
22-
conn.Flush()
23-
str,_ := redis.String(conn.Receive())
24-
return str
35+
func releaseConn(c *redis.Conn) {
36+
(*c).Close()
2537
}
26-
func Release(){
27-
conn.Close()
38+
39+
func DoGet(userId string) {
40+
c := getConn()
41+
c.Do("INCR",userId)
42+
c.Close()
43+
}
44+
45+
func PipeGet(userId string) {
46+
c := getConn()
47+
c.Send("INCR",userId)
48+
c.Close()
2849
}

redis_test/conn_test.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,21 @@ import(
55
)
66

77
func TestDoGet(t *testing.T){
8-
Init("tcp","127.0.0.1:6379")
9-
str := DoGet("test")
10-
if str != "1"{
11-
t.Errorf("DoGet() is faild")
12-
}
13-
defer Release()
8+
DoGet("test")
149
}
1510

1611
func TestPipeGet(t *testing.T){
17-
Init("tcp","127.0.0.1:6379")
18-
str := PipeGet("test")
19-
if str != "1"{
20-
t.Errorf("DoGet() is faild")
21-
}
22-
defer Release()
12+
PipeGet("test")
2313
}
2414

2515
func BenchmarkDoGet(b *testing.B){
26-
b.StopTimer()
27-
Init("tcp","127.0.0.1:6379")
28-
b.StartTimer()
2916
for i:=0;i<b.N;i++ {
3017
DoGet("test")
3118
}
32-
defer Release()
3319
}
3420

3521
func BenchmarkPipeGet(b *testing.B){
36-
b.StopTimer()
37-
Init("tcp","127.0.0.1:6379")
38-
b.StartTimer()
3922
for i:=0;i<b.N;i++ {
4023
PipeGet("test")
4124
}
42-
defer Release()
4325
}

redispipeline/pipeline.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package RedisCtrl
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/garyburd/redigo/redis"
7+
)
8+
9+
func DoRedis(c redis.Conn) {
10+
c.Send("MULTI")
11+
c.Send("INCR", "foo0")
12+
c.Send("INCR", "foo1")
13+
c.Send("INCR", "foo2")
14+
c.Send("INCR", "foo3")
15+
c.Send("HINCRBY", "map", "key1", "1")
16+
r, _ := c.Do("EXEC")
17+
fmt.Println(r)
18+
}

redispipeline/redispool.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package RedisCtrl
2+
3+
import(
4+
"fmt"
5+
6+
"github.com/garyburd/redigo/redis"
7+
)
8+
9+
func

redispool_demo.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
import (
3+
"net/http"
4+
"runtime"
5+
"io"
6+
"fmt"
7+
"log"
8+
"time"
9+
"github.com/garyburd/redigo/redis"
10+
)
11+
// 重写生成连接池方法
12+
func newPool() *redis.Pool {
13+
return &redis.Pool{
14+
MaxIdle: 80,
15+
MaxActive: 1200, // max number of connections
16+
Dial: func() (redis.Conn, error) {
17+
c, err := redis.Dial("tcp", "localhost:6379")
18+
if err != nil {
19+
panic(err.Error())
20+
}
21+
return c, err
22+
},
23+
}
24+
}
25+
// 生成连接池
26+
var pool = newPool()
27+
func redisServer(w http.ResponseWriter, r *http.Request) {
28+
startTime := time.Now()
29+
// 从连接池里面获得一个连接
30+
c := pool.Get()
31+
// 连接完关闭,其实没有关闭,是放回池里,也就是队列里面,等待下一个重用
32+
defer c.Close()
33+
dbkey := "netgame:info"
34+
if ok, err := redis.Bool(c.Do("LPUSH", dbkey, "yangzetao")); ok {
35+
} else {
36+
log.Print(err)
37+
}
38+
msg := fmt.Sprintf("用时:%s", time.Now().Sub(startTime));
39+
io.WriteString(w, msg+"\n\n");
40+
}
41+
func main() {
42+
runtime.GOMAXPROCS(runtime.NumCPU());
43+
http.HandleFunc("/", redisServer);
44+
http.ListenAndServe(":9527", nil);
45+
}

0 commit comments

Comments
 (0)