github.com/dbernstein1/tyk@v2.9.0-beta9-dl-apic+incompatible/regexp/cache_regexp_str_func_ret_str.go (about) 1 package regexp 2 3 import ( 4 "regexp" 5 "time" 6 ) 7 8 type regexpStrFuncRetStrCache struct { 9 *cache 10 } 11 12 func newRegexpStrFuncRetStrCache(ttl time.Duration, isEnabled bool) *regexpStrFuncRetStrCache { 13 return ®expStrFuncRetStrCache{ 14 cache: newCache( 15 ttl, 16 isEnabled, 17 ), 18 } 19 } 20 21 func (c *regexpStrFuncRetStrCache) do(r *regexp.Regexp, src string, repl func(string) string, noCacheFn func(string, func(string) string) string) string { 22 // return if cache is not enabled 23 if !c.enabled() { 24 return noCacheFn(src, repl) 25 } 26 27 kb := keyBuilderPool.Get().(*keyBuilder) 28 defer keyBuilderPool.Put(kb) 29 kb.Reset() 30 31 // generate key, check key size 32 nsKey := kb.AppendString(r.String()).AppendString(src).Appendf("%p", repl).UnsafeKey() 33 if len(nsKey) > maxKeySize { 34 return noCacheFn(src, repl) 35 } 36 37 // cache hit 38 if res, found := c.getString(nsKey); found { 39 return res 40 } 41 42 // cache miss, add to cache if value is not too big 43 res := noCacheFn(src, repl) 44 if len(res) > maxValueSize { 45 return res 46 } 47 48 c.add(kb.Key(), res) 49 50 return res 51 }