7
7
"os"
8
8
"runtime"
9
9
"sync"
10
+ "syscall"
10
11
"time"
11
12
)
12
13
@@ -20,7 +21,9 @@ func (item Item) Expired() bool {
20
21
if item .Expiration == 0 {
21
22
return false
22
23
}
23
- return time .Now ().UnixNano () > item .Expiration
24
+ var tv syscall.Timeval
25
+ syscall .Gettimeofday (& tv )
26
+ return tv .Nano () > item .Expiration
24
27
}
25
28
26
29
const (
@@ -102,22 +105,38 @@ func (c *cache) Replace(k string, x interface{}, d time.Duration) error {
102
105
// whether the key was found.
103
106
func (c * cache ) Get (k string ) (interface {}, bool ) {
104
107
c .mu .RLock ()
105
- // "Inlining" of get and expired
108
+ // "Inlining" of get and Expired
106
109
item , found := c .items [k ]
107
- if ! found || ( item . Expiration > 0 && time . Now (). UnixNano () > item . Expiration ) {
110
+ if ! found {
108
111
c .mu .RUnlock ()
109
112
return nil , false
110
113
}
114
+ if item .Expiration > 0 {
115
+ var tv syscall.Timeval
116
+ syscall .Gettimeofday (& tv )
117
+ if tv .Nano () > item .Expiration {
118
+ c .mu .RUnlock ()
119
+ return nil , false
120
+ }
121
+ }
111
122
c .mu .RUnlock ()
112
- return item .Object , found
123
+ return item .Object , true
113
124
}
114
125
115
126
func (c * cache ) get (k string ) (interface {}, bool ) {
116
127
item , found := c .items [k ]
117
- // "Inlining" of expired
118
- if ! found || (item .Expiration > 0 && time .Now ().UnixNano () > item .Expiration ) {
128
+ if ! found {
119
129
return nil , false
120
130
}
131
+ // "Inlining" of Expired
132
+ if item .Expiration > 0 {
133
+ var tv syscall.Timeval
134
+ syscall .Gettimeofday (& tv )
135
+ if tv .Nano () > item .Expiration {
136
+ c .mu .RUnlock ()
137
+ return nil , false
138
+ }
139
+ }
121
140
return item .Object , true
122
141
}
123
142
@@ -871,8 +890,12 @@ type keyAndValue struct {
871
890
872
891
// Delete all expired items from the cache.
873
892
func (c * cache ) DeleteExpired () {
874
- var evictedItems []keyAndValue
875
- now := time .Now ().UnixNano ()
893
+ var (
894
+ evictedItems []keyAndValue
895
+ tv syscall.Timeval
896
+ )
897
+ syscall .Gettimeofday (& tv )
898
+ now := tv .Nano ()
876
899
c .mu .Lock ()
877
900
for k , v := range c .items {
878
901
// "Inlining" of expired
0 commit comments