Skip to content

Commit f6cdd07

Browse files
committed
Merge branch 'timeval'
2 parents 2f60853 + 2f0c74e commit f6cdd07

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

cache.go

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"runtime"
99
"sync"
10+
"syscall"
1011
"time"
1112
)
1213

@@ -20,7 +21,9 @@ func (item Item) Expired() bool {
2021
if item.Expiration == 0 {
2122
return false
2223
}
23-
return time.Now().UnixNano() > item.Expiration
24+
var tv syscall.Timeval
25+
syscall.Gettimeofday(&tv)
26+
return tv.Nano() > item.Expiration
2427
}
2528

2629
const (
@@ -102,22 +105,38 @@ func (c *cache) Replace(k string, x interface{}, d time.Duration) error {
102105
// whether the key was found.
103106
func (c *cache) Get(k string) (interface{}, bool) {
104107
c.mu.RLock()
105-
// "Inlining" of get and expired
108+
// "Inlining" of get and Expired
106109
item, found := c.items[k]
107-
if !found || (item.Expiration > 0 && time.Now().UnixNano() > item.Expiration) {
110+
if !found {
108111
c.mu.RUnlock()
109112
return nil, false
110113
}
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+
}
111122
c.mu.RUnlock()
112-
return item.Object, found
123+
return item.Object, true
113124
}
114125

115126
func (c *cache) get(k string) (interface{}, bool) {
116127
item, found := c.items[k]
117-
// "Inlining" of expired
118-
if !found || (item.Expiration > 0 && time.Now().UnixNano() > item.Expiration) {
128+
if !found {
119129
return nil, false
120130
}
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+
}
121140
return item.Object, true
122141
}
123142

@@ -871,8 +890,12 @@ type keyAndValue struct {
871890

872891
// Delete all expired items from the cache.
873892
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()
876899
c.mu.Lock()
877900
for k, v := range c.items {
878901
// "Inlining" of expired

0 commit comments

Comments
 (0)