mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 13:20:55 +00:00
code style simplify
This commit is contained in:
parent
0b1b121db8
commit
0ff058bd25
24
cache/memory.go
vendored
24
cache/memory.go
vendored
@ -49,15 +49,14 @@ func NewMemoryCache() *MemoryCache {
|
|||||||
func (bc *MemoryCache) Get(name string) interface{} {
|
func (bc *MemoryCache) Get(name string) interface{} {
|
||||||
bc.lock.RLock()
|
bc.lock.RLock()
|
||||||
defer bc.lock.RUnlock()
|
defer bc.lock.RUnlock()
|
||||||
itm, ok := bc.items[name]
|
if itm, ok := bc.items[name]; ok {
|
||||||
if !ok {
|
if (time.Now().Unix() - itm.Lastaccess.Unix()) > itm.expired {
|
||||||
return nil
|
go bc.Delete(name)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return itm.val
|
||||||
}
|
}
|
||||||
if (time.Now().Unix() - itm.Lastaccess.Unix()) > itm.expired {
|
return nil
|
||||||
go bc.Delete(name)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return itm.val
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put cache to memory.
|
// Put cache to memory.
|
||||||
@ -65,12 +64,11 @@ func (bc *MemoryCache) Get(name string) interface{} {
|
|||||||
func (bc *MemoryCache) Put(name string, value interface{}, expired int64) error {
|
func (bc *MemoryCache) Put(name string, value interface{}, expired int64) error {
|
||||||
bc.lock.Lock()
|
bc.lock.Lock()
|
||||||
defer bc.lock.Unlock()
|
defer bc.lock.Unlock()
|
||||||
t := MemoryItem{
|
bc.items[name] = &MemoryItem{
|
||||||
val: value,
|
val: value,
|
||||||
Lastaccess: time.Now(),
|
Lastaccess: time.Now(),
|
||||||
expired: expired,
|
expired: expired,
|
||||||
}
|
}
|
||||||
bc.items[name] = &t
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,8 +80,7 @@ func (bc *MemoryCache) Delete(name string) error {
|
|||||||
return errors.New("key not exist")
|
return errors.New("key not exist")
|
||||||
}
|
}
|
||||||
delete(bc.items, name)
|
delete(bc.items, name)
|
||||||
_, valid := bc.items[name]
|
if _, ok := bc.items[name]; ok {
|
||||||
if valid {
|
|
||||||
return errors.New("delete key error")
|
return errors.New("delete key error")
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -214,8 +211,7 @@ func (bc *MemoryCache) item_expired(name string) bool {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
sec := time.Now().Unix() - itm.Lastaccess.Unix()
|
if time.Now().Unix()-itm.Lastaccess.Unix() >= itm.expired {
|
||||||
if sec >= itm.expired {
|
|
||||||
delete(bc.items, name)
|
delete(bc.items, name)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user