1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-26 05:11:31 +00:00

extract a expire fun

This commit is contained in:
JessonChan 2016-01-07 09:13:47 +08:00
parent eff200e014
commit b0b9812de6

17
cache/memory.go vendored
View File

@ -34,6 +34,14 @@ type MemoryItem struct {
expired int64 expired int64
} }
func (mi *MemoryItem) isExpire() bool {
// expired==0 means never
if mi.expired == 0 {
return false
}
return time.Now().Unix()-mi.Lastaccess.Unix() > mi.expired
}
// 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 {
@ -55,7 +63,7 @@ func (bc *MemoryCache) Get(name string) interface{} {
bc.RLock() bc.RLock()
defer bc.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) go bc.Delete(name)
return nil return nil
} }
@ -222,15 +230,12 @@ func (bc *MemoryCache) vaccuum() {
func (bc *MemoryCache) itemExpired(name string) bool { func (bc *MemoryCache) itemExpired(name string) bool {
bc.Lock() bc.Lock()
defer bc.Unlock() defer bc.Unlock()
itm, ok := bc.items[name] itm, ok := bc.items[name]
if !ok { if !ok {
return true return true
} }
// expired==0 means never if itm.isExpire() {
if itm.expired==0{
return false
}
if time.Now().Unix()-itm.Lastaccess.Unix() >= itm.expired {
delete(bc.items, name) delete(bc.items, name)
return true return true
} }