Expirations are now based on last accessed instead of time created

master
Frederick F. Kautz IV 10 years ago
parent c8db3e1c3b
commit 0da04c6f17
  1. 2
      pkg/server/server.go
  2. 15
      pkg/storage/drivers/memory/lru/lru.go
  3. 11
      pkg/storage/drivers/memory/memory.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
}

@ -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
}

@ -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
}
}

Loading…
Cancel
Save