Merge branch 'timeval'

master
Patrick Mylund Nielsen 9 years ago
commit f6cdd07cbb

@ -7,6 +7,7 @@ import (
"os" "os"
"runtime" "runtime"
"sync" "sync"
"syscall"
"time" "time"
) )
@ -20,7 +21,9 @@ func (item Item) Expired() bool {
if item.Expiration == 0 { if item.Expiration == 0 {
return false return false
} }
return time.Now().UnixNano() > item.Expiration var tv syscall.Timeval
syscall.Gettimeofday(&tv)
return tv.Nano() > item.Expiration
} }
const ( const (
@ -102,22 +105,38 @@ 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 || (item.Expiration > 0 && time.Now().UnixNano() > item.Expiration) { if !found {
c.mu.RUnlock()
return nil, false
}
if item.Expiration > 0 {
var tv syscall.Timeval
syscall.Gettimeofday(&tv)
if tv.Nano() > item.Expiration {
c.mu.RUnlock() c.mu.RUnlock()
return nil, false return nil, false
} }
}
c.mu.RUnlock() c.mu.RUnlock()
return item.Object, found return item.Object, true
} }
func (c *cache) get(k string) (interface{}, bool) { func (c *cache) get(k string) (interface{}, bool) {
item, found := c.items[k] item, found := c.items[k]
// "Inlining" of expired if !found {
if !found || (item.Expiration > 0 && time.Now().UnixNano() > item.Expiration) { return nil, false
}
// "Inlining" of Expired
if item.Expiration > 0 {
var tv syscall.Timeval
syscall.Gettimeofday(&tv)
if tv.Nano() > item.Expiration {
c.mu.RUnlock()
return nil, false return nil, false
} }
}
return item.Object, true return item.Object, true
} }
@ -871,8 +890,12 @@ 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 (
now := time.Now().UnixNano() evictedItems []keyAndValue
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

Loading…
Cancel
Save