Compare commits

...

6 Commits

Author SHA1 Message Date
75725db6fb remove gmlock for print of glog; add time.Time support for gdb 2019-05-15 23:53:48 +08:00
5cd8475143 add time.Time support for convertParam function of gdb 2019-05-15 16:47:39 +08:00
5629f37939 version updates 2019-05-14 22:38:03 +08:00
08ec04d8b6 fix issue in unit test case of gredis 2019-05-14 22:37:13 +08:00
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
15 changed files with 188 additions and 69 deletions

View File

@ -17,6 +17,7 @@ import (
"github.com/gogf/gf/g/util/gconv"
"reflect"
"strings"
"time"
)
// 格式化SQL查询条件
@ -136,6 +137,10 @@ func convertParam(value interface{}) interface{} {
}
switch kind {
case reflect.Struct:
// 底层数据库引擎支持 time.Time 类型
if _, ok := value.(time.Time); ok {
return value
}
return gconv.String(value)
}
return value

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

@ -71,7 +71,7 @@ func Test_Stats(t *testing.T) {
redis.SetIdleTimeout(500*time.Millisecond)
redis.SetMaxConnLifetime(500*time.Millisecond)
array := make([]gredis.Conn, 0)
array := make([]*gredis.Conn, 0)
for i := 0; i < 10; i++ {
array = append(array, redis.Conn())
}

View File

@ -9,20 +9,19 @@
package glog
import (
"errors"
"fmt"
"github.com/gogf/gf/g/container/gtype"
"github.com/gogf/gf/g/os/gfile"
"github.com/gogf/gf/g/os/gfpool"
"github.com/gogf/gf/g/os/gmlock"
"github.com/gogf/gf/g/os/gtime"
"github.com/gogf/gf/g/text/gregex"
"io"
"os"
"runtime"
"strings"
"sync"
"time"
"errors"
"fmt"
"github.com/gogf/gf/g/container/gtype"
"github.com/gogf/gf/g/os/gfile"
"github.com/gogf/gf/g/os/gfpool"
"github.com/gogf/gf/g/os/gtime"
"github.com/gogf/gf/g/text/gregex"
"io"
"os"
"runtime"
"strings"
"sync"
"time"
)
type Logger struct {
@ -216,11 +215,7 @@ func (l *Logger) print(std io.Writer, s string) {
if _, ok := writer.(*Writer); ok {
if f := l.getFilePointer(); f != nil {
defer f.Close()
key := l.path.Val()
gmlock.Lock(key)
_, err := io.WriteString(f, s)
gmlock.Unlock(key)
if err != nil {
if _, err := io.WriteString(f, s); err != nil {
fmt.Fprintln(os.Stderr, err.Error())
}
}

View File

@ -7,10 +7,10 @@
package gconv_test
import (
"github.com/gogf/gf/g"
"github.com/gogf/gf/g/test/gtest"
"github.com/gogf/gf/g/util/gconv"
"testing"
"github.com/gogf/gf/g"
"github.com/gogf/gf/g/test/gtest"
"github.com/gogf/gf/g/util/gconv"
"testing"
)
@ -113,7 +113,6 @@ func Test_Map_StructWithJsonTag(t *testing.T) {
})
}
// 私有属性不会进行转换
func Test_Map_PrivateAttribute(t *testing.T) {
type User struct {
Id int
@ -123,4 +122,24 @@ func Test_Map_PrivateAttribute(t *testing.T) {
user := &User{1, "john"}
gtest.Assert(gconv.Map(user), g.Map{"Id" : 1})
})
}
}
//
//func Test_Map_StructInherit(t *testing.T) {
// type Base struct {
// Id int
// }
// type User struct {
// Base
// Name string
// }
// gtest.Case(t, func() {
// user := &User{
// Base : Base {
// Id : 100,
// },
// Name : "john",
// }
// fmt.Println(gconv.Map(user))
// //gtest.Assert(gconv.Map(user), g.Map{"Id" : 1})
// })
//}

View File

@ -0,0 +1,26 @@
package main
import (
"fmt"
"github.com/gogf/gf/g"
"time"
)
func main() {
db := g.DB()
// 开启调试模式以便于记录所有执行的SQL
db.SetDebug(true)
r, e := db.Table("user").Data(g.Map{
"passport" : "1",
"password" : "1",
"nickname" : "1",
"create_time" : time.Now(),
}).Insert()
if e != nil {
panic(e)
}
if r != nil {
fmt.Println(r.LastInsertId())
}
}

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,40 +1,24 @@
package main
import (
"encoding/json"
"fmt"
"time"
"github.com/gogf/gf/g"
"github.com/gogf/gf/g/util/gconv"
)
func main() {
//b, e := gjson.MarshalOrdered(g.Map{
// "a" : 1,
// "b" : 2,
// "c" : 3,
//})
//fmt.Println(e)
//fmt.Println(string(b))
//m := map[string]interface{}{
// "facet_is_special_price":[]string{"1"},
// "score_outlet":"0",
// "skus":[]string{"DI139BE71WDWDFMX", "DI139BE71WDWDFMX-519406"},
// "facet_novelty_two_days":[]string{"0"},
// "facet_brand":[]string{"139"},
// "sku":[]string{"DI139BE71WDWDFMX"},
//}
for {
m := make(map[string]interface{})
m["facet_is_special_price"] = []string{"1"}
m["score_outlet"] = "0"
m["skus"] = []string{"DI139BE71WDWDFMX", "DI139BE71WDWDFMX-519406"}
m["facet_novelty_two_days"] = []string{"0"}
m["facet_brand"] = []string{"139"}
m["sku"] = []string{"DI139BE71WDWDFMX"}
b, _ := json.Marshal(m)
fmt.Println(string(b))
time.Sleep(100*time.Millisecond)
type Person struct{
Name string
}
type Staff struct{
Person
StaffId int
}
staff := &Staff{}
params := g.Map{
"Name" : "john",
"StaffId" : "10000",
}
gconv.Struct(params, staff)
fmt.Println(staff)
}

View File

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