mirror of
https://github.com/astaxie/beego.git
synced 2024-11-25 21:01:31 +00:00
update cache add mutex
This commit is contained in:
parent
3b89071360
commit
512d115639
42
cache.go
42
cache.go
@ -4,27 +4,14 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
Kb = 1024
|
|
||||||
Mb = 1024 * 1024
|
|
||||||
Gb = 1024 * 1024 * 1024
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
DefaultEvery int = 60 // 1 minute
|
DefaultEvery int = 60 // 1 minute
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
InvalidCacheItem = errors.New("invalid cache item")
|
|
||||||
ItemIsDirectory = errors.New("can't cache a directory")
|
|
||||||
ItemNotInCache = errors.New("item not in cache")
|
|
||||||
ItemTooLarge = errors.New("item too large for cache")
|
|
||||||
WriteIncomplete = errors.New("incomplete write of cache item")
|
|
||||||
)
|
|
||||||
|
|
||||||
type BeeItem struct {
|
type BeeItem struct {
|
||||||
val interface{}
|
val interface{}
|
||||||
Lastaccess time.Time
|
Lastaccess time.Time
|
||||||
@ -37,6 +24,7 @@ func (itm *BeeItem) Access() interface{} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type BeeCache struct {
|
type BeeCache struct {
|
||||||
|
lock sync.RWMutex
|
||||||
dur time.Duration
|
dur time.Duration
|
||||||
items map[string]*BeeItem
|
items map[string]*BeeItem
|
||||||
Every int // Run an expiration check Every seconds
|
Every int // Run an expiration check Every seconds
|
||||||
@ -44,13 +32,14 @@ type BeeCache struct {
|
|||||||
|
|
||||||
// NewDefaultCache returns a new FileCache with sane defaults.
|
// NewDefaultCache returns a new FileCache with sane defaults.
|
||||||
func NewBeeCache() *BeeCache {
|
func NewBeeCache() *BeeCache {
|
||||||
cache := BeeCache{time.Since(time.Now()),
|
cache := BeeCache{dur: time.Since(time.Now()),
|
||||||
nil,
|
Every: DefaultEvery}
|
||||||
DefaultEvery}
|
|
||||||
return &cache
|
return &cache
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bc *BeeCache) Get(name string) interface{} {
|
func (bc *BeeCache) Get(name string) interface{} {
|
||||||
|
bc.lock.RLock()
|
||||||
|
defer bc.lock.RUnlock()
|
||||||
itm, ok := bc.items[name]
|
itm, ok := bc.items[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
@ -59,8 +48,10 @@ func (bc *BeeCache) Get(name string) interface{} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (bc *BeeCache) Put(name string, value interface{}, expired int) error {
|
func (bc *BeeCache) Put(name string, value interface{}, expired int) error {
|
||||||
|
bc.lock.Lock()
|
||||||
|
defer bc.lock.Unlock()
|
||||||
t := BeeItem{val: value, Lastaccess: time.Now(), expired: expired}
|
t := BeeItem{val: value, Lastaccess: time.Now(), expired: expired}
|
||||||
if bc.IsExist(name) {
|
if _, ok := bc.items[name]; !ok {
|
||||||
return errors.New("the key is exist")
|
return errors.New("the key is exist")
|
||||||
} else {
|
} else {
|
||||||
bc.items[name] = &t
|
bc.items[name] = &t
|
||||||
@ -69,8 +60,9 @@ func (bc *BeeCache) Put(name string, value interface{}, expired int) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (bc *BeeCache) Delete(name string) (ok bool, err error) {
|
func (bc *BeeCache) Delete(name string) (ok bool, err error) {
|
||||||
_, ok = bc.items[name]
|
bc.lock.Lock()
|
||||||
if !ok {
|
defer bc.lock.Unlock()
|
||||||
|
if _, ok = bc.items[name]; !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
delete(bc.items, name)
|
delete(bc.items, name)
|
||||||
@ -82,6 +74,8 @@ func (bc *BeeCache) Delete(name string) (ok bool, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (bc *BeeCache) IsExist(name string) bool {
|
func (bc *BeeCache) IsExist(name string) bool {
|
||||||
|
bc.lock.RLock()
|
||||||
|
defer bc.lock.RUnlock()
|
||||||
_, ok := bc.items[name]
|
_, ok := bc.items[name]
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
@ -108,15 +102,15 @@ func (bc *BeeCache) vaccuum() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
for name, _ := range bc.items {
|
for name, _ := range bc.items {
|
||||||
if bc.item_expired(name) {
|
bc.item_expired(name)
|
||||||
delete(bc.items, name)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// item_expired returns true if an item is expired.
|
// item_expired returns true if an item is expired.
|
||||||
func (bc *BeeCache) item_expired(name string) bool {
|
func (bc *BeeCache) item_expired(name string) bool {
|
||||||
|
bc.lock.Lock()
|
||||||
|
defer bc.lock.Unlock()
|
||||||
itm, ok := bc.items[name]
|
itm, ok := bc.items[name]
|
||||||
if !ok {
|
if !ok {
|
||||||
return true
|
return true
|
||||||
@ -124,8 +118,10 @@ func (bc *BeeCache) item_expired(name string) bool {
|
|||||||
dur := time.Now().Sub(itm.Lastaccess)
|
dur := time.Now().Sub(itm.Lastaccess)
|
||||||
sec, err := strconv.Atoi(fmt.Sprintf("%0.0f", dur.Seconds()))
|
sec, err := strconv.Atoi(fmt.Sprintf("%0.0f", dur.Seconds()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
delete(bc.items, name)
|
||||||
return true
|
return true
|
||||||
} else if sec >= itm.expired {
|
} else if sec >= itm.expired {
|
||||||
|
delete(bc.items, name)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
Loading…
Reference in New Issue
Block a user