-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.go
56 lines (45 loc) · 1.16 KB
/
entry.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
package recache
import (
"regexp"
"sync/atomic"
"git.sr.ht/~jamesponddotco/xstd-go/xerrors"
)
// ErrInvalidEntry is returned when the entry in the cache is invalid.
const ErrInvalidEntry xerrors.Error = "invalid entry"
// Entry represents an item in the cache.
type Entry struct {
regex *regexp.Regexp
pattern string
key string
frequency atomic.Uint64
}
// NewEntry creates a new entry in the cache.
func NewEntry(key, pattern string, regex *regexp.Regexp) *Entry {
if pattern == "" || regex == nil {
return nil
}
return &Entry{
regex: regex,
pattern: pattern,
key: key,
frequency: atomic.Uint64{},
}
}
// Load returns the compiled regex and pattern, and increments the frequency of
// the entry by one.
func (e *Entry) Load() (*regexp.Regexp, string, error) {
e.frequency.Add(1)
return e.regex, e.pattern, nil
}
// Pattern returns the entry's pattern.
func (e *Entry) Pattern() string {
return e.pattern
}
// Key returns the entry's cache key.
func (e *Entry) Key() string {
return e.key
}
// Frequency returns the number of times the entry has been loaded.
func (e *Entry) Frequency() uint64 {
return e.frequency.Load()
}