@@ -13,17 +13,17 @@ import (
13
13
14
14
type Item struct {
15
15
Object interface {}
16
- Expiration syscall. Timeval
16
+ Expiration int64
17
17
}
18
18
19
19
// Returns true if the item has expired.
20
20
func (item Item ) Expired () bool {
21
- if item .Expiration . Sec == 0 {
21
+ if item .Expiration == 0 {
22
22
return false
23
23
}
24
24
var tv syscall.Timeval
25
25
syscall .Gettimeofday (& tv )
26
- return tv .Sec > item . Expiration . Sec || ( tv . Sec == item . Expiration . Sec && tv . Usec > item .Expiration . Usec )
26
+ return tv .Nano () > item .Expiration
27
27
}
28
28
29
29
const (
@@ -60,12 +60,12 @@ func (c *cache) Set(k string, x interface{}, d time.Duration) {
60
60
}
61
61
62
62
func (c * cache ) set (k string , x interface {}, d time.Duration ) {
63
- var e syscall. Timeval
63
+ var e int64
64
64
if d == DefaultExpiration {
65
65
d = c .defaultExpiration
66
66
}
67
67
if d > 0 {
68
- e = syscall . NsecToTimeval ( time .Now ().Add (d ).UnixNano () )
68
+ e = time .Now ().Add (d ).UnixNano ()
69
69
}
70
70
c .items [k ] = Item {
71
71
Object : x ,
@@ -105,16 +105,16 @@ func (c *cache) Replace(k string, x interface{}, d time.Duration) error {
105
105
// whether the key was found.
106
106
func (c * cache ) Get (k string ) (interface {}, bool ) {
107
107
c .mu .RLock ()
108
- // "Inlining" of get and expired
108
+ // "Inlining" of get and Expired
109
109
item , found := c .items [k ]
110
110
if ! found {
111
111
c .mu .RUnlock ()
112
112
return nil , false
113
113
}
114
- if item .Expiration . Sec > 0 {
114
+ if item .Expiration > 0 {
115
115
var tv syscall.Timeval
116
116
syscall .Gettimeofday (& tv )
117
- if tv .Sec > item . Expiration . Sec || ( tv . Sec == item . Expiration . Sec && tv . Usec > item .Expiration . Usec ) {
117
+ if tv .Nano () > item .Expiration {
118
118
c .mu .RUnlock ()
119
119
return nil , false
120
120
}
@@ -129,10 +129,10 @@ func (c *cache) get(k string) (interface{}, bool) {
129
129
return nil , false
130
130
}
131
131
// "Inlining" of Expired
132
- if item .Expiration . Sec > 0 {
132
+ if item .Expiration > 0 {
133
133
var tv syscall.Timeval
134
134
syscall .Gettimeofday (& tv )
135
- if tv .Sec > item . Expiration . Sec || ( tv . Sec == item . Expiration . Sec && tv . Usec > item .Expiration . Usec ) {
135
+ if tv .Nano () > item .Expiration {
136
136
c .mu .RUnlock ()
137
137
return nil , false
138
138
}
@@ -890,13 +890,16 @@ type keyAndValue struct {
890
890
891
891
// Delete all expired items from the cache.
892
892
func (c * cache ) DeleteExpired () {
893
- var evictedItems []keyAndValue
894
- var now syscall.Timeval
895
- syscall .Gettimeofday (& now )
893
+ var (
894
+ evictedItems []keyAndValue
895
+ tv syscall.Timeval
896
+ )
897
+ syscall .Gettimeofday (& tv )
898
+ now := tv .Nano ()
896
899
c .mu .Lock ()
897
900
for k , v := range c .items {
898
901
// "Inlining" of expired
899
- if v .Expiration . Sec > 0 && ( now . Sec > v .Expiration . Sec || ( now . Sec == v . Expiration . Sec && now . Usec > v . Expiration . Usec )) {
902
+ if v .Expiration > 0 && now > v .Expiration {
900
903
ov , evicted := c .delete (k )
901
904
if evicted {
902
905
evictedItems = append (evictedItems , keyAndValue {k , ov })
0 commit comments