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

     1  package regexp
     2  
     3  import (
     4  	"regexp"
     5  	"time"
     6  )
     7  
     8  type regexpCache struct {
     9  	*cache
    10  	noCacheFunc func(string) (*regexp.Regexp, error)
    11  }
    12  
    13  func newRegexpCache(ttl time.Duration, isEnabled bool, fn func(string) (*regexp.Regexp, error)) *regexpCache {
    14  	return &regexpCache{
    15  		cache: newCache(
    16  			ttl,
    17  			isEnabled,
    18  		),
    19  		noCacheFunc: fn,
    20  	}
    21  }
    22  
    23  func (c *regexpCache) doNoCacheFunc(str string) (*Regexp, error) {
    24  	rx, err := c.noCacheFunc(str)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  
    29  	return &Regexp{
    30  			rx,
    31  			false,
    32  		},
    33  		nil
    34  }
    35  
    36  func (c *regexpCache) do(str string) (*Regexp, error) {
    37  	// return if cache is not enabled
    38  	if !c.enabled() {
    39  		return c.doNoCacheFunc(str)
    40  	}
    41  
    42  	// cache hit
    43  	if rx, found := c.getRegexp(str); found {
    44  		return &Regexp{
    45  				rx,
    46  				true,
    47  			},
    48  			nil
    49  	}
    50  
    51  	// cache miss, add to cache
    52  	regExp, err := c.doNoCacheFunc(str)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	c.add(str, regExp.Regexp)
    57  
    58  	return regExp, nil
    59  }