浏览代码

chore: improve GetQueryMap performance. (#1918)

Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
Bo-Yi Wu 6 年之前
父节点
当前提交
4b6df417e4
共有 1 个文件被更改,包括 8 次插入5 次删除
  1. 8 5
      context.go

+ 8 - 5
context.go

@@ -386,15 +386,17 @@ func (c *Context) QueryArray(key string) []string {
 	return values
 }
 
-// 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) {
-
+func (c *Context) getQueryCache() {
 	if c.queryCache == nil {
 		c.queryCache = make(url.Values)
 		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 {
 		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
 // whether at least one value exists for the given key.
 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