fix cache's bug expird is not changed by get method
Showing
5 changed files
with
23 additions
and
25 deletions
| ... | @@ -6,7 +6,7 @@ import ( | ... | @@ -6,7 +6,7 @@ import ( |
| 6 | 6 | ||
| 7 | type Cache interface { | 7 | type Cache interface { |
| 8 | Get(key string) interface{} | 8 | Get(key string) interface{} |
| 9 | Put(key string, val interface{}, timeout int) error | 9 | Put(key string, val interface{}, timeout int64) error |
| 10 | Delete(key string) error | 10 | Delete(key string) error |
| 11 | IsExist(key string) bool | 11 | IsExist(key string) bool |
| 12 | ClearAll() error | 12 | ClearAll() error |
| ... | @@ -28,7 +28,7 @@ func Register(name string, adapter Cache) { | ... | @@ -28,7 +28,7 @@ func Register(name string, adapter Cache) { |
| 28 | adapters[name] = adapter | 28 | adapters[name] = adapter |
| 29 | } | 29 | } |
| 30 | 30 | ||
| 31 | // config need to be correct JSON as string: {"interval":360} | 31 | // config need to be correct JSON as string: {"interval":360} |
| 32 | func NewCache(adapterName, config string) (Cache, error) { | 32 | func NewCache(adapterName, config string) (Cache, error) { |
| 33 | adapter, ok := adapters[adapterName] | 33 | adapter, ok := adapters[adapterName] |
| 34 | if !ok { | 34 | if !ok { | ... | ... |
| ... | @@ -6,7 +6,7 @@ import ( | ... | @@ -6,7 +6,7 @@ import ( |
| 6 | ) | 6 | ) |
| 7 | 7 | ||
| 8 | func Test_cache(t *testing.T) { | 8 | func Test_cache(t *testing.T) { |
| 9 | bm, err := NewCache("memory", `{"interval":60}`) | 9 | bm, err := NewCache("memory", `{"interval":20}`) |
| 10 | if err != nil { | 10 | if err != nil { |
| 11 | t.Error("init err") | 11 | t.Error("init err") |
| 12 | } | 12 | } |
| ... | @@ -21,7 +21,7 @@ func Test_cache(t *testing.T) { | ... | @@ -21,7 +21,7 @@ func Test_cache(t *testing.T) { |
| 21 | t.Error("get err") | 21 | t.Error("get err") |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | time.Sleep(70 * time.Second) | 24 | time.Sleep(30 * time.Second) |
| 25 | 25 | ||
| 26 | if bm.IsExist("astaxie") { | 26 | if bm.IsExist("astaxie") { |
| 27 | t.Error("check err") | 27 | t.Error("check err") | ... | ... |
| ... | @@ -28,7 +28,7 @@ func (rc *MemcacheCache) Get(key string) interface{} { | ... | @@ -28,7 +28,7 @@ func (rc *MemcacheCache) Get(key string) interface{} { |
| 28 | return contain | 28 | return contain |
| 29 | } | 29 | } |
| 30 | 30 | ||
| 31 | func (rc *MemcacheCache) Put(key string, val interface{}, timeout int) error { | 31 | func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error { |
| 32 | if rc.c == nil { | 32 | if rc.c == nil { |
| 33 | rc.c = rc.connectInit() | 33 | rc.c = rc.connectInit() |
| 34 | } | 34 | } | ... | ... |
| ... | @@ -4,7 +4,6 @@ import ( | ... | @@ -4,7 +4,6 @@ import ( |
| 4 | "encoding/json" | 4 | "encoding/json" |
| 5 | "errors" | 5 | "errors" |
| 6 | "fmt" | 6 | "fmt" |
| 7 | "strconv" | ||
| 8 | "sync" | 7 | "sync" |
| 9 | "time" | 8 | "time" |
| 10 | ) | 9 | ) |
| ... | @@ -16,12 +15,7 @@ var ( | ... | @@ -16,12 +15,7 @@ var ( |
| 16 | type MemoryItem struct { | 15 | type MemoryItem struct { |
| 17 | val interface{} | 16 | val interface{} |
| 18 | Lastaccess time.Time | 17 | Lastaccess time.Time |
| 19 | expired int | 18 | expired int64 |
| 20 | } | ||
| 21 | |||
| 22 | func (itm *MemoryItem) Access() interface{} { | ||
| 23 | itm.Lastaccess = time.Now() | ||
| 24 | return itm.val | ||
| 25 | } | 19 | } |
| 26 | 20 | ||
| 27 | type MemoryCache struct { | 21 | type MemoryCache struct { |
| ... | @@ -44,13 +38,21 @@ func (bc *MemoryCache) Get(name string) interface{} { | ... | @@ -44,13 +38,21 @@ func (bc *MemoryCache) Get(name string) interface{} { |
| 44 | if !ok { | 38 | if !ok { |
| 45 | return nil | 39 | return nil |
| 46 | } | 40 | } |
| 47 | return itm.Access() | 41 | if (time.Now().Unix() - itm.Lastaccess.Unix()) > itm.expired { |
| 42 | go bc.Delete(name) | ||
| 43 | return nil | ||
| 44 | } | ||
| 45 | return itm.val | ||
| 48 | } | 46 | } |
| 49 | 47 | ||
| 50 | func (bc *MemoryCache) Put(name string, value interface{}, expired int) error { | 48 | func (bc *MemoryCache) Put(name string, value interface{}, expired int64) error { |
| 51 | bc.lock.Lock() | 49 | bc.lock.Lock() |
| 52 | defer bc.lock.Unlock() | 50 | defer bc.lock.Unlock() |
| 53 | t := MemoryItem{val: value, Lastaccess: time.Now(), expired: expired} | 51 | t := MemoryItem{ |
| 52 | val: value, | ||
| 53 | Lastaccess: time.Now(), | ||
| 54 | expired: expired, | ||
| 55 | } | ||
| 54 | if _, ok := bc.items[name]; ok { | 56 | if _, ok := bc.items[name]; ok { |
| 55 | return errors.New("the key is exist") | 57 | return errors.New("the key is exist") |
| 56 | } else { | 58 | } else { |
| ... | @@ -87,11 +89,11 @@ func (bc *MemoryCache) ClearAll() error { | ... | @@ -87,11 +89,11 @@ func (bc *MemoryCache) ClearAll() error { |
| 87 | return nil | 89 | return nil |
| 88 | } | 90 | } |
| 89 | 91 | ||
| 90 | // Start activates the file cache; it will | 92 | // Start activates the file cache; it will |
| 91 | func (bc *MemoryCache) StartAndGC(config string) error { | 93 | func (bc *MemoryCache) StartAndGC(config string) error { |
| 92 | var cf map[string]int | 94 | var cf map[string]int |
| 93 | json.Unmarshal([]byte(config), &cf) | 95 | json.Unmarshal([]byte(config), &cf) |
| 94 | if _, ok := cf["every"]; !ok { | 96 | if _, ok := cf["interval"]; !ok { |
| 95 | cf = make(map[string]int) | 97 | cf = make(map[string]int) |
| 96 | cf["interval"] = DefaultEvery | 98 | cf["interval"] = DefaultEvery |
| 97 | } | 99 | } |
| ... | @@ -110,7 +112,7 @@ func (bc *MemoryCache) vaccuum() { | ... | @@ -110,7 +112,7 @@ func (bc *MemoryCache) vaccuum() { |
| 110 | return | 112 | return |
| 111 | } | 113 | } |
| 112 | for { | 114 | for { |
| 113 | <-time.After(time.Duration(bc.dur) * time.Second) | 115 | <-time.After(bc.dur) |
| 114 | if bc.items == nil { | 116 | if bc.items == nil { |
| 115 | return | 117 | return |
| 116 | } | 118 | } |
| ... | @@ -128,12 +130,8 @@ func (bc *MemoryCache) item_expired(name string) bool { | ... | @@ -128,12 +130,8 @@ func (bc *MemoryCache) item_expired(name string) bool { |
| 128 | if !ok { | 130 | if !ok { |
| 129 | return true | 131 | return true |
| 130 | } | 132 | } |
| 131 | dur := time.Now().Sub(itm.Lastaccess) | 133 | sec := time.Now().Unix() - itm.Lastaccess.Unix() |
| 132 | sec, err := strconv.Atoi(fmt.Sprintf("%0.0f", dur.Seconds())) | 134 | if sec >= itm.expired { |
| 133 | if err != nil { | ||
| 134 | delete(bc.items, name) | ||
| 135 | return true | ||
| 136 | } else if sec >= itm.expired { | ||
| 137 | delete(bc.items, name) | 135 | delete(bc.items, name) |
| 138 | return true | 136 | return true |
| 139 | } | 137 | } | ... | ... |
| ... | @@ -31,7 +31,7 @@ func (rc *RedisCache) Get(key string) interface{} { | ... | @@ -31,7 +31,7 @@ func (rc *RedisCache) Get(key string) interface{} { |
| 31 | return v | 31 | return v |
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | func (rc *RedisCache) Put(key string, val interface{}, timeout int) error { | 34 | func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error { |
| 35 | if rc.c == nil { | 35 | if rc.c == nil { |
| 36 | rc.c = rc.connectInit() | 36 | rc.c = rc.connectInit() |
| 37 | } | 37 | } | ... | ... |
-
Please register or sign in to post a comment