mirror of
https://gitee.com/johng/gf
synced 2026-07-06 13:42:46 +08:00
some improving
This commit is contained in:
@ -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"
|
||||
|
||||
@ -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 == "" {
|
||||
|
||||
@ -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
|
||||
|
||||
@ -63,6 +63,7 @@ type (
|
||||
|
||||
// Router item just for dumping.
|
||||
RouterItem struct {
|
||||
Type int
|
||||
Middleware string
|
||||
Domain string
|
||||
Method string
|
||||
@ -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],
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
@ -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{} {
|
||||
|
||||
@ -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()
|
||||
@ -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.
|
||||
@ -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)
|
||||
@ -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 {
|
||||
@ -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)
|
||||
@ -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)
|
||||
@ -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{} {
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@ package gview
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/util/gutil"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/encoding/ghtml"
|
||||
@ -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)
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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 {
|
||||
@ -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{} {
|
||||
|
||||
@ -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)
|
||||
|
||||
Reference in New Issue
Block a user