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

     1  package regexp
     2  
     3  import (
     4  	"regexp"
     5  	"time"
     6  )
     7  
     8  type regexpByteRetBoolCache struct {
     9  	*cache
    10  }
    11  
    12  func newRegexpByteRetBoolCache(ttl time.Duration, isEnabled bool) *regexpByteRetBoolCache {
    13  	return &regexpByteRetBoolCache{
    14  		cache: newCache(
    15  			ttl,
    16  			isEnabled,
    17  		),
    18  	}
    19  }
    20  
    21  func (c *regexpByteRetBoolCache) do(r *regexp.Regexp, b []byte, noCacheFn func([]byte) bool) bool {
    22  	// return if cache is not enabled
    23  	if !c.enabled() {
    24  		return noCacheFn(b)
    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()).AppendBytes(b).UnsafeKey()
    33  	if len(nsKey) > maxKeySize {
    34  		return noCacheFn(b)
    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(b)
    44  	c.add(kb.Key(), res)
    45  
    46  	return res
    47  }