sm
/
cache
1
0
Fork 0

Various fixes for upstream acceptance, and formatting

Removes the ctime tracking and access. Reverts NewFrom() changes from
upstream, and adds NewFromWithLRU() in place of it.
This commit is contained in:
M 2017-04-18 12:40:40 -04:00
parent 378662d53f
commit d8d9d6491e
1 changed files with 179 additions and 125 deletions

158
cache.go
View File

@ -13,7 +13,6 @@ import (
type Item struct {
Object interface{}
Expiration *time.Time
Ctime *time.Time
Atime *time.Time
}
@ -30,16 +29,12 @@ func (item *Item) LastAccessed() *time.Time {
return item.Atime
}
// Set the Atime
func (item *Item) Accessed() {
now := time.Now()
item.Atime = &now
}
// Return the Ctime
func (item *Item) Created() *time.Time {
return item.Ctime
}
const (
// For use with functions that take an expiration time.
NoExpiration time.Duration = -1
@ -75,29 +70,23 @@ func (c *cache) Set(k string, x interface{}, d time.Duration) {
func (c *cache) set(k string, x interface{}, d time.Duration) {
var e *time.Time
t := time.Now()
at := &t
ct := &t
if d == DefaultExpiration {
d = c.defaultExpiration
}
if d > 0 {
t = time.Now().Add(d)
t := time.Now().Add(d)
e = &t
}
if c.maxItems > 0 {
// We don't want to hit the expiration if we're updating.
_, found := c.get(k)
if found {
// Update means change the Ctime
ct = c.items[k].Created()
}
// Using maxItems (LRU)
now := time.Now()
at := &now
c.items[k] = &Item{
Object: x,
Expiration: e,
Ctime: ct,
Atime: at,
}
} else {
@ -152,7 +141,9 @@ func (c *cache) get(k string) (interface{}, bool) {
return nil, false
}
if c.maxItems > 0 { item.Accessed() }
if c.maxItems > 0 {
item.Accessed()
}
return item.Object, true
}
@ -170,7 +161,9 @@ func (c *cache) Increment(k string, n int64) error {
return fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
switch v.Object.(type) {
case int:
@ -220,7 +213,9 @@ func (c *cache) IncrementFloat(k string, n float64) error {
return fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
switch v.Object.(type) {
case float32:
@ -246,7 +241,9 @@ func (c *cache) IncrementInt(k string, n int) (int, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(int)
if !ok {
@ -270,7 +267,9 @@ func (c *cache) IncrementInt8(k string, n int8) (int8, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(int8)
if !ok {
@ -294,7 +293,9 @@ func (c *cache) IncrementInt16(k string, n int16) (int16, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(int16)
if !ok {
@ -318,7 +319,9 @@ func (c *cache) IncrementInt32(k string, n int32) (int32, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(int32)
if !ok {
@ -342,7 +345,9 @@ func (c *cache) IncrementInt64(k string, n int64) (int64, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(int64)
if !ok {
@ -366,7 +371,9 @@ func (c *cache) IncrementUint(k string, n uint) (uint, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(uint)
if !ok {
@ -390,7 +397,9 @@ func (c *cache) IncrementUintptr(k string, n uintptr) (uintptr, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(uintptr)
if !ok {
@ -414,7 +423,9 @@ func (c *cache) IncrementUint8(k string, n uint8) (uint8, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(uint8)
if !ok {
@ -438,7 +449,9 @@ func (c *cache) IncrementUint16(k string, n uint16) (uint16, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(uint16)
if !ok {
@ -462,7 +475,9 @@ func (c *cache) IncrementUint32(k string, n uint32) (uint32, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(uint32)
if !ok {
@ -486,7 +501,9 @@ func (c *cache) IncrementUint64(k string, n uint64) (uint64, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(uint64)
if !ok {
@ -510,7 +527,9 @@ func (c *cache) IncrementFloat32(k string, n float32) (float32, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(float32)
if !ok {
@ -534,7 +553,9 @@ func (c *cache) IncrementFloat64(k string, n float64) (float64, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(float64)
if !ok {
@ -562,7 +583,9 @@ func (c *cache) Decrement(k string, n int64) error {
return fmt.Errorf("Item not found")
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
switch v.Object.(type) {
case int:
@ -612,7 +635,9 @@ func (c *cache) DecrementFloat(k string, n float64) error {
return fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
switch v.Object.(type) {
case float32:
@ -638,7 +663,9 @@ func (c *cache) DecrementInt(k string, n int) (int, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(int)
if !ok {
@ -662,7 +689,9 @@ func (c *cache) DecrementInt8(k string, n int8) (int8, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(int8)
if !ok {
@ -686,7 +715,9 @@ func (c *cache) DecrementInt16(k string, n int16) (int16, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(int16)
if !ok {
@ -710,7 +741,9 @@ func (c *cache) DecrementInt32(k string, n int32) (int32, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(int32)
if !ok {
@ -734,7 +767,9 @@ func (c *cache) DecrementInt64(k string, n int64) (int64, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(int64)
if !ok {
@ -758,7 +793,9 @@ func (c *cache) DecrementUint(k string, n uint) (uint, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(uint)
if !ok {
@ -782,7 +819,9 @@ func (c *cache) DecrementUintptr(k string, n uintptr) (uintptr, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(uintptr)
if !ok {
@ -806,7 +845,9 @@ func (c *cache) DecrementUint8(k string, n uint8) (uint8, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(uint8)
if !ok {
@ -830,7 +871,9 @@ func (c *cache) DecrementUint16(k string, n uint16) (uint16, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(uint16)
if !ok {
@ -854,7 +897,9 @@ func (c *cache) DecrementUint32(k string, n uint32) (uint32, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(uint32)
if !ok {
@ -878,7 +923,9 @@ func (c *cache) DecrementUint64(k string, n uint64) (uint64, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(uint64)
if !ok {
@ -902,7 +949,9 @@ func (c *cache) DecrementFloat32(k string, n float32) (float32, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(float32)
if !ok {
@ -926,7 +975,9 @@ func (c *cache) DecrementFloat64(k string, n float64) (float64, error) {
return 0, fmt.Errorf("Item %s not found", k)
}
if c.maxItems > 0 { v.Accessed() }
if c.maxItems > 0 {
v.Accessed()
}
rv, ok := v.Object.(float64)
if !ok {
@ -1078,6 +1129,7 @@ func (c *cache) Flush() {
func (c *cache) DeleteLRU(numItems int) {
c.Lock()
defer c.Unlock()
var lastTime int64 = 0
lastItems := make([]string, numItems) // ringbuffer for the last numItems
@ -1098,7 +1150,7 @@ func (c *cache) DeleteLRU(numItems int) {
// or start overwriting it
if liCount < numItems {
lastItems[liCount] = k
liCount ++
liCount++
} else {
lastItems[0] = k
liCount = 1
@ -1115,8 +1167,6 @@ func (c *cache) DeleteLRU(numItems int) {
c.delete(lastName)
}
}
c.Unlock()
}
type janitor struct {
@ -1227,6 +1277,10 @@ func NewWithLRU(defaultExpiration, cleanupInterval time.Duration, maxItems int)
// gob.Register() the individual types stored in the cache before encoding a
// map retrieved with c.Items(), and to register those same types before
// decoding a blob containing an items map.
func NewFrom(defaultExpiration, cleanupInterval time.Duration, items map[string]*Item, maxItems int) *Cache {
func NewFrom(defaultExpiration, cleanupInterval time.Duration, items map[string]*Item) *Cache {
return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, 0)
}
func NewFromWithLRU(defaultExpiration, cleanupInterval time.Duration, items map[string]*Item, maxItems int) *Cache {
return newCacheWithJanitor(defaultExpiration, cleanupInterval, items, maxItems)
}