-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add caching logic for FindStringSubmatch in tyk/regexp
- Loading branch information
Showing
3 changed files
with
92 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package regexp | ||
|
||
import ( | ||
"regexp" | ||
"time" | ||
) | ||
|
||
type regexpStrRetSliceStrCache struct { | ||
*cache | ||
} | ||
|
||
func newRegexpStrRetSliceStrCache(ttl time.Duration, isEnabled bool) *regexpStrRetSliceStrCache { | ||
return ®expStrRetSliceStrCache{ | ||
cache: newCache( | ||
ttl, | ||
isEnabled, | ||
), | ||
} | ||
} | ||
|
||
func (c *regexpStrRetSliceStrCache) do(r *regexp.Regexp, s string, noCacheFn func(s string) []string) []string { | ||
// return if cache is not enabled | ||
if !c.enabled() { | ||
return noCacheFn(s) | ||
} | ||
|
||
// generate key, check key size | ||
key := r.String() + s | ||
if len(key) > maxKeySize { | ||
return noCacheFn(s) | ||
} | ||
|
||
// cache hit | ||
if res, found := c.getStrSlice(key); found { | ||
return res | ||
} | ||
|
||
// cache miss, add to cache if value is not too big | ||
res := noCacheFn(s) | ||
if len(res) <= maxValueSize { | ||
c.add(key, res) | ||
} | ||
|
||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters