Commit eff200e0 authored by JessonChan's avatar JessonChan

modify as xuxiaohei suggest

https://github.com/astaxie/beego/issues/1259
parent 8832334d
...@@ -37,7 +37,7 @@ type MemoryItem struct { ...@@ -37,7 +37,7 @@ type MemoryItem struct {
// 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,8 +52,8 @@ func NewMemoryCache() Cache { ...@@ -52,8 +52,8 @@ 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 (time.Now().Unix() - itm.Lastaccess.Unix()) > itm.expired {
go bc.Delete(name) go bc.Delete(name)
...@@ -77,12 +77,8 @@ func (bc *MemoryCache) GetMulti(names []string) []interface{} { ...@@ -77,12 +77,8 @@ 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 expired is 0, it will be cleaned by next gc operation ( default gc clock is 1 minute).
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()
defer bc.lock.Unlock() defer bc.Unlock()
if expired == 0 {
//ten years,behave as the file cache
expired = 86400 * 365 * 10
}
bc.items[name] = &MemoryItem{ bc.items[name] = &MemoryItem{
val: value, val: value,
Lastaccess: time.Now(), Lastaccess: time.Now(),
...@@ -93,8 +89,8 @@ func (bc *MemoryCache) Put(name string, value interface{}, expired int64) error ...@@ -93,8 +89,8 @@ func (bc *MemoryCache) Put(name string, value interface{}, expired int64) error
// 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")
} }
...@@ -108,8 +104,8 @@ func (bc *MemoryCache) Delete(name string) error { ...@@ -108,8 +104,8 @@ func (bc *MemoryCache) Delete(name string) error {
// Incr increase cache counter in memory. // Incr increase cache counter in memory.
// it supports int,int32,int64,uint,uint32,uint64. // 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")
...@@ -135,8 +131,8 @@ func (bc *MemoryCache) Incr(key string) error { ...@@ -135,8 +131,8 @@ func (bc *MemoryCache) Incr(key string) error {
// 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")
...@@ -174,16 +170,16 @@ func (bc *MemoryCache) Decr(key string) error { ...@@ -174,16 +170,16 @@ 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] _, ok := bc.items[name]
return ok return ok
} }
// 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
} }
...@@ -224,12 +220,16 @@ func (bc *MemoryCache) vaccuum() { ...@@ -224,12 +220,16 @@ 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
} }
// expired==0 means never
if itm.expired==0{
return false
}
if time.Now().Unix()-itm.Lastaccess.Unix() >= itm.expired { if time.Now().Unix()-itm.Lastaccess.Unix() >= itm.expired {
delete(bc.items, name) delete(bc.items, name)
return true return true
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment