Back to weekly

This commit is contained in:
Patrick Mylund Nielsen 2012-01-04 09:11:27 +01:00
parent 62a061f1f4
commit 9e6775b151
2 changed files with 26 additions and 36 deletions

View File

@ -2,7 +2,6 @@ package cache
import ( import (
"fmt" "fmt"
"os"
"reflect" "reflect"
"runtime" "runtime"
"sync" "sync"
@ -92,7 +91,7 @@ type Cache struct {
} }
type cache struct { type cache struct {
DefaultExpiration int64 DefaultExpiration time.Duration
Items map[string]*Item Items map[string]*Item
mu *sync.Mutex mu *sync.Mutex
janitor *janitor janitor *janitor
@ -100,30 +99,30 @@ type cache struct {
type Item struct { type Item struct {
Object interface{} Object interface{}
Expiration *int64 Expiration *time.Time
} }
type janitor struct { type janitor struct {
Interval int64 Interval time.Duration
stop chan bool stop chan bool
} }
// Adds an item to the cache, replacing any existing item. If the duration is 0, the // Adds an item to the cache, replacing any existing item. If the duration is 0, the
// cache's default expiration time is used. If it is -1, the item never expires. // cache's default expiration time is used. If it is -1, the item never expires.
func (c *cache) Set(k string, x interface{}, d int64) { func (c *cache) Set(k string, x interface{}, d time.Duration) {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
c.set(k, x, d) c.set(k, x, d)
} }
func (c *cache) set(k string, x interface{}, d int64) { func (c *cache) set(k string, x interface{}, d time.Duration) {
var e *int64 var e *time.Time
if d == 0 { if d == 0 {
d = c.DefaultExpiration d = c.DefaultExpiration
} }
if d > 0 { if d > 0 {
t := time.Nanoseconds() + d t := time.Now().Add(d)
e = &t e = &t
} }
c.Items[k] = &Item{ c.Items[k] = &Item{
@ -133,8 +132,8 @@ func (c *cache) set(k string, x interface{}, d int64) {
} }
// Adds an item to the cache only if an item doesn't already exist for the given key, // Adds an item to the cache only if an item doesn't already exist for the given key,
// or if the existing item has expired. Returns an os.Error if not. // or if the existing item has expired. Returns an error if not.
func (c *cache) Add(k string, x interface{}, d int64) os.Error { func (c *cache) Add(k string, x interface{}, d time.Duration) error {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
@ -146,9 +145,9 @@ func (c *cache) Add(k string, x interface{}, d int64) os.Error {
return nil return nil
} }
// Sets a new value for the cache item only if it already exists. Returns an os.Error if // Sets a new value for the cache item only if it already exists. Returns an error if
// it does not. // it does not.
func (c *cache) Replace(k string, x interface{}, d int64) os.Error { func (c *cache) Replace(k string, x interface{}, d time.Duration) error {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
@ -182,10 +181,10 @@ func (c *cache) get(k string) (interface{}, bool) {
} }
// Increment an item of type int, int8, int16, int32, int64, uintptr, uint, uint8, // Increment an item of type int, int8, int16, int32, int64, uintptr, uint, uint8,
// uint32, uint64, float32 or float64 by n. Returns an os.Error if the item's value is // uint32, uint64, float32 or float64 by n. Returns an error if the item's value is
// not an integer, if it was not found, or if it is not possible to increment it by // not an integer, if it was not found, or if it is not possible to increment it by
// n. Passing a negative number will cause the item to be decremented. // n. Passing a negative number will cause the item to be decremented.
func (c *cache) IncrementFloat(k string, n float64) os.Error { func (c *cache) IncrementFloat(k string, n float64) error {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock() defer c.mu.Unlock()
@ -229,18 +228,18 @@ func (c *cache) IncrementFloat(k string, n float64) os.Error {
} }
// Increment an item of type int, int8, int16, int32, int64, uintptr, uint, uint8, // Increment an item of type int, int8, int16, int32, int64, uintptr, uint, uint8,
// uint32, or uint64, float32 or float64 by n. Returns an os.Error if the item's value // uint32, or uint64, float32 or float64 by n. Returns an error if the item's value
// is not an integer, if it was not found, or if it is not possible to increment it // is not an integer, if it was not found, or if it is not possible to increment it
// by n. Passing a negative number will cause the item to be decremented. // by n. Passing a negative number will cause the item to be decremented.
func (c *cache) Increment(k string, n int64) os.Error { func (c *cache) Increment(k string, n int64) error {
return c.IncrementFloat(k, float64(n)) return c.IncrementFloat(k, float64(n))
} }
// Decrement an item of type int, int8, int16, int32, int64, uintptr, uint, uint8, // Decrement an item of type int, int8, int16, int32, int64, uintptr, uint, uint8,
// uint32, or uint64, float32 or float64 by n. Returns an os.Error if the item's value // uint32, or uint64, float32 or float64 by n. Returns an error if the item's value
// is not an integer, if it was not found, or if it is not possible to decrement it // is not an integer, if it was not found, or if it is not possible to decrement it
// by n. // by n.
func (c *cache) Decrement(k string, n int64) os.Error { func (c *cache) Decrement(k string, n int64) error {
return c.Increment(k, n*-1) return c.Increment(k, n*-1)
} }
@ -253,7 +252,7 @@ func (c *cache) Delete(k string) {
} }
func (c *cache) delete(k string) { func (c *cache) delete(k string) {
c.Items[k] = nil, false delete(c.Items, k)
} }
// Deletes all expired items from the cache. // Deletes all expired items from the cache.
@ -281,7 +280,7 @@ func (i *Item) Expired() bool {
if i.Expiration == nil { if i.Expiration == nil {
return false return false
} }
return *i.Expiration < time.Nanoseconds() return i.Expiration.Before(time.Now())
} }
func (j *janitor) Run(c *cache) { func (j *janitor) Run(c *cache) {
@ -310,7 +309,7 @@ func stopJanitor(c *Cache) {
// expire and must be deleted manually. If the cleanup interval is less than one, // expire and must be deleted manually. If the cleanup interval is less than one,
// expired items are not deleted from the cache before their next lookup or before // expired items are not deleted from the cache before their next lookup or before
// calling DeleteExpired. // calling DeleteExpired.
func New(de, ci int64) *Cache { func New(de, ci time.Duration) *Cache {
if de == 0 { if de == 0 {
de = -1 de = -1
} }

View File

@ -5,15 +5,6 @@ import (
"time" "time"
) )
const (
Nanosecond = 1
Microsecond = 1000 * Nanosecond
Millisecond = 1000 * Microsecond
Second = 1000 * Millisecond
Minute = 60 * Second
Hour = 60 * Minute
)
func TestCache(t *testing.T) { func TestCache(t *testing.T) {
tc := New(0, 0) tc := New(0, 0)
@ -70,19 +61,19 @@ func TestCache(t *testing.T) {
func TestCacheTimes(t *testing.T) { func TestCacheTimes(t *testing.T) {
var found bool var found bool
tc := New(50*Millisecond, 1*Millisecond) tc := New(50*time.Millisecond, 1*time.Millisecond)
tc.Set("a", 1, 0) tc.Set("a", 1, 0)
tc.Set("b", 2, -1) tc.Set("b", 2, -1)
tc.Set("c", 3, 20*Millisecond) tc.Set("c", 3, 20*time.Millisecond)
tc.Set("d", 4, 70*Millisecond) tc.Set("d", 4, 70*time.Millisecond)
<-time.After(25 * Millisecond) <-time.After(25 * time.Millisecond)
_, found = tc.Get("c") _, found = tc.Get("c")
if found { if found {
t.Error("Found c when it should have been automatically deleted") t.Error("Found c when it should have been automatically deleted")
} }
<-time.After(30 * Millisecond) <-time.After(30 * time.Millisecond)
_, found = tc.Get("a") _, found = tc.Get("a")
if found { if found {
t.Error("Found a when it should have been automatically deleted") t.Error("Found a when it should have been automatically deleted")
@ -98,7 +89,7 @@ func TestCacheTimes(t *testing.T) {
t.Error("Did not find d even though it was set to expire later than the default") t.Error("Did not find d even though it was set to expire later than the default")
} }
<-time.After(20 * Millisecond) <-time.After(20 * time.Millisecond)
_, found = tc.Get("d") _, found = tc.Get("d")
if found { if found {
t.Error("Found d when it should have been automatically deleted (later than the default)") t.Error("Found d when it should have been automatically deleted (later than the default)")