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

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