Skip to content

Commit

Permalink
chore: improve GetQueryMap performance. (gin-gonic#1918)
Browse files Browse the repository at this point in the history
Signed-off-by: Bo-Yi Wu <[email protected]>
  • Loading branch information
appleboy authored and thinkerou committed May 29, 2019
1 parent 233a3e4 commit 4b6df41
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand Down

0 comments on commit 4b6df41

Please sign in to comment.