Skip to content

Commit

Permalink
some improving
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn committed Dec 8, 2019
1 parent fe4f8e1 commit 6ca5141
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 76 deletions.
5 changes: 3 additions & 2 deletions frame/gins/gins.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"github.com/gogf/gf/internal/intlog"
"github.com/gogf/gf/net/ghttp"
"github.com/gogf/gf/util/gutil"

"github.com/gogf/gf/os/gfile"

Expand Down Expand Up @@ -242,8 +243,8 @@ func parseDBConfigNode(value interface{}) *gdb.ConfigNode {
if err != nil {
glog.Panic(err)
}
if value, ok := nodeMap["link"]; ok {
node.LinkInfo = gconv.String(value)
if _, v := gutil.MapPossibleItemByKey(nodeMap, "link"); v != nil {
node.LinkInfo = gconv.String(v)
}
// Parse link syntax.
if node.LinkInfo != "" && node.Type == "" {
Expand Down
4 changes: 2 additions & 2 deletions net/ghttp/ghttp_response_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ func (r *Response) buildInVars(params ...map[string]interface{}) map[string]inte
if c := gcfg.Instance(); c.Available() {
vars["Config"] = c.GetMap(".")
}
vars["Get"] = r.Request.GetQueryMap()
vars["Post"] = r.Request.GetPostMap()
vars["Form"] = r.Request.GetFormMap()
vars["Query"] = r.Request.GetQueryMap()
vars["Cookie"] = r.Request.Cookie.Map()
vars["Session"] = r.Request.Session.Map()
return vars
Expand Down
2 changes: 2 additions & 0 deletions net/ghttp/ghttp_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type (

// Router item just for dumping.
RouterItem struct {
Type int
Middleware string
Domain string
Method string
Expand Down Expand Up @@ -380,6 +381,7 @@ func (s *Server) GetRouterMap() map[string][]RouterItem {
array, _ := gregex.MatchString(`(.*?)%([A-Z]+):(.+)@(.+)`, k)
for index, registeredItem := range registeredItems {
item := RouterItem{
Type: registeredItem.handler.itemType,
Middleware: array[1],
Domain: array[4],
Method: array[2],
Expand Down
2 changes: 1 addition & 1 deletion net/gipv4/gipv4.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func IntranetIPArray() (ips []string, err error) {
continue
}
ipStr := ip.String()
if ipStr != "127.0.0.1" && IsIntranet(ipStr) {
if IsIntranet(ipStr) {
ips = append(ips, ipStr)
}
}
Expand Down
41 changes: 10 additions & 31 deletions os/gcache/gcache.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,20 @@ import "time"
var cache = New()

// Set sets cache with <key>-<value> pair, which is expired after <duration>.
//
// The parameter <duration> can be either type of int or time.Duration.
// If <duration> is type of int, it means <duration> milliseconds.
// If <duration> <=0 means it does not expire.
// It does not expire if <duration> <= 0.
func Set(key interface{}, value interface{}, duration time.Duration) {
cache.Set(key, value, duration)
}

// SetIfNotExist sets cache with <key>-<value> pair if <key> does not exist in the cache,
// which is expired after <duration>.
//
// The parameter <duration> can be either type of int or time.Duration.
// If <duration> is type of int, it means <duration> milliseconds.
// If <duration> <=0 means it does not expire.
// which is expired after <duration>. It does not expire if <duration> <= 0.
func SetIfNotExist(key interface{}, value interface{}, duration time.Duration) bool {
return cache.SetIfNotExist(key, value, duration)
}

// Sets batch sets cache with key-value pairs by <data>, which is expired after <duration>.
//
// The parameter <duration> can be either type of int or time.Duration.
// If <duration> is type of int, it means <duration> milliseconds.
// If <duration> <=0 means it does not expire.
// It does not expire if <duration> <= 0.
func Sets(data map[interface{}]interface{}, duration time.Duration) {
cache.Sets(data, duration)
}
Expand All @@ -50,33 +41,21 @@ func Get(key interface{}) interface{} {
// or sets <key>-<value> pair and returns <value> if <key> does not exist in the cache.
// The key-value pair expires after <duration>.
//
// The parameter <duration> can be either type of int or time.Duration.
// If <duration> is type of int, it means <duration> milliseconds.
// If <duration> <=0 means it does not expire.
// It does not expire if <duration> <= 0.
func GetOrSet(key interface{}, value interface{}, duration time.Duration) interface{} {
return cache.GetOrSet(key, value, duration)
}

// GetOrSetFunc returns the value of <key>,
// or sets <key> with result of function <f> and returns its result
// if <key> does not exist in the cache.
// The key-value pair expires after <duration>.
//
// The parameter <duration> can be either type of int or time.Duration.
// If <duration> is type of int, it means <duration> milliseconds.
// If <duration> <=0 means it does not expire.
// GetOrSetFunc returns the value of <key>, or sets <key> with result of function <f>
// and returns its result if <key> does not exist in the cache. The key-value pair expires
// after <duration>. It does not expire if <duration> <= 0.
func GetOrSetFunc(key interface{}, f func() interface{}, duration time.Duration) interface{} {
return cache.GetOrSetFunc(key, f, duration)
}

// GetOrSetFuncLock returns the value of <key>,
// or sets <key> with result of function <f> and returns its result
// if <key> does not exist in the cache.
// The key-value pair expires after <duration>.
//
// The parameter <duration> can be either type of int or time.Duration.
// If <duration> is type of int, it means <duration> milliseconds.
// If <duration> <=0 means it does not expire.
// GetOrSetFuncLock returns the value of <key>, or sets <key> with result of function <f>
// and returns its result if <key> does not exist in the cache. The key-value pair expires
// after <duration>. It does not expire if <duration> <= 0.
//
// Note that the function <f> is executed within writing mutex lock.
func GetOrSetFuncLock(key interface{}, f func() interface{}, duration time.Duration) interface{} {
Expand Down
50 changes: 13 additions & 37 deletions os/gcache/gcache_mem_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ func (c *memCache) getOrNewExpireSet(expire int64) (expireSet *gset.Set) {

// Set sets cache with <key>-<value> pair, which is expired after <duration>.
//
// The parameter <duration> can be either type of int or time.Duration.
// If <duration> is type of int, it means <duration> milliseconds.
// If <duration> <=0 means it does not expire.
// It does not expire if <duration> <= 0.
func (c *memCache) Set(key interface{}, value interface{}, duration time.Duration) {
expireTime := c.getInternalExpire(duration.Nanoseconds() / 1000000)
c.dataMu.Lock()
Expand All @@ -120,9 +118,7 @@ func (c *memCache) Set(key interface{}, value interface{}, duration time.Duratio
// doSetWithLockCheck sets cache with <key>-<value> pair if <key> does not exist in the cache,
// which is expired after <duration>.
//
// The parameter <duration> can be either type of int or time.Duration.
// If <duration> is type of int, it means <duration> milliseconds.
// If <duration> <=0 means it does not expire.
// It does not expire if <duration> <= 0.
//
// It doubly checks the <key> whether exists in the cache using mutex writing lock
// before setting it to the cache.
Expand Down Expand Up @@ -154,11 +150,7 @@ func (c *memCache) getInternalExpire(expire int64) int64 {
}

// SetIfNotExist sets cache with <key>-<value> pair if <key> does not exist in the cache,
// which is expired after <duration>.
//
// The parameter <duration> can be either type of int or time.Duration.
// If <duration> is type of int, it means <duration> milliseconds.
// If <duration> <=0 means it does not expire.
// which is expired after <duration>. It does not expire if <duration> <= 0.
func (c *memCache) SetIfNotExist(key interface{}, value interface{}, duration time.Duration) bool {
if !c.Contains(key) {
c.doSetWithLockCheck(key, value, duration)
Expand All @@ -169,9 +161,7 @@ func (c *memCache) SetIfNotExist(key interface{}, value interface{}, duration ti

// Sets batch sets cache with key-value pairs by <data>, which is expired after <duration>.
//
// The parameter <duration> can be either type of int or time.Duration.
// If <duration> is type of int, it means <duration> milliseconds.
// If <duration> <=0 means it does not expire.
// It does not expire if <duration> <= 0.
func (c *memCache) Sets(data map[interface{}]interface{}, duration time.Duration) {
expireTime := c.getInternalExpire(duration.Nanoseconds() / 1000000)
for k, v := range data {
Expand All @@ -198,13 +188,9 @@ func (c *memCache) Get(key interface{}) interface{} {
return nil
}

// GetOrSet returns the value of <key>,
// or sets <key>-<value> pair and returns <value> if <key> does not exist in the cache.
// The key-value pair expires after <duration>.
//
// The parameter <duration> can be either type of int or time.Duration.
// If <duration> is type of int, it means <duration> milliseconds.
// If <duration> <=0 means it does not expire.
// GetOrSet returns the value of <key>, or sets <key>-<value> pair and returns <value> if <key>
// does not exist in the cache. The key-value pair expires after <duration>. It does not expire
// if <duration> <= 0.
func (c *memCache) GetOrSet(key interface{}, value interface{}, duration time.Duration) interface{} {
if v := c.Get(key); v == nil {
return c.doSetWithLockCheck(key, value, duration)
Expand All @@ -213,14 +199,9 @@ func (c *memCache) GetOrSet(key interface{}, value interface{}, duration time.Du
}
}

// GetOrSetFunc returns the value of <key>,
// or sets <key> with result of function <f> and returns its result
// if <key> does not exist in the cache.
// The key-value pair expires after <duration>.
//
// The parameter <duration> can be either type of int or time.Duration.
// If <duration> is type of int, it means <duration> milliseconds.
// If <duration> <=0 means it does not expire.
// GetOrSetFunc returns the value of <key>, or sets <key> with result of function <f>
// and returns its result if <key> does not exist in the cache. The key-value pair expires
// after <duration>. It does not expire if <duration> <= 0.
func (c *memCache) GetOrSetFunc(key interface{}, f func() interface{}, duration time.Duration) interface{} {
if v := c.Get(key); v == nil {
return c.doSetWithLockCheck(key, f(), duration)
Expand All @@ -229,14 +210,9 @@ func (c *memCache) GetOrSetFunc(key interface{}, f func() interface{}, duration
}
}

// GetOrSetFuncLock returns the value of <key>,
// or sets <key> with result of function <f> and returns its result
// if <key> does not exist in the cache.
// The key-value pair expires after <duration>.
//
// The parameter <duration> can be either type of int or time.Duration.
// If <duration> is type of int, it means <duration> milliseconds.
// If <duration> <=0 means it does not expire.
// GetOrSetFuncLock returns the value of <key>, or sets <key> with result of function <f>
// and returns its result if <key> does not exist in the cache. The key-value pair expires
// after <duration>. It does not expire if <duration> <= 0.
//
// Note that the function <f> is executed within writing mutex lock.
func (c *memCache) GetOrSetFuncLock(key interface{}, f func() interface{}, duration time.Duration) interface{} {
Expand Down
1 change: 1 addition & 0 deletions os/gview/gview.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,6 @@ func New(path ...string) *View {
view.BindFunc("tolower", view.funcToLower)
view.BindFunc("nl2br", view.funcNl2Br)
view.BindFunc("include", view.funcInclude)
view.BindFunc("dump", view.funcDump)
return view
}
11 changes: 11 additions & 0 deletions os/gview/gview_buildin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package gview

import (
"fmt"
"github.com/gogf/gf/util/gutil"
"strings"

"github.com/gogf/gf/encoding/ghtml"
Expand All @@ -17,6 +18,16 @@ import (
"github.com/gogf/gf/util/gconv"
)

// funcDump implements build-in template function: dump
func (view *View) funcDump(values ...interface{}) (result string) {
result += "<!--\n"
for _, v := range values {
result += gutil.Export(v) + "\n"
}
result += "-->\n"
return result
}

// funcEq implements build-in template function: eq
func (view *View) funcEq(value interface{}, others ...interface{}) bool {
s := gconv.String(value)
Expand Down
4 changes: 2 additions & 2 deletions os/gview/gview_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ func (view *View) SetConfigWithMap(m map[string]interface{}) error {
return errors.New("configuration cannot be empty")
}
// Most common used configuration support for single view path.
k1, v1 := gutil.MapPossibleItemByKey(m, "paths")
_, v1 := gutil.MapPossibleItemByKey(m, "paths")
_, v2 := gutil.MapPossibleItemByKey(m, "path")
if v1 == nil && v2 != nil {
m[k1] = []interface{}{v2}
m["paths"] = []interface{}{v2}
}
config := Config{}
err := gconv.Struct(m, &config)
Expand Down
8 changes: 8 additions & 0 deletions os/gview/gview_doparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ func (view *View) Parse(file string, params ...Params) (result string, err error
return
}
item := r.(*fileCacheItem)
// It's not necessary continuing parsing if template content is empty.
if item.content == "" {
return "", nil
}
// Get the template object instance for <folder>.
tpl, err = view.getTemplate(item.folder, fmt.Sprintf(`*%s`, gfile.Ext(item.path)))
if err != nil {
Expand Down Expand Up @@ -145,6 +149,10 @@ func (view *View) ParseDefault(params ...Params) (result string, err error) {
// ParseContent parses given template content <content> with template variables <params>
// and returns the parsed content in []byte.
func (view *View) ParseContent(content string, params ...Params) (string, error) {
// It's not necessary continuing parsing if template content is empty.
if content == "" {
return "", nil
}
err := (error)(nil)
key := fmt.Sprintf("%s_%v", gCONTENT_TEMPLATE_NAME, view.delimiters)
tpl := templates.GetOrSetFuncLock(key, func() interface{} {
Expand Down
2 changes: 1 addition & 1 deletion util/gconv/gconv_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ func MapToMapDeep(params interface{}, pointer interface{}, mapping ...map[string
return doMapToMap(params, pointer, true, mapping...)
}

// doMapStruct converts map type variable <params> to another map type variable <pointer>.
// doMapToMap converts map type variable <params> to another map type variable <pointer>.
// The elements of <pointer> should be type of *map.
func doMapToMap(params interface{}, pointer interface{}, deep bool, mapping ...map[string]string) error {
paramsRv := reflect.ValueOf(params)
Expand Down

0 comments on commit 6ca5141

Please sign in to comment.