github.com/dbernstein1/tyk@v2.9.0-beta9-dl-apic+incompatible/regexp/cache_regexp_str_ret_bool.go (about)

     1  package regexp
     2  
     3  import (
     4  	"regexp"
     5  	"time"
     6  )
     7  
     8  type regexpStrRetBoolCache struct {
     9  	*cache
    10  }
    11  
    12  func newRegexpStrRetBoolCache(ttl time.Duration, isEnabled bool) *regexpStrRetBoolCache {
    13  	return &regexpStrRetBoolCache{
    14  		cache: newCache(
    15  			ttl,
    16  			isEnabled,
    17  		),
    18  	}
    19  }
    20  
    21  func (c *regexpStrRetBoolCache) do(r *regexp.Regexp, s string, noCacheFn func(string) bool) bool {
    22  	// return if cache is not enabled
    23  	if !c.enabled() {
    24  		return noCacheFn(s)
    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(s).UnsafeKey()
    33  	if len(nsKey) > maxKeySize {
    34  		return noCacheFn(s)
    35  	}
    36  
    37  	// cache hit
    38  	if res, found := c.getBool(nsKey); found {
    39  		return res
    40  	}
    41  
    42  	// cache miss, add to cache
    43  	res := noCacheFn(s)
    44  	c.add(kb.Key(), res)
    45  
    46  	return res
    47  }