-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.go
52 lines (42 loc) · 1.01 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
package gocached
import "time"
type Entry[K comparable, V any] struct {
Key K
Value V
Written time.Time
Options EntryOptions
}
func (entry *Entry[K, V]) IsOutdated() bool {
if entry.IsPastExpiryDate() || entry.IsPastTTL() {
return true
}
return false
}
func (entry *Entry[K, V]) IsPastExpiryDate() bool {
return entry.Options.ExpiryDate != nil && time.Now().After(*entry.Options.ExpiryDate)
}
func (entry *Entry[K, V]) IsPastTTL() bool {
return entry.Options.TTL != nil && time.Now().Sub(entry.Written) > *entry.Options.TTL
}
type EntryOptions struct {
ExpiryDate *time.Time
TTL *time.Duration
}
type EntryFn func(*EntryOptions)
func makeEntryOptions(opts []EntryFn) EntryOptions {
options := EntryOptions{}
for _, optFn := range opts {
optFn(&options)
}
return options
}
func WithTTL(ttl time.Duration) EntryFn {
return func(opts *EntryOptions) {
opts.TTL = &ttl
}
}
func WithExpiryDate(expiryDate time.Time) EntryFn {
return func(opts *EntryOptions) {
opts.ExpiryDate = &expiryDate
}
}