-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
64 lines (52 loc) · 1.56 KB
/
cache.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package recache
import (
"context"
"regexp"
)
const (
// DefaultFlag is the default flag used when compiling regular expressions.
DefaultFlag Flag = 0
// FlagPOSIX specifies that the regular expression should be restricted to
// POSIX syntax.
FlagPOSIX Flag = 1 << iota
// FlagMust specifies that the regular expression should be compiled using
// MustCompile.
FlagMust
// FlagMustPOSIX is like Must but restricts the regular expression to POSIX
// syntax.
FlagMustPOSIX = FlagMust | FlagPOSIX
)
// Flag controls the behavior of the Get method when compiling regular
// expressions.
type Flag int
// String returns a string representation of the flag.
func (f Flag) String() string {
switch f {
case FlagPOSIX:
return "POSIX"
case FlagMust:
return "Must"
case FlagMustPOSIX:
return "MustPOSIX"
default:
return "Default"
}
}
// Cache is a storage mechanism used to store and retrieve compiled regular
// expressions for improved performance.
type Cache interface {
// Get returns a compiled regular expression from the cache given a pattern
// and an optional flag.
Get(ctx context.Context, pattern string, flag Flag) (*regexp.Regexp, error)
// SetCapacity sets the maximum number of regular expressions that can be
// stored in the cache.
SetCapacity(capacity int) error
// Capacity returns the maximum number of regular expressions that can be
// stored in the cache.
Capacity() int
// Size returns the number of regular expressions currently stored in the
// cache.
Size() int
// Clear removes all regular expressions from the cache.
Clear()
}