From 28748f60fdf5565bf98e627bb0b9021c36196396 Mon Sep 17 00:00:00 2001 From: "weicheng.dengos" Date: Wed, 8 Aug 2018 14:09:04 +0800 Subject: [PATCH] optimize: add cacheQuery for Request.URL.Query(); Change-Id: I1198254ec71ac38a750e2aacfd33f1111c450e76 --- context.go | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/context.go b/context.go index 724ded79..70fcfee4 100644 --- a/context.go +++ b/context.go @@ -57,6 +57,9 @@ type Context struct { // Accepted defines a list of manually accepted formats for content negotiation. Accepted []string + + // cacheQuery + cacheQuery url.Values } /************************************/ @@ -71,6 +74,11 @@ func (c *Context) reset() { c.Keys = nil c.Errors = c.Errors[0:0] c.Accepted = nil + c.cacheQuery = nil + + if c.Request != nil && c.Request.URL != nil { + c.cacheQuery = c.Request.URL.Query() + } } // Copy returns a copy of the current context that can be safely used outside the request's scope. @@ -81,6 +89,7 @@ func (c *Context) Copy() *Context { cp.Writer = &cp.writermem cp.index = abortIndex cp.handlers = nil + cp.cacheQuery = nil return &cp } @@ -354,7 +363,12 @@ func (c *Context) QueryArray(key string) []string { // 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) { - if values, ok := c.Request.URL.Query()[key]; ok && len(values) > 0 { + cacheQuery := c.cacheQuery + if cacheQuery == nil { + cacheQuery = c.Request.URL.Query() + } + + if values, ok := cacheQuery[key]; ok && len(values) > 0 { return values, true } return []string{}, false @@ -369,7 +383,12 @@ 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) + cacheQuery := c.cacheQuery + if cacheQuery == nil { + cacheQuery = c.Request.URL.Query() + } + + return c.get(cacheQuery, key) } // PostForm returns the specified key from a POST urlencoded form or multipart form