Use intermediary timevals

This commit is contained in:
Patrick Mylund Nielsen 2015-11-30 15:02:02 -05:00
parent 01842a547c
commit 2f0c74ebb8
2 changed files with 19 additions and 17 deletions

View File

@ -13,17 +13,17 @@ import (
type Item struct { type Item struct {
Object interface{} Object interface{}
Expiration syscall.Timeval Expiration int64
} }
// Returns true if the item has expired. // Returns true if the item has expired.
func (item Item) Expired() bool { func (item Item) Expired() bool {
if item.Expiration.Sec == 0 { if item.Expiration == 0 {
return false return false
} }
var tv syscall.Timeval var tv syscall.Timeval
syscall.Gettimeofday(&tv) syscall.Gettimeofday(&tv)
return tv.Sec > item.Expiration.Sec || (tv.Sec == item.Expiration.Sec && tv.Usec > item.Expiration.Usec) return tv.Nano() > item.Expiration
} }
const ( const (
@ -60,12 +60,12 @@ func (c *cache) Set(k string, x interface{}, d time.Duration) {
} }
func (c *cache) set(k string, x interface{}, d time.Duration) { func (c *cache) set(k string, x interface{}, d time.Duration) {
var e syscall.Timeval var e int64
if d == DefaultExpiration { if d == DefaultExpiration {
d = c.defaultExpiration d = c.defaultExpiration
} }
if d > 0 { if d > 0 {
e = syscall.NsecToTimeval(time.Now().Add(d).UnixNano()) e = time.Now().Add(d).UnixNano()
} }
c.items[k] = Item{ c.items[k] = Item{
Object: x, Object: x,
@ -105,16 +105,16 @@ func (c *cache) Replace(k string, x interface{}, d time.Duration) error {
// whether the key was found. // whether the key was found.
func (c *cache) Get(k string) (interface{}, bool) { func (c *cache) Get(k string) (interface{}, bool) {
c.mu.RLock() c.mu.RLock()
// "Inlining" of get and expired // "Inlining" of get and Expired
item, found := c.items[k] item, found := c.items[k]
if !found { if !found {
c.mu.RUnlock() c.mu.RUnlock()
return nil, false return nil, false
} }
if item.Expiration.Sec > 0 { if item.Expiration > 0 {
var tv syscall.Timeval var tv syscall.Timeval
syscall.Gettimeofday(&tv) syscall.Gettimeofday(&tv)
if tv.Sec > item.Expiration.Sec || (tv.Sec == item.Expiration.Sec && tv.Usec > item.Expiration.Usec) { if tv.Nano() > item.Expiration {
c.mu.RUnlock() c.mu.RUnlock()
return nil, false return nil, false
} }
@ -129,10 +129,10 @@ func (c *cache) get(k string) (interface{}, bool) {
return nil, false return nil, false
} }
// "Inlining" of Expired // "Inlining" of Expired
if item.Expiration.Sec > 0 { if item.Expiration > 0 {
var tv syscall.Timeval var tv syscall.Timeval
syscall.Gettimeofday(&tv) syscall.Gettimeofday(&tv)
if tv.Sec > item.Expiration.Sec || (tv.Sec == item.Expiration.Sec && tv.Usec > item.Expiration.Usec) { if tv.Nano() > item.Expiration {
c.mu.RUnlock() c.mu.RUnlock()
return nil, false return nil, false
} }
@ -890,13 +890,16 @@ type keyAndValue struct {
// Delete all expired items from the cache. // Delete all expired items from the cache.
func (c *cache) DeleteExpired() { func (c *cache) DeleteExpired() {
var evictedItems []keyAndValue var (
var now syscall.Timeval evictedItems []keyAndValue
syscall.Gettimeofday(&now) tv syscall.Timeval
)
syscall.Gettimeofday(&tv)
now := tv.Nano()
c.mu.Lock() c.mu.Lock()
for k, v := range c.items { for k, v := range c.items {
// "Inlining" of expired // "Inlining" of expired
if v.Expiration.Sec > 0 && (now.Sec > v.Expiration.Sec || (now.Sec == v.Expiration.Sec && now.Usec > v.Expiration.Usec)) { if v.Expiration > 0 && now > v.Expiration {
ov, evicted := c.delete(k) ov, evicted := c.delete(k)
if evicted { if evicted {
evictedItems = append(evictedItems, keyAndValue{k, ov}) evictedItems = append(evictedItems, keyAndValue{k, ov})

View File

@ -6,7 +6,6 @@ import (
"runtime" "runtime"
"strconv" "strconv"
"sync" "sync"
"syscall"
"testing" "testing"
"time" "time"
) )
@ -111,11 +110,11 @@ func TestNewFrom(t *testing.T) {
m := map[string]Item{ m := map[string]Item{
"a": Item{ "a": Item{
Object: 1, Object: 1,
Expiration: syscall.Timeval{}, Expiration: 0,
}, },
"b": Item{ "b": Item{
Object: 2, Object: 2,
Expiration: syscall.Timeval{}, Expiration: 0,
}, },
} }
tc := NewFrom(DefaultExpiration, 0, m) tc := NewFrom(DefaultExpiration, 0, m)