Documentation ¶
Overview ¶
Package lrure implements a thread-safe LRU cache for Go's standard regex package that complies with the recache.Cache interface.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
Cache is a thread-safe LRU cache for Go's standard regex package.
func New ¶
New returns a new LRU cache with the given capacity.
If capacity is less than 1, recache.DefaultCapacity is used instead.
func (*Cache) Capacity ¶
Capacity returns the maximum number of regular expressions that can be stored in the cache.
func (*Cache) Get ¶
Get returns a compiled regular expression from the cache given a pattern and an optional flag.
If the regular expression is not in the cache, it is compiled and added to it.
Example ¶
// Create a new Cache instance with the default cache capacity. cache := lrure.New(recache.DefaultCapacity) // Add the regular expression to the cache for the first time, which will // cause it to be compiled. regex, err := cache.Get(context.Background(), "p([a-z]+)ch", recache.DefaultFlag) if err != nil { log.Fatal(err) } // Match the string against the regular expression. fmt.Println(regex.MatchString("peach")) // Get the regular expression, which by now has been compiled and returns // super fast, without the need for recompilation. sameRegex, err := cache.Get(context.Background(), "p([a-z]+)ch", recache.DefaultFlag) if err != nil { log.Fatal(err) } // Match the string against the regular expression. fmt.Println(sameRegex.MatchString("peach"))
Output: true true
func (*Cache) SetCapacity ¶
SetCapacity sets the maximum number of regular expressions that can be stored in the cache.