From 023e1da777581b2827989de7d2981cb0b40e9f23 Mon Sep 17 00:00:00 2001 From: John Date: Sat, 30 Jun 2018 22:50:21 +0800 Subject: [PATCH] =?UTF-8?q?=E6=A1=86=E6=9E=B6=E9=83=A8=E5=88=86=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TODO | 3 +- .../gmap/{gmap_test.go => gmap_bench_test.go} | 23 ++++++------ g/container/gmap/int_interface_map.go | 36 +++++++++++++++++++ g/encoding/gjson/gjson.go | 6 +++- g/encoding/gparser/gparser.go | 6 ++-- g/net/ghttp/ghttp_server_cookie.go | 8 ++--- g/net/ghttp/ghttp_server_session.go | 4 +++ g/os/gtime/gtime.go | 9 +++++ g/util/gconv/gconv.go | 9 +++-- geg/other/test.go | 7 ++-- 10 files changed, 84 insertions(+), 27 deletions(-) rename g/container/gmap/{gmap_test.go => gmap_bench_test.go} (78%) diff --git a/TODO b/TODO index a1ee9dcca..e37de6cb1 100644 --- a/TODO +++ b/TODO @@ -11,7 +11,7 @@ Cookie&Session数据池化处理; ghttp.Client增加proxy特性; gtime增加对时区转换的封装,并简化失去转换时对类似+80500时区的支持; 改进gf-orm的where查询功能,参考thinkphp 里的where查询语法; -改进ghttp.Server平滑重启机制,当新进程接管服务后,再使用进程间通信方式通知父进程销毁; + DONE: @@ -40,6 +40,7 @@ DONE: 23. 平滑重启机制改进,以便于开发阶段调试; 24. 对grpool进行优化改进,包括属性原子操作封装采用gtype实现,修正设计BUG:https://github.com/johng-cn/gf/issues/6; 25. gredis增加redis密码支持; +26. 改进ghttp.Server平滑重启机制,当新进程接管服务后,再使用进程间通信方式通知父进程销毁; diff --git a/g/container/gmap/gmap_test.go b/g/container/gmap/gmap_bench_test.go similarity index 78% rename from g/container/gmap/gmap_test.go rename to g/container/gmap/gmap_bench_test.go index 8291ef0f5..ccba0b6b0 100644 --- a/g/container/gmap/gmap_test.go +++ b/g/container/gmap/gmap_bench_test.go @@ -6,25 +6,24 @@ // go test *.go -bench=".*" -package gmap_test +package gmap import ( "testing" - "gitee.com/johng/gf/g/container/gmap" "strconv" ) -var ibm = gmap.NewIntBoolMap() -var iim = gmap.NewIntIntMap() -var iifm = gmap.NewIntInterfaceMap() -var ism = gmap.NewIntStringMap() -var ififm = gmap.NewInterfaceInterfaceMap() -var sbm = gmap.NewStringBoolMap() -var sim = gmap.NewStringIntMap() -var sifm = gmap.NewStringInterfaceMap() -var ssm = gmap.NewStringStringMap() -var uifm = gmap.NewUintInterfaceMap() +var ibm = NewIntBoolMap() +var iim = NewIntIntMap() +var iifm = NewIntInterfaceMap() +var ism = NewIntStringMap() +var ififm = NewInterfaceInterfaceMap() +var sbm = NewStringBoolMap() +var sim = NewStringIntMap() +var sifm = NewStringInterfaceMap() +var ssm = NewStringStringMap() +var uifm = NewUintInterfaceMap() func BenchmarkIntBoolMap_Set(b *testing.B) { for i := 0; i < b.N; i++ { diff --git a/g/container/gmap/int_interface_map.go b/g/container/gmap/int_interface_map.go index d36eed4e5..708598771 100644 --- a/g/container/gmap/int_interface_map.go +++ b/g/container/gmap/int_interface_map.go @@ -89,10 +89,42 @@ func (this *IntInterfaceMap) GetInt(key int) int { return gconv.Int(this.Get(key)) } +func (this *IntInterfaceMap) GetInt8(key int) int8 { + return gconv.Int8(this.Get(key)) +} + +func (this *IntInterfaceMap) GetInt16(key int) int16 { + return gconv.Int16(this.Get(key)) +} + +func (this *IntInterfaceMap) GetInt32(key int) int32 { + return gconv.Int32(this.Get(key)) +} + +func (this *IntInterfaceMap) GetInt64(key int) int64 { + return gconv.Int64(this.Get(key)) +} + func (this *IntInterfaceMap) GetUint (key int) uint { return gconv.Uint(this.Get(key)) } +func (this *IntInterfaceMap) GetUint8 (key int) uint8 { + return gconv.Uint8(this.Get(key)) +} + +func (this *IntInterfaceMap) GetUint16 (key int) uint16 { + return gconv.Uint16(this.Get(key)) +} + +func (this *IntInterfaceMap) GetUint32 (key int) uint32 { + return gconv.Uint32(this.Get(key)) +} + +func (this *IntInterfaceMap) GetUint64 (key int) uint64 { + return gconv.Uint64(this.Get(key)) +} + func (this *IntInterfaceMap) GetFloat32 (key int) float32 { return gconv.Float32(this.Get(key)) } @@ -105,6 +137,10 @@ func (this *IntInterfaceMap) GetString (key int) string { return gconv.String(this.Get(key)) } +func (this *IntInterfaceMap) GetStrings (key int) []string { + return gconv.Strings(this.Get(key)) +} + // 删除键值对 func (this *IntInterfaceMap) Remove(key int) { this.mu.Lock() diff --git a/g/encoding/gjson/gjson.go b/g/encoding/gjson/gjson.go index b1fecc7c1..24dd51d50 100644 --- a/g/encoding/gjson/gjson.go +++ b/g/encoding/gjson/gjson.go @@ -101,9 +101,13 @@ func Load (path string) (*Json, error) { } // 支持的配置文件格式:xml, json, yaml/yml, toml -func LoadContent (data []byte, t string) (*Json, error) { +func LoadContent (data []byte, dataType...string) (*Json, error) { var err error var result interface{} + t := "json" + if len(dataType) > 0 { + t = dataType[0] + } switch t { case "xml": fallthrough case ".xml": diff --git a/g/encoding/gparser/gparser.go b/g/encoding/gparser/gparser.go index d4588d7cd..a711ef466 100644 --- a/g/encoding/gparser/gparser.go +++ b/g/encoding/gparser/gparser.go @@ -32,9 +32,9 @@ func Load (path string) (*Parser, error) { } } -// 支持的配置文件格式:xml, json, yaml/yml, toml -func LoadContent (data []byte, fileType string) (*Parser, error) { - if j, e := gjson.LoadContent(data, fileType); e == nil { +// 支持的数据内容格式:json(默认), xml, yaml/yml, toml +func LoadContent (data []byte, dataType...string) (*Parser, error) { + if j, e := gjson.LoadContent(data, dataType...); e == nil { return &Parser{j}, nil } else { return nil, e diff --git a/g/net/ghttp/ghttp_server_cookie.go b/g/net/ghttp/ghttp_server_cookie.go index 363d57779..df0a78a18 100644 --- a/g/net/ghttp/ghttp_server_cookie.go +++ b/g/net/ghttp/ghttp_server_cookie.go @@ -12,7 +12,6 @@ package ghttp import ( "sync" "time" - "strings" "net/http" "gitee.com/johng/gf/g/os/gtime" ) @@ -43,7 +42,7 @@ func GetCookie(r *Request) *Cookie { } c := &Cookie { data : make(map[string]CookieItem), - domain : strings.Split(r.Host, ":")[0], + domain : r.GetHost(), server : r.Server, request : r, response : r.Response, @@ -53,15 +52,13 @@ func GetCookie(r *Request) *Cookie { return c } -// 从请求流中初始化 +// 从请求流中初始化,无锁 func (c *Cookie) init() { - c.mu.Lock() for _, v := range c.request.Cookies() { c.data[v.Name] = CookieItem { v.Value, v.Domain, v.Path, v.Expires.Second(), v.HttpOnly, } } - c.mu.Unlock() } // 获取所有的Cookie并构造成map返回 @@ -137,6 +134,7 @@ func (c *Cookie) Close() { func (c *Cookie) Output() { c.mu.RLock() for k, v := range c.data { + // 只有expire != 0的才是服务端在本地请求中设置的cookie if v.expire == 0 { continue } diff --git a/g/net/ghttp/ghttp_server_session.go b/g/net/ghttp/ghttp_server_session.go index e043596ab..418ce1c98 100644 --- a/g/net/ghttp/ghttp_server_session.go +++ b/g/net/ghttp/ghttp_server_session.go @@ -86,6 +86,10 @@ func (s *Session) GetUint (k string) uint { return gconv.Uint(s.Get(k)) } +func (s *Session) GetUint8 (k string) uint8 { + return gconv.Uint8(s.Get(k)) +} + func (s *Session) GetFloat32 (k string) float32 { return gconv.Float32(s.Get(k)) } diff --git a/g/os/gtime/gtime.go b/g/os/gtime/gtime.go index 5a1214511..a61da09c3 100644 --- a/g/os/gtime/gtime.go +++ b/g/os/gtime/gtime.go @@ -92,6 +92,15 @@ func Format(format string, timestamps...int64) string { } // 字符串转换为时间对象,需要给定字符串时间格式,format格式形如:2006-01-02 15:04:05 +// 不传递自定义格式下默认支持的标准时间格式: +// "2017-12-14 04:51:34 +0805 LMT", +// "2006-01-02T15:04:05Z07:00", +// "2014-01-17T01:19:15+08:00", +// "2018-02-09T20:46:17.897Z", +// "2018-02-09 20:46:17.897", +// "2018-02-09T20:46:17Z", +// "2018-02-09 20:46:17", +// "2018-02-09", func StrToTime(str string, format...string) (time.Time, error) { // 优先使用用户输入日期格式进行转换 if len(format) > 0 { diff --git a/g/util/gconv/gconv.go b/g/util/gconv/gconv.go index 1b3f6903d..98ab9a7e1 100644 --- a/g/util/gconv/gconv.go +++ b/g/util/gconv/gconv.go @@ -18,7 +18,7 @@ import ( ) // 将变量i转换为字符串指定的类型t -func Convert(i interface{}, t string) interface{} { +func Convert(i interface{}, t string, params...interface{}) interface{} { switch t { case "int": return Int(i) case "int8": return Int8(i) @@ -35,7 +35,12 @@ func Convert(i interface{}, t string) interface{} { case "bool": return Bool(i) case "string": return String(i) case "[]byte": return Bytes(i) - case "time.Time": return Time(i) + case "time.Time": + if len(params) > 0 { + return Time(i, String(params[0])) + } + return Time(i) + case "time.Duration": return TimeDuration(i) default: return i diff --git a/geg/other/test.go b/geg/other/test.go index 5d0e543e1..2a09e7127 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -2,10 +2,11 @@ package main import ( "fmt" - "gitee.com/johng/gf/g/util/gconv" + "gitee.com/johng/gf/g/encoding/gjson" ) func main() { - fmt.Println(gconv.Float64(float32(19.66))) - fmt.Println(float64(float32(19.66))) + content := `[0.00000000059, 1.598877777409]` + j, _ := gjson.LoadContent([]byte(content), "json") + fmt.Println(j.GetString("0")) }