-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.go
49 lines (38 loc) · 991 Bytes
/
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
package kash
import (
"time"
"math"
"fmt"
)
// Element example; https://golang.org/src/container/list/list_test.go
const MaxDuration = time.Nanosecond * math.MaxInt64
type Cache interface{}
type element struct {
AccessedAt time.Time
WriteAt time.Time
Value interface{}
Weight int64
}
func newElement(value interface{}) *element {
return newElementWithWeight(value, 1)
}
func newElementWithWeight(value interface{}, weight int64) *element {
return &element{
AccessedAt: time.Now().UTC(),
WriteAt: time.Now().UTC(),
Value: value,
Weight: weight,
}
}
func (e *element) AccessStale(now time.Time, ttl time.Duration) bool {
return e.AccessedAt.Before(now.Add(-1 * ttl))
}
func (e *element) WriteStale(now time.Time, ttl time.Duration) bool {
return e.WriteAt.Before(now.Add(-1 * ttl))
}
func (e *element) String() string {
return fmt.Sprintf("%+v %+v", e.Value, e.WriteAt)
}
func (e1 *element) Compare(e2 *element) bool {
return e1.Value == e2.Value
}