chore: improve GetQueryMap performance. (#1918)

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
This commit is contained in:
Bo-Yi Wu 2019-05-29 14:54:55 +08:00 committed by 田欧
parent 233a3e493d
commit 4b6df417e4

View File

@ -386,15 +386,17 @@ func (c *Context) QueryArray(key string) []string {
return values return values
} }
// GetQueryArray returns a slice of strings for a given query key, plus func (c *Context) getQueryCache() {
// a boolean value whether at least one value exists for the given key.
func (c *Context) GetQueryArray(key string) ([]string, bool) {
if c.queryCache == nil { if c.queryCache == nil {
c.queryCache = make(url.Values) c.queryCache = make(url.Values)
c.queryCache, _ = url.ParseQuery(c.Request.URL.RawQuery) c.queryCache, _ = url.ParseQuery(c.Request.URL.RawQuery)
} }
}
// GetQueryArray returns a slice of strings for a given query key, plus
// a boolean value whether at least one value exists for the given key.
func (c *Context) GetQueryArray(key string) ([]string, bool) {
c.getQueryCache()
if values, ok := c.queryCache[key]; ok && len(values) > 0 { if values, ok := c.queryCache[key]; ok && len(values) > 0 {
return values, true return values, true
} }
@ -410,7 +412,8 @@ func (c *Context) QueryMap(key string) map[string]string {
// GetQueryMap returns a map for a given query key, plus a boolean value // GetQueryMap returns a map for a given query key, plus a boolean value
// whether at least one value exists for the given key. // whether at least one value exists for the given key.
func (c *Context) GetQueryMap(key string) (map[string]string, bool) { func (c *Context) GetQueryMap(key string) (map[string]string, bool) {
return c.get(c.Request.URL.Query(), key) c.getQueryCache()
return c.get(c.queryCache, key)
} }
// PostForm returns the specified key from a POST urlencoded form or multipart form // PostForm returns the specified key from a POST urlencoded form or multipart form