Skip to content

Commit 2f0c74e

Browse files
committed
Use intermediary timevals
1 parent 01842a5 commit 2f0c74e

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

cache.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ import (
1313

1414
type Item struct {
1515
Object interface{}
16-
Expiration syscall.Timeval
16+
Expiration int64
1717
}
1818

1919
// Returns true if the item has expired.
2020
func (item Item) Expired() bool {
21-
if item.Expiration.Sec == 0 {
21+
if item.Expiration == 0 {
2222
return false
2323
}
2424
var tv syscall.Timeval
2525
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
2727
}
2828

2929
const (
@@ -60,12 +60,12 @@ func (c *cache) Set(k string, x interface{}, d time.Duration) {
6060
}
6161

6262
func (c *cache) set(k string, x interface{}, d time.Duration) {
63-
var e syscall.Timeval
63+
var e int64
6464
if d == DefaultExpiration {
6565
d = c.defaultExpiration
6666
}
6767
if d > 0 {
68-
e = syscall.NsecToTimeval(time.Now().Add(d).UnixNano())
68+
e = time.Now().Add(d).UnixNano()
6969
}
7070
c.items[k] = Item{
7171
Object: x,
@@ -105,16 +105,16 @@ func (c *cache) Replace(k string, x interface{}, d time.Duration) error {
105105
// whether the key was found.
106106
func (c *cache) Get(k string) (interface{}, bool) {
107107
c.mu.RLock()
108-
// "Inlining" of get and expired
108+
// "Inlining" of get and Expired
109109
item, found := c.items[k]
110110
if !found {
111111
c.mu.RUnlock()
112112
return nil, false
113113
}
114-
if item.Expiration.Sec > 0 {
114+
if item.Expiration > 0 {
115115
var tv syscall.Timeval
116116
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 {
118118
c.mu.RUnlock()
119119
return nil, false
120120
}
@@ -129,10 +129,10 @@ func (c *cache) get(k string) (interface{}, bool) {
129129
return nil, false
130130
}
131131
// "Inlining" of Expired
132-
if item.Expiration.Sec > 0 {
132+
if item.Expiration > 0 {
133133
var tv syscall.Timeval
134134
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 {
136136
c.mu.RUnlock()
137137
return nil, false
138138
}
@@ -890,13 +890,16 @@ type keyAndValue struct {
890890

891891
// Delete all expired items from the cache.
892892
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()
896899
c.mu.Lock()
897900
for k, v := range c.items {
898901
// "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 {
900903
ov, evicted := c.delete(k)
901904
if evicted {
902905
evictedItems = append(evictedItems, keyAndValue{k, ov})

cache_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"runtime"
77
"strconv"
88
"sync"
9-
"syscall"
109
"testing"
1110
"time"
1211
)
@@ -111,11 +110,11 @@ func TestNewFrom(t *testing.T) {
111110
m := map[string]Item{
112111
"a": Item{
113112
Object: 1,
114-
Expiration: syscall.Timeval{},
113+
Expiration: 0,
115114
},
116115
"b": Item{
117116
Object: 2,
118-
Expiration: syscall.Timeval{},
117+
Expiration: 0,
119118
},
120119
}
121120
tc := NewFrom(DefaultExpiration, 0, m)

0 commit comments

Comments
 (0)