Compare commits

...

2 Commits

Author SHA1 Message Date
c0b46f364a version updates 2019-05-14 22:02:09 +08:00
8c5f74e8bb add DoVar/ReceiveVar function for gredis 2019-05-14 21:34:38 +08:00
9 changed files with 102 additions and 12 deletions

View File

@ -7,14 +7,17 @@
// Package gredis provides convenient client for redis server.
//
// Redis Client.
//
// Redis Commands Official: https://redis.io/commands
//
// Redis Chinese Documentation: http://redisdoc.com/
package gredis
import (
"fmt"
"github.com/gogf/gf/g/container/gmap"
"github.com/gogf/gf/third/github.com/gomodule/redigo/redis"
"github.com/gogf/gf/g/container/gvar"
"github.com/gogf/gf/third/github.com/gomodule/redigo/redis"
"time"
)
@ -31,7 +34,9 @@ type Redis struct {
}
// Redis connection.
type Conn redis.Conn
type Conn struct {
redis.Conn
}
// Redis configuration.
type Config struct {
@ -134,16 +139,17 @@ func (r *Redis) Close() error {
return r.pool.Close()
}
// Alias of GetConn, see GetConn.
func (r *Redis) Conn() Conn {
return r.GetConn()
}
// GetConn returns a raw underlying connection object,
// Conn returns a raw underlying connection object,
// which expose more methods to communicate with server.
// **You should call Close function manually if you do not use this connection any further.**
func (r *Redis) GetConn() Conn {
return r.pool.Get().(Conn)
func (r *Redis) Conn() *Conn {
return &Conn{ r.pool.Get() }
}
// Alias of Conn, see Conn.
func (r *Redis) GetConn() *Conn {
return r.Conn()
}
// SetMaxIdle sets the MaxIdle attribute of the connection pool.
@ -175,15 +181,21 @@ func (r *Redis) Stats() *PoolStats {
// Do automatically get a connection from pool, and close it when reply received.
// It does not really "close" the connection, but drop it back to the connection pool.
func (r *Redis) Do(command string, args ...interface{}) (interface{}, error) {
conn := r.pool.Get()
conn := &Conn{ r.pool.Get() }
defer conn.Close()
return conn.Do(command, args...)
}
// DoVar returns value from Do as gvar.Var.
func (r *Redis) DoVar(command string, args ...interface{}) (*gvar.Var, error) {
v, err := r.Do(command, args...)
return gvar.New(v, true), err
}
// Deprecated.
// Send writes the command to the client's output buffer.
func (r *Redis) Send(command string, args ...interface{}) error {
conn := r.pool.Get()
conn := &Conn{ r.pool.Get() }
defer conn.Close()
return conn.Send(command, args...)
}

View File

@ -0,0 +1,21 @@
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gredis
import "github.com/gogf/gf/g/container/gvar"
// DoVar returns value from Do as gvar.Var.
func (c *Conn) DoVar(command string, args ...interface{}) (*gvar.Var, error) {
v, err := c.Do(command, args...)
return gvar.New(v, true), err
}
// ReceiveVar receives a single reply as gvar.Var from the Redis server.
func (c *Conn) ReceiveVar() (*gvar.Var, error) {
v, err := c.Receive()
return gvar.New(v, true), err
}

View File

@ -0,0 +1,16 @@
package main
import (
"fmt"
"github.com/gogf/gf/g"
)
func main() {
conn := g.Redis().Conn()
defer conn.Close()
if _, err := conn.Do("SET", "k", "v"); err != nil {
panic(err)
}
v, _ := conn.DoVar("GET", "k")
fmt.Println(v.String())
}

View File

@ -0,0 +1,19 @@
package main
import (
"fmt"
"github.com/gogf/gf/g"
)
func main() {
conn := g.Redis().Conn()
defer conn.Close()
conn.Send("SET", "foo", "bar")
conn.Send("GET", "foo")
conn.Flush()
// reply from SET
conn.Receive()
// reply from GET
v, _ := conn.ReceiveVar()
fmt.Println(v.String())
}

View File

@ -0,0 +1,22 @@
package main
import (
"fmt"
"github.com/gogf/gf/g"
)
func main() {
conn := g.Redis().Conn()
defer conn.Close()
_, err := conn.Do("SUBSCRIBE", "channel")
if err != nil {
panic(err)
}
for {
reply, err := conn.ReceiveVar()
if err != nil {
panic(err)
}
fmt.Println(reply.Strings())
}
}

View File

@ -1,4 +1,4 @@
package gf
const VERSION = "v1.6.11"
const VERSION = "v1.6.12"
const AUTHORS = "john<john@goframe.org>"