Skip to content

Commit eb4f9f6

Browse files
committed
Use UnixNano int64s instead of Time
1 parent 31c7be0 commit eb4f9f6

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

cache.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ var emptyTime = time.Time{}
1414

1515
type Item struct {
1616
Object interface{}
17-
Expiration time.Time
17+
Expiration int64
1818
}
1919

20-
func (item Item) expired(now time.Time) bool {
21-
if item.Expiration == emptyTime {
20+
func (item Item) expired(now int64) bool {
21+
if item.Expiration == 0 {
2222
return false
2323
}
24-
return item.Expiration.Before(now)
24+
return now > item.Expiration
2525
}
2626

2727
// Returns true if the item has expired.
2828
func (item Item) Expired() bool {
2929
// "Inlining" of expired
30-
if item.Expiration == emptyTime {
30+
if item.Expiration == 0 {
3131
return false
3232
}
33-
return item.Expiration.Before(time.Now())
33+
return time.Now().UnixNano() > item.Expiration
3434
}
3535

3636
const (
@@ -67,12 +67,12 @@ func (c *cache) Set(k string, x interface{}, d time.Duration) {
6767
}
6868

6969
func (c *cache) set(k string, x interface{}, d time.Duration) {
70-
e := emptyTime
70+
var e int64
7171
if d == DefaultExpiration {
7272
d = c.defaultExpiration
7373
}
7474
if d > 0 {
75-
e = time.Now().Add(d)
75+
e = time.Now().Add(d).UnixNano()
7676
}
7777
c.items[k] = Item{
7878
Object: x,
@@ -881,7 +881,7 @@ type keyAndValue struct {
881881
// Delete all expired items from the cache.
882882
func (c *cache) DeleteExpired() {
883883
var evictedItems []keyAndValue
884-
now := time.Now()
884+
now := time.Now().UnixNano()
885885
c.mu.Lock()
886886
for k, v := range c.items {
887887
if v.expired(now) {

cache_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,11 @@ func TestNewFrom(t *testing.T) {
110110
m := map[string]Item{
111111
"a": Item{
112112
Object: 1,
113-
Expiration: emptyTime,
113+
Expiration: 0,
114114
},
115115
"b": Item{
116116
Object: 2,
117-
Expiration: emptyTime,
117+
Expiration: 0,
118118
},
119119
}
120120
tc := NewFrom(DefaultExpiration, 0, m)

0 commit comments

Comments
 (0)