From 4b6df417e4bda80a698a64aa085779a7ad1269c0 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Wed, 29 May 2019 14:54:55 +0800 Subject: [PATCH] chore: improve GetQueryMap performance. (#1918) Signed-off-by: Bo-Yi Wu --- context.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/context.go b/context.go index 64810d54..71337acd 100644 --- a/context.go +++ b/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