1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-04 21:00:55 +00:00

isExist func will check if the value is expired

This commit is contained in:
JessonChan 2016-01-07 09:36:23 +08:00
parent 6465dbd703
commit 8aed4c13d7

12
cache/memory.go vendored
View File

@ -82,14 +82,14 @@ 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() bc.Lock()
defer bc.Unlock() defer bc.Unlock()
bc.items[name] = &MemoryItem{ bc.items[name] = &MemoryItem{
val: value, val: value,
cratedTime: time.Now(), cratedTime: time.Now(),
lifespan: expired, lifespan: lifespan,
} }
return nil return nil
} }
@ -179,8 +179,10 @@ func (bc *MemoryCache) Decr(key string) error {
func (bc *MemoryCache) IsExist(name string) bool { func (bc *MemoryCache) IsExist(name string) bool {
bc.RLock() bc.RLock()
defer bc.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.