Skip to content

Commit 01842a5

Browse files
committed
Use timevals
1 parent 1924ec3 commit 01842a5

File tree

2 files changed

+34
-15
lines changed

2 files changed

+34
-15
lines changed

cache.go

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

13-
var emptyTime = time.Time{}
14-
1514
type Item struct {
1615
Object interface{}
17-
Expiration int64
16+
Expiration syscall.Timeval
1817
}
1918

2019
// Returns true if the item has expired.
2120
func (item Item) Expired() bool {
22-
if item.Expiration == 0 {
21+
if item.Expiration.Sec == 0 {
2322
return false
2423
}
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)
2627
}
2728

2829
const (
@@ -59,12 +60,12 @@ func (c *cache) Set(k string, x interface{}, d time.Duration) {
5960
}
6061

6162
func (c *cache) set(k string, x interface{}, d time.Duration) {
62-
var e int64
63+
var e syscall.Timeval
6364
if d == DefaultExpiration {
6465
d = c.defaultExpiration
6566
}
6667
if d > 0 {
67-
e = time.Now().Add(d).UnixNano()
68+
e = syscall.NsecToTimeval(time.Now().Add(d).UnixNano())
6869
}
6970
c.items[k] = Item{
7071
Object: x,
@@ -106,20 +107,36 @@ func (c *cache) Get(k string) (interface{}, bool) {
106107
c.mu.RLock()
107108
// "Inlining" of get and expired
108109
item, found := c.items[k]
109-
if !found || (item.Expiration > 0 && time.Now().UnixNano() > item.Expiration) {
110+
if !found {
110111
c.mu.RUnlock()
111112
return nil, false
112113
}
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+
}
113122
c.mu.RUnlock()
114-
return item.Object, found
123+
return item.Object, true
115124
}
116125

117126
func (c *cache) get(k string) (interface{}, bool) {
118127
item, found := c.items[k]
119-
// "Inlining" of expired
120-
if !found || (item.Expiration > 0 && time.Now().UnixNano() > item.Expiration) {
128+
if !found {
121129
return nil, false
122130
}
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+
}
123140
return item.Object, true
124141
}
125142

@@ -874,11 +891,12 @@ type keyAndValue struct {
874891
// Delete all expired items from the cache.
875892
func (c *cache) DeleteExpired() {
876893
var evictedItems []keyAndValue
877-
now := time.Now().UnixNano()
894+
var now syscall.Timeval
895+
syscall.Gettimeofday(&now)
878896
c.mu.Lock()
879897
for k, v := range c.items {
880898
// "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)) {
882900
ov, evicted := c.delete(k)
883901
if evicted {
884902
evictedItems = append(evictedItems, keyAndValue{k, ov})

cache_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"runtime"
77
"strconv"
88
"sync"
9+
"syscall"
910
"testing"
1011
"time"
1112
)
@@ -110,11 +111,11 @@ func TestNewFrom(t *testing.T) {
110111
m := map[string]Item{
111112
"a": Item{
112113
Object: 1,
113-
Expiration: 0,
114+
Expiration: syscall.Timeval{},
114115
},
115116
"b": Item{
116117
Object: 2,
117-
Expiration: 0,
118+
Expiration: syscall.Timeval{},
118119
},
119120
}
120121
tc := NewFrom(DefaultExpiration, 0, m)

0 commit comments

Comments
 (0)