1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-23 11:10:50 +00:00
Beego/cache.go

134 lines
2.4 KiB
Go
Raw Normal View History

2013-03-13 16:14:09 +00:00
package beego
import (
"errors"
"fmt"
"strconv"
2013-03-14 08:27:19 +00:00
"sync"
2013-03-13 16:14:09 +00:00
"time"
)
var (
DefaultEvery int = 60 // 1 minute
)
type BeeItem struct {
val interface{}
Lastaccess time.Time
expired int
}
func (itm *BeeItem) Access() interface{} {
itm.Lastaccess = time.Now()
return itm.val
}
type BeeCache struct {
2013-03-14 08:27:19 +00:00
lock sync.RWMutex
2013-03-13 16:14:09 +00:00
dur time.Duration
items map[string]*BeeItem
Every int // Run an expiration check Every seconds
}
// NewDefaultCache returns a new FileCache with sane defaults.
func NewBeeCache() *BeeCache {
2013-03-14 08:27:19 +00:00
cache := BeeCache{dur: time.Since(time.Now()),
Every: DefaultEvery}
2013-03-13 16:14:09 +00:00
return &cache
}
func (bc *BeeCache) Get(name string) interface{} {
2013-03-14 08:27:19 +00:00
bc.lock.RLock()
defer bc.lock.RUnlock()
2013-03-13 16:14:09 +00:00
itm, ok := bc.items[name]
if !ok {
return nil
}
return itm.Access()
}
func (bc *BeeCache) Put(name string, value interface{}, expired int) error {
2013-03-14 08:27:19 +00:00
bc.lock.Lock()
defer bc.lock.Unlock()
2013-03-13 16:14:09 +00:00
t := BeeItem{val: value, Lastaccess: time.Now(), expired: expired}
2013-03-26 14:31:54 +00:00
if _, ok := bc.items[name]; ok {
2013-03-13 16:14:09 +00:00
return errors.New("the key is exist")
} else {
bc.items[name] = &t
}
return nil
}
func (bc *BeeCache) Delete(name string) (ok bool, err error) {
2013-03-14 08:27:19 +00:00
bc.lock.Lock()
defer bc.lock.Unlock()
if _, ok = bc.items[name]; !ok {
2013-03-13 16:14:09 +00:00
return
}
delete(bc.items, name)
_, valid := bc.items[name]
if valid {
ok = false
}
return
}
// Return all of the item in a BeeCache
func (bc *BeeCache) Items() map[string]*BeeItem {
return bc.items
}
2013-03-13 16:14:09 +00:00
func (bc *BeeCache) IsExist(name string) bool {
2013-03-14 08:27:19 +00:00
bc.lock.RLock()
defer bc.lock.RUnlock()
2013-03-13 16:14:09 +00:00
_, ok := bc.items[name]
return ok
}
// Start activates the file cache; it will
func (bc *BeeCache) Start() error {
dur, err := time.ParseDuration(fmt.Sprintf("%ds", bc.Every))
if err != nil {
return err
}
bc.dur = dur
bc.items = make(map[string]*BeeItem, 0)
go bc.vaccuum()
return nil
}
func (bc *BeeCache) vaccuum() {
if bc.Every < 1 {
return
}
for {
<-time.After(time.Duration(bc.dur))
if bc.items == nil {
return
}
for name, _ := range bc.items {
2013-03-14 08:27:19 +00:00
bc.item_expired(name)
2013-03-13 16:14:09 +00:00
}
}
}
// item_expired returns true if an item is expired.
func (bc *BeeCache) item_expired(name string) bool {
2013-03-14 08:27:19 +00:00
bc.lock.Lock()
defer bc.lock.Unlock()
2013-03-13 16:14:09 +00:00
itm, ok := bc.items[name]
if !ok {
return true
}
dur := time.Now().Sub(itm.Lastaccess)
sec, err := strconv.Atoi(fmt.Sprintf("%0.0f", dur.Seconds()))
if err != nil {
2013-03-14 08:27:19 +00:00
delete(bc.items, name)
2013-03-13 16:14:09 +00:00
return true
} else if sec >= itm.expired {
2013-03-14 08:27:19 +00:00
delete(bc.items, name)
2013-03-13 16:14:09 +00:00
return true
}
return false
}