github.com/dbernstein1/tyk@v2.9.0-beta9-dl-apic+incompatible/regexp/cache.go (about) 1 package regexp 2 3 import ( 4 "regexp" 5 "time" 6 7 gocache "github.com/pmylund/go-cache" 8 ) 9 10 const ( 11 defaultCacheItemTTL = 60 * time.Second 12 defaultCacheCleanupInterval = 5 * time.Minute 13 14 maxKeySize = 1024 15 maxValueSize = 2048 16 ) 17 18 type cache struct { 19 *gocache.Cache 20 isEnabled bool 21 ttl time.Duration 22 } 23 24 func newCache(ttl time.Duration, isEnabled bool) *cache { 25 return &cache{ 26 Cache: gocache.New(ttl, defaultCacheCleanupInterval), 27 isEnabled: isEnabled, 28 ttl: ttl, 29 } 30 } 31 32 func (c *cache) enabled() bool { 33 return c.isEnabled && c.Cache != nil 34 } 35 36 func (c *cache) add(key string, value interface{}) { 37 c.Add(key, value, c.ttl) 38 } 39 40 func (c *cache) getRegexp(key string) (*regexp.Regexp, bool) { 41 if val, found := c.Get(key); found { 42 return val.(*regexp.Regexp).Copy(), true 43 } 44 45 return nil, false 46 } 47 48 func (c *cache) getString(key string) (string, bool) { 49 if val, found := c.Get(key); found { 50 return val.(string), true 51 } 52 53 return "", false 54 } 55 56 func (c *cache) getStrSlice(key string) ([]string, bool) { 57 if val, found := c.Get(key); found { 58 return val.([]string), true 59 } 60 61 return []string{}, false 62 } 63 64 func (c *cache) getStrSliceOfSlices(key string) ([][]string, bool) { 65 if val, found := c.Get(key); found { 66 return val.([][]string), true 67 } 68 69 return [][]string{}, false 70 } 71 72 func (c *cache) getBool(key string) (bool, bool) { 73 if val, found := c.Get(key); found { 74 return val.(bool), true 75 } 76 77 return false, false 78 } 79 80 func (c *cache) reset(ttl time.Duration, isEnabled bool) { 81 if c.Cache == nil { 82 return 83 } 84 85 c.isEnabled = isEnabled 86 c.ttl = ttl 87 c.Flush() 88 }