mirror of
https://github.com/astaxie/beego.git
synced 2024-11-25 18:50:55 +00:00
fix cache's bug expird is not changed by get method
This commit is contained in:
parent
9b392a0601
commit
174298b497
2
cache/cache.go
vendored
2
cache/cache.go
vendored
@ -6,7 +6,7 @@ import (
|
|||||||
|
|
||||||
type Cache interface {
|
type Cache interface {
|
||||||
Get(key string) interface{}
|
Get(key string) interface{}
|
||||||
Put(key string, val interface{}, timeout int) error
|
Put(key string, val interface{}, timeout int64) error
|
||||||
Delete(key string) error
|
Delete(key string) error
|
||||||
IsExist(key string) bool
|
IsExist(key string) bool
|
||||||
ClearAll() error
|
ClearAll() error
|
||||||
|
4
cache/cache_test.go
vendored
4
cache/cache_test.go
vendored
@ -6,7 +6,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Test_cache(t *testing.T) {
|
func Test_cache(t *testing.T) {
|
||||||
bm, err := NewCache("memory", `{"interval":60}`)
|
bm, err := NewCache("memory", `{"interval":20}`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error("init err")
|
t.Error("init err")
|
||||||
}
|
}
|
||||||
@ -21,7 +21,7 @@ func Test_cache(t *testing.T) {
|
|||||||
t.Error("get err")
|
t.Error("get err")
|
||||||
}
|
}
|
||||||
|
|
||||||
time.Sleep(70 * time.Second)
|
time.Sleep(30 * time.Second)
|
||||||
|
|
||||||
if bm.IsExist("astaxie") {
|
if bm.IsExist("astaxie") {
|
||||||
t.Error("check err")
|
t.Error("check err")
|
||||||
|
2
cache/memcache.go
vendored
2
cache/memcache.go
vendored
@ -28,7 +28,7 @@ func (rc *MemcacheCache) Get(key string) interface{} {
|
|||||||
return contain
|
return contain
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rc *MemcacheCache) Put(key string, val interface{}, timeout int) error {
|
func (rc *MemcacheCache) Put(key string, val interface{}, timeout int64) error {
|
||||||
if rc.c == nil {
|
if rc.c == nil {
|
||||||
rc.c = rc.connectInit()
|
rc.c = rc.connectInit()
|
||||||
}
|
}
|
||||||
|
34
cache/memory.go
vendored
34
cache/memory.go
vendored
@ -4,7 +4,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -16,12 +15,7 @@ var (
|
|||||||
type MemoryItem struct {
|
type MemoryItem struct {
|
||||||
val interface{}
|
val interface{}
|
||||||
Lastaccess time.Time
|
Lastaccess time.Time
|
||||||
expired int
|
expired int64
|
||||||
}
|
|
||||||
|
|
||||||
func (itm *MemoryItem) Access() interface{} {
|
|
||||||
itm.Lastaccess = time.Now()
|
|
||||||
return itm.val
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type MemoryCache struct {
|
type MemoryCache struct {
|
||||||
@ -44,13 +38,21 @@ func (bc *MemoryCache) Get(name string) interface{} {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return itm.Access()
|
if (time.Now().Unix() - itm.Lastaccess.Unix()) > itm.expired {
|
||||||
|
go bc.Delete(name)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return itm.val
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bc *MemoryCache) Put(name string, value interface{}, expired int) 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{val: value, Lastaccess: time.Now(), expired: expired}
|
t := MemoryItem{
|
||||||
|
val: value,
|
||||||
|
Lastaccess: time.Now(),
|
||||||
|
expired: expired,
|
||||||
|
}
|
||||||
if _, ok := bc.items[name]; ok {
|
if _, ok := bc.items[name]; ok {
|
||||||
return errors.New("the key is exist")
|
return errors.New("the key is exist")
|
||||||
} else {
|
} else {
|
||||||
@ -91,7 +93,7 @@ func (bc *MemoryCache) ClearAll() error {
|
|||||||
func (bc *MemoryCache) StartAndGC(config string) error {
|
func (bc *MemoryCache) StartAndGC(config string) error {
|
||||||
var cf map[string]int
|
var cf map[string]int
|
||||||
json.Unmarshal([]byte(config), &cf)
|
json.Unmarshal([]byte(config), &cf)
|
||||||
if _, ok := cf["every"]; !ok {
|
if _, ok := cf["interval"]; !ok {
|
||||||
cf = make(map[string]int)
|
cf = make(map[string]int)
|
||||||
cf["interval"] = DefaultEvery
|
cf["interval"] = DefaultEvery
|
||||||
}
|
}
|
||||||
@ -110,7 +112,7 @@ func (bc *MemoryCache) vaccuum() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
<-time.After(time.Duration(bc.dur) * time.Second)
|
<-time.After(bc.dur)
|
||||||
if bc.items == nil {
|
if bc.items == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -128,12 +130,8 @@ func (bc *MemoryCache) item_expired(name string) bool {
|
|||||||
if !ok {
|
if !ok {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
dur := time.Now().Sub(itm.Lastaccess)
|
sec := time.Now().Unix() - itm.Lastaccess.Unix()
|
||||||
sec, err := strconv.Atoi(fmt.Sprintf("%0.0f", dur.Seconds()))
|
if sec >= itm.expired {
|
||||||
if err != nil {
|
|
||||||
delete(bc.items, name)
|
|
||||||
return true
|
|
||||||
} else if sec >= itm.expired {
|
|
||||||
delete(bc.items, name)
|
delete(bc.items, name)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
2
cache/redis.go
vendored
2
cache/redis.go
vendored
@ -31,7 +31,7 @@ func (rc *RedisCache) Get(key string) interface{} {
|
|||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rc *RedisCache) Put(key string, val interface{}, timeout int) error {
|
func (rc *RedisCache) Put(key string, val interface{}, timeout int64) error {
|
||||||
if rc.c == nil {
|
if rc.c == nil {
|
||||||
rc.c = rc.connectInit()
|
rc.c = rc.connectInit()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user