Skip to content

Commit 2f6b517

Browse files
authored
Merge pull request #6 from HasMatthew/nanosecond_ttl
Use nanosecond-resolution TTL instead of second-resolution.
2 parents ddcff8e + 162d4e2 commit 2f6b517

File tree

5 files changed

+22
-20
lines changed

5 files changed

+22
-20
lines changed

bucket.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (b *bucket) get(key string) *Item {
1717
}
1818

1919
func (b *bucket) set(key string, value interface{}, duration time.Duration) (*Item, *Item) {
20-
expires := time.Now().Add(duration).Unix()
20+
expires := time.Now().Add(duration).UnixNano()
2121
item := newItem(key, value, expires)
2222
b.Lock()
2323
defer b.Unlock()

cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (c *Cache) Get(key string) *Item {
4747
if item == nil {
4848
return nil
4949
}
50-
if item.expires > time.Now().Unix() {
50+
if item.expires > time.Now().UnixNano() {
5151
c.promote(item)
5252
}
5353
return item

item.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,19 +85,19 @@ func (i *Item) Release() {
8585

8686
func (i *Item) Expired() bool {
8787
expires := atomic.LoadInt64(&i.expires)
88-
return expires < time.Now().Unix()
88+
return expires < time.Now().UnixNano()
8989
}
9090

9191
func (i *Item) TTL() time.Duration {
9292
expires := atomic.LoadInt64(&i.expires)
93-
return time.Second * time.Duration(expires-time.Now().Unix())
93+
return time.Nanosecond * time.Duration(expires-time.Now().UnixNano())
9494
}
9595

9696
func (i *Item) Expires() time.Time {
9797
expires := atomic.LoadInt64(&i.expires)
98-
return time.Unix(expires, 0)
98+
return time.Unix(0, expires)
9999
}
100100

101101
func (i *Item) Extend(duration time.Duration) {
102-
atomic.StoreInt64(&i.expires, time.Now().Add(duration).Unix())
102+
atomic.StoreInt64(&i.expires, time.Now().Add(duration).UnixNano())
103103
}

item_test.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package ccache
22

33
import (
4-
. "github.com/karlseguin/expect"
4+
"math"
55
"testing"
66
"time"
7+
8+
. "github.com/karlseguin/expect"
79
)
810

911
type ItemTests struct{}
@@ -19,29 +21,29 @@ func (_ *ItemTests) Promotability() {
1921
}
2022

2123
func (_ *ItemTests) Expired() {
22-
now := time.Now().Unix()
23-
item1 := &Item{expires: now + 1}
24-
item2 := &Item{expires: now - 1}
24+
now := time.Now().UnixNano()
25+
item1 := &Item{expires: now + (10 * int64(time.Millisecond))}
26+
item2 := &Item{expires: now - (10 * int64(time.Millisecond))}
2527
Expect(item1.Expired()).To.Equal(false)
2628
Expect(item2.Expired()).To.Equal(true)
2729
}
2830

2931
func (_ *ItemTests) TTL() {
30-
now := time.Now().Unix()
31-
item1 := &Item{expires: now + 10}
32-
item2 := &Item{expires: now - 10}
33-
Expect(item1.TTL()).To.Equal(time.Second * 10)
34-
Expect(item2.TTL()).To.Equal(time.Second * -10)
32+
now := time.Now().UnixNano()
33+
item1 := &Item{expires: now + int64(time.Second)}
34+
item2 := &Item{expires: now - int64(time.Second)}
35+
Expect(int(math.Ceil(item1.TTL().Seconds()))).To.Equal(1)
36+
Expect(int(math.Ceil(item2.TTL().Seconds()))).To.Equal(-1)
3537
}
3638

3739
func (_ *ItemTests) Expires() {
38-
now := time.Now().Unix()
39-
item := &Item{expires: now + 10}
40-
Expect(item.Expires().Unix()).To.Equal(now + 10)
40+
now := time.Now().UnixNano()
41+
item := &Item{expires: now + (10)}
42+
Expect(item.Expires().UnixNano()).To.Equal(now + 10)
4143
}
4244

4345
func (_ *ItemTests) Extend() {
44-
item := &Item{expires: time.Now().Unix() + 10}
46+
item := &Item{expires: time.Now().UnixNano() + 10}
4547
item.Extend(time.Minute * 2)
4648
Expect(item.Expires().Unix()).To.Equal(time.Now().Unix() + 120)
4749
}

layeredcache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (c *LayeredCache) Get(primary, secondary string) *Item {
5858
if item == nil {
5959
return nil
6060
}
61-
if item.expires > time.Now().Unix() {
61+
if item.expires > time.Now().UnixNano() {
6262
c.promote(item)
6363
}
6464
return item

0 commit comments

Comments
 (0)