mirror of
https://github.com/astaxie/beego.git
synced 2024-11-25 23:31:29 +00:00
Merge pull request #1532 from JessonChan/develop
mem cache put function fixed
This commit is contained in:
commit
3fdf72f14c
78
cache/memory.go
vendored
78
cache/memory.go
vendored
@ -27,17 +27,25 @@ var (
|
|||||||
DefaultEvery = 60 // 1 minute
|
DefaultEvery = 60 // 1 minute
|
||||||
)
|
)
|
||||||
|
|
||||||
// MemoryItem store enery cache item.
|
// MemoryItem store memory cache item.
|
||||||
type MemoryItem struct {
|
type MemoryItem struct {
|
||||||
val interface{}
|
val interface{}
|
||||||
Lastaccess time.Time
|
createdTime time.Time
|
||||||
expired int64
|
lifespan int64
|
||||||
|
}
|
||||||
|
|
||||||
|
func (mi *MemoryItem) isExpire() bool {
|
||||||
|
// 0 means forever
|
||||||
|
if mi.lifespan == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return time.Now().Unix()-mi.createdTime.Unix() > mi.lifespan
|
||||||
}
|
}
|
||||||
|
|
||||||
// MemoryCache is Memory cache adapter.
|
// MemoryCache is Memory cache adapter.
|
||||||
// it contains a RW locker for safe map storage.
|
// it contains a RW locker for safe map storage.
|
||||||
type MemoryCache struct {
|
type MemoryCache struct {
|
||||||
lock sync.RWMutex
|
sync.RWMutex
|
||||||
dur time.Duration
|
dur time.Duration
|
||||||
items map[string]*MemoryItem
|
items map[string]*MemoryItem
|
||||||
Every int // run an expiration check Every clock time
|
Every int // run an expiration check Every clock time
|
||||||
@ -52,11 +60,10 @@ func NewMemoryCache() Cache {
|
|||||||
// Get cache from memory.
|
// Get cache from memory.
|
||||||
// if non-existed or expired, return nil.
|
// if non-existed or expired, return nil.
|
||||||
func (bc *MemoryCache) Get(name string) interface{} {
|
func (bc *MemoryCache) Get(name string) interface{} {
|
||||||
bc.lock.RLock()
|
bc.RLock()
|
||||||
defer bc.lock.RUnlock()
|
defer bc.RUnlock()
|
||||||
if itm, ok := bc.items[name]; ok {
|
if itm, ok := bc.items[name]; ok {
|
||||||
if (time.Now().Unix() - itm.Lastaccess.Unix()) > itm.expired {
|
if itm.isExpire() {
|
||||||
go bc.Delete(name)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return itm.val
|
return itm.val
|
||||||
@ -75,22 +82,22 @@ func (bc *MemoryCache) GetMulti(names []string) []interface{} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Put cache to memory.
|
// Put cache to memory.
|
||||||
// if expired is 0, it will be cleaned by next gc operation ( default gc clock is 1 minute).
|
// if lifespan is 0, it will be forever till restart.
|
||||||
func (bc *MemoryCache) Put(name string, value interface{}, expired int64) error {
|
func (bc *MemoryCache) Put(name string, value interface{}, lifespan int64) error {
|
||||||
bc.lock.Lock()
|
bc.Lock()
|
||||||
defer bc.lock.Unlock()
|
defer bc.Unlock()
|
||||||
bc.items[name] = &MemoryItem{
|
bc.items[name] = &MemoryItem{
|
||||||
val: value,
|
val: value,
|
||||||
Lastaccess: time.Now(),
|
createdTime: time.Now(),
|
||||||
expired: expired,
|
lifespan: lifespan,
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete cache in memory.
|
// Delete cache in memory.
|
||||||
func (bc *MemoryCache) Delete(name string) error {
|
func (bc *MemoryCache) Delete(name string) error {
|
||||||
bc.lock.Lock()
|
bc.Lock()
|
||||||
defer bc.lock.Unlock()
|
defer bc.Unlock()
|
||||||
if _, ok := bc.items[name]; !ok {
|
if _, ok := bc.items[name]; !ok {
|
||||||
return errors.New("key not exist")
|
return errors.New("key not exist")
|
||||||
}
|
}
|
||||||
@ -102,10 +109,10 @@ func (bc *MemoryCache) Delete(name string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Incr increase cache counter in memory.
|
// Incr increase cache counter in memory.
|
||||||
// it supports int,int64,int32,uint,uint64,uint32.
|
// it supports int,int32,int64,uint,uint32,uint64.
|
||||||
func (bc *MemoryCache) Incr(key string) error {
|
func (bc *MemoryCache) Incr(key string) error {
|
||||||
bc.lock.RLock()
|
bc.RLock()
|
||||||
defer bc.lock.RUnlock()
|
defer bc.RUnlock()
|
||||||
itm, ok := bc.items[key]
|
itm, ok := bc.items[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("key not exist")
|
return errors.New("key not exist")
|
||||||
@ -113,10 +120,10 @@ func (bc *MemoryCache) Incr(key string) error {
|
|||||||
switch itm.val.(type) {
|
switch itm.val.(type) {
|
||||||
case int:
|
case int:
|
||||||
itm.val = itm.val.(int) + 1
|
itm.val = itm.val.(int) + 1
|
||||||
case int64:
|
|
||||||
itm.val = itm.val.(int64) + 1
|
|
||||||
case int32:
|
case int32:
|
||||||
itm.val = itm.val.(int32) + 1
|
itm.val = itm.val.(int32) + 1
|
||||||
|
case int64:
|
||||||
|
itm.val = itm.val.(int64) + 1
|
||||||
case uint:
|
case uint:
|
||||||
itm.val = itm.val.(uint) + 1
|
itm.val = itm.val.(uint) + 1
|
||||||
case uint32:
|
case uint32:
|
||||||
@ -124,15 +131,15 @@ func (bc *MemoryCache) Incr(key string) error {
|
|||||||
case uint64:
|
case uint64:
|
||||||
itm.val = itm.val.(uint64) + 1
|
itm.val = itm.val.(uint64) + 1
|
||||||
default:
|
default:
|
||||||
return errors.New("item val is not int int64 int32")
|
return errors.New("item val is not (u)int (u)int32 (u)int64")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decr decrease counter in memory.
|
// Decr decrease counter in memory.
|
||||||
func (bc *MemoryCache) Decr(key string) error {
|
func (bc *MemoryCache) Decr(key string) error {
|
||||||
bc.lock.RLock()
|
bc.RLock()
|
||||||
defer bc.lock.RUnlock()
|
defer bc.RUnlock()
|
||||||
itm, ok := bc.items[key]
|
itm, ok := bc.items[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
return errors.New("key not exist")
|
return errors.New("key not exist")
|
||||||
@ -170,16 +177,18 @@ func (bc *MemoryCache) Decr(key string) error {
|
|||||||
|
|
||||||
// IsExist check cache exist in memory.
|
// IsExist check cache exist in memory.
|
||||||
func (bc *MemoryCache) IsExist(name string) bool {
|
func (bc *MemoryCache) IsExist(name string) bool {
|
||||||
bc.lock.RLock()
|
bc.RLock()
|
||||||
defer bc.lock.RUnlock()
|
defer bc.RUnlock()
|
||||||
_, ok := bc.items[name]
|
if v, ok := bc.items[name]; ok {
|
||||||
return ok
|
return !v.isExpire()
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClearAll will delete all cache in memory.
|
// ClearAll will delete all cache in memory.
|
||||||
func (bc *MemoryCache) ClearAll() error {
|
func (bc *MemoryCache) ClearAll() error {
|
||||||
bc.lock.Lock()
|
bc.Lock()
|
||||||
defer bc.lock.Unlock()
|
defer bc.Unlock()
|
||||||
bc.items = make(map[string]*MemoryItem)
|
bc.items = make(map[string]*MemoryItem)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -220,13 +229,14 @@ func (bc *MemoryCache) vaccuum() {
|
|||||||
|
|
||||||
// itemExpired returns true if an item is expired.
|
// itemExpired returns true if an item is expired.
|
||||||
func (bc *MemoryCache) itemExpired(name string) bool {
|
func (bc *MemoryCache) itemExpired(name string) bool {
|
||||||
bc.lock.Lock()
|
bc.Lock()
|
||||||
defer bc.lock.Unlock()
|
defer bc.Unlock()
|
||||||
|
|
||||||
itm, ok := bc.items[name]
|
itm, ok := bc.items[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if time.Now().Unix()-itm.Lastaccess.Unix() >= itm.expired {
|
if itm.isExpire() {
|
||||||
delete(bc.items, name)
|
delete(bc.items, name)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user