From 0da04c6f172052583c54e152e98c85e585f01d41 Mon Sep 17 00:00:00 2001 From: "Frederick F. Kautz IV" Date: Wed, 29 Apr 2015 15:28:42 -0700 Subject: [PATCH] Expirations are now based on last accessed instead of time created --- pkg/server/server.go | 2 +- pkg/storage/drivers/memory/lru/lru.go | 15 +++++++++++++++ pkg/storage/drivers/memory/memory.go | 11 +++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/pkg/server/server.go b/pkg/server/server.go index 4d3338775..479441038 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -39,7 +39,7 @@ type MemoryFactory struct { // GetStartServerFunc builds memory api servers func (f MemoryFactory) GetStartServerFunc() StartServerFunc { return func() (chan<- string, <-chan error) { - _, _, driver := memory.Start(f.MaxMemory, 3*time.Hour) + _, _, driver := memory.Start(f.MaxMemory, 1*time.Hour) ctrl, status, _ := httpserver.Start(api.HTTPHandler(f.Domain, driver), f.Config) return ctrl, status } diff --git a/pkg/storage/drivers/memory/lru/lru.go b/pkg/storage/drivers/memory/lru/lru.go index e0c34a024..4dabcf72b 100644 --- a/pkg/storage/drivers/memory/lru/lru.go +++ b/pkg/storage/drivers/memory/lru/lru.go @@ -34,6 +34,7 @@ package lru import ( "container/list" + "sync" ) // Cache is an LRU cache. It is not safe for concurrent access. @@ -48,6 +49,7 @@ type Cache struct { ll *list.List cache map[interface{}]*list.Element + lock *sync.RWMutex } // A Key may be any value that is comparable. See http://golang.org/ref/spec#Comparison_operators @@ -66,11 +68,14 @@ func New(maxEntries int) *Cache { MaxEntries: maxEntries, ll: list.New(), cache: make(map[interface{}]*list.Element), + lock: &sync.RWMutex{}, } } // Add adds a value to the cache. func (c *Cache) Add(key Key, value interface{}) { + c.lock.Lock() + defer c.lock.Unlock() if c.cache == nil { c.cache = make(map[interface{}]*list.Element) c.ll = list.New() @@ -89,6 +94,8 @@ func (c *Cache) Add(key Key, value interface{}) { // Get looks up a key's value from the cache. func (c *Cache) Get(key Key) (value interface{}, ok bool) { + c.lock.Lock() + defer c.lock.Unlock() if c.cache == nil { return } @@ -101,6 +108,8 @@ func (c *Cache) Get(key Key) (value interface{}, ok bool) { // Remove removes the provided key from the cache. func (c *Cache) Remove(key Key) { + c.lock.Lock() + defer c.lock.Unlock() if c.cache == nil { return } @@ -111,6 +120,8 @@ func (c *Cache) Remove(key Key) { // RemoveOldest removes the oldest item from the cache. func (c *Cache) RemoveOldest() { + c.lock.Lock() + defer c.lock.Unlock() if c.cache == nil { return } @@ -122,6 +133,8 @@ func (c *Cache) RemoveOldest() { // GetOldest returns the oldest key, value, ok without modifying the lru func (c *Cache) GetOldest() (key Key, value interface{}, ok bool) { + c.lock.Lock() + defer c.lock.Unlock() if c.cache == nil { return nil, nil, false } @@ -143,6 +156,8 @@ func (c *Cache) removeElement(e *list.Element) { // Len returns the number of items in the cache. func (c *Cache) Len() int { + c.lock.Lock() + defer c.lock.Unlock() if c.cache == nil { return 0 } diff --git a/pkg/storage/drivers/memory/memory.go b/pkg/storage/drivers/memory/memory.go index 26c19d342..56c299275 100644 --- a/pkg/storage/drivers/memory/memory.go +++ b/pkg/storage/drivers/memory/memory.go @@ -123,6 +123,7 @@ func (memory *memoryDriver) GetObject(w io.Writer, bucket string, object string) dataSlice := data.([]byte) objectBuffer := bytes.NewBuffer(dataSlice) written, err := io.Copy(w, objectBuffer) + go memory.updateAccessTime(objectKey) return written, iodine.New(err, nil) } } @@ -282,6 +283,7 @@ func (memory *memoryDriver) CreateObject(bucket, key, contentType, expectedMD5Su Md5: md5Sum, Size: int64(totalLength), } + newObject.lastAccessed = time.Now() memory.lock.Lock() if _, ok := memory.objectMetadata[objectKey]; ok == true { memory.lock.Unlock() @@ -503,3 +505,12 @@ func (memory *memoryDriver) expireObjects() { } } } + +func (memory *memoryDriver) updateAccessTime(key string) { + memory.lock.Lock() + defer memory.lock.Unlock() + if object, ok := memory.objectMetadata[key]; ok { + object.lastAccessed = time.Now() + memory.objectMetadata[key] = object + } +}