@@ -7,22 +7,23 @@ import (
7
7
"os"
8
8
"runtime"
9
9
"sync"
10
+ "syscall"
10
11
"time"
11
12
)
12
13
13
- var emptyTime = time.Time {}
14
-
15
14
type Item struct {
16
15
Object interface {}
17
- Expiration int64
16
+ Expiration syscall. Timeval
18
17
}
19
18
20
19
// Returns true if the item has expired.
21
20
func (item Item ) Expired () bool {
22
- if item .Expiration == 0 {
21
+ if item .Expiration . Sec == 0 {
23
22
return false
24
23
}
25
- return time .Now ().UnixNano () > item .Expiration
24
+ var tv syscall.Timeval
25
+ syscall .Gettimeofday (& tv )
26
+ return tv .Sec > item .Expiration .Sec || (tv .Sec == item .Expiration .Sec && tv .Usec > item .Expiration .Usec )
26
27
}
27
28
28
29
const (
@@ -59,12 +60,12 @@ func (c *cache) Set(k string, x interface{}, d time.Duration) {
59
60
}
60
61
61
62
func (c * cache ) set (k string , x interface {}, d time.Duration ) {
62
- var e int64
63
+ var e syscall. Timeval
63
64
if d == DefaultExpiration {
64
65
d = c .defaultExpiration
65
66
}
66
67
if d > 0 {
67
- e = time .Now ().Add (d ).UnixNano ()
68
+ e = syscall . NsecToTimeval ( time .Now ().Add (d ).UnixNano () )
68
69
}
69
70
c .items [k ] = Item {
70
71
Object : x ,
@@ -106,20 +107,36 @@ func (c *cache) Get(k string) (interface{}, bool) {
106
107
c .mu .RLock ()
107
108
// "Inlining" of get and expired
108
109
item , found := c .items [k ]
109
- if ! found || ( item . Expiration > 0 && time . Now (). UnixNano () > item . Expiration ) {
110
+ if ! found {
110
111
c .mu .RUnlock ()
111
112
return nil , false
112
113
}
114
+ if item .Expiration .Sec > 0 {
115
+ var tv syscall.Timeval
116
+ syscall .Gettimeofday (& tv )
117
+ if tv .Sec > item .Expiration .Sec || (tv .Sec == item .Expiration .Sec && tv .Usec > item .Expiration .Usec ) {
118
+ c .mu .RUnlock ()
119
+ return nil , false
120
+ }
121
+ }
113
122
c .mu .RUnlock ()
114
- return item .Object , found
123
+ return item .Object , true
115
124
}
116
125
117
126
func (c * cache ) get (k string ) (interface {}, bool ) {
118
127
item , found := c .items [k ]
119
- // "Inlining" of expired
120
- if ! found || (item .Expiration > 0 && time .Now ().UnixNano () > item .Expiration ) {
128
+ if ! found {
121
129
return nil , false
122
130
}
131
+ // "Inlining" of Expired
132
+ if item .Expiration .Sec > 0 {
133
+ var tv syscall.Timeval
134
+ syscall .Gettimeofday (& tv )
135
+ if tv .Sec > item .Expiration .Sec || (tv .Sec == item .Expiration .Sec && tv .Usec > item .Expiration .Usec ) {
136
+ c .mu .RUnlock ()
137
+ return nil , false
138
+ }
139
+ }
123
140
return item .Object , true
124
141
}
125
142
@@ -874,11 +891,12 @@ type keyAndValue struct {
874
891
// Delete all expired items from the cache.
875
892
func (c * cache ) DeleteExpired () {
876
893
var evictedItems []keyAndValue
877
- now := time .Now ().UnixNano ()
894
+ var now syscall.Timeval
895
+ syscall .Gettimeofday (& now )
878
896
c .mu .Lock ()
879
897
for k , v := range c .items {
880
898
// "Inlining" of expired
881
- if v .Expiration > 0 && now > v .Expiration {
899
+ if v .Expiration . Sec > 0 && ( now . Sec > v .Expiration . Sec || ( now . Sec == v . Expiration . Sec && now . Usec > v . Expiration . Usec )) {
882
900
ov , evicted := c .delete (k )
883
901
if evicted {
884
902
evictedItems = append (evictedItems , keyAndValue {k , ov })
0 commit comments