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

225 lines
4.8 KiB
Go
Raw Normal View History

2014-04-12 05:18:18 +00:00
// Beego (http://beego.me/)
// @description beego is an open-source, high-performance web framework for the Go programming language.
// @link http://github.com/astaxie/beego for the canonical source repository
// @license http://github.com/astaxie/beego/blob/master/LICENSE
// @authors astaxie
2013-04-22 10:56:30 +00:00
package cache
import (
"encoding/json"
"errors"
"fmt"
"sync"
"time"
)
var (
2013-12-22 05:35:02 +00:00
// clock time of recycling the expired cache items in memory.
2013-04-22 10:56:30 +00:00
DefaultEvery int = 60 // 1 minute
)
2013-12-22 05:35:02 +00:00
// Memory cache item.
2013-04-22 10:56:30 +00:00
type MemoryItem struct {
val interface{}
Lastaccess time.Time
expired int64
2013-04-22 10:56:30 +00:00
}
2013-12-22 05:35:02 +00:00
// Memory cache adapter.
// it contains a RW locker for safe map storage.
2013-04-22 10:56:30 +00:00
type MemoryCache struct {
lock sync.RWMutex
dur time.Duration
items map[string]*MemoryItem
Every int // run an expiration check Every clock time
2013-04-22 10:56:30 +00:00
}
2013-12-22 05:35:02 +00:00
// NewMemoryCache returns a new MemoryCache.
2013-04-22 10:56:30 +00:00
func NewMemoryCache() *MemoryCache {
cache := MemoryCache{items: make(map[string]*MemoryItem)}
return &cache
}
2013-12-22 05:35:02 +00:00
// Get cache from memory.
// if non-existed or expired, return nil.
2013-04-22 10:56:30 +00:00
func (bc *MemoryCache) Get(name string) interface{} {
bc.lock.RLock()
defer bc.lock.RUnlock()
itm, ok := bc.items[name]
if !ok {
return nil
}
if (time.Now().Unix() - itm.Lastaccess.Unix()) > itm.expired {
go bc.Delete(name)
return nil
}
return itm.val
2013-04-22 10:56:30 +00:00
}
2013-12-22 05:35:02 +00:00
// Put cache to memory.
// 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 {
2013-04-22 10:56:30 +00:00
bc.lock.Lock()
defer bc.lock.Unlock()
t := MemoryItem{
val: value,
Lastaccess: time.Now(),
expired: expired,
}
bc.items[name] = &t
2013-04-22 10:56:30 +00:00
return nil
}
2013-12-22 05:35:02 +00:00
/// Delete cache in memory.
2013-04-22 10:56:30 +00:00
func (bc *MemoryCache) Delete(name string) error {
bc.lock.Lock()
defer bc.lock.Unlock()
if _, ok := bc.items[name]; !ok {
return errors.New("key not exist")
}
delete(bc.items, name)
_, valid := bc.items[name]
if valid {
return errors.New("delete key error")
}
return nil
}
2013-12-22 05:35:02 +00:00
// Increase cache counter in memory.
// it supports int,int64,int32,uint,uint64,uint32.
2013-07-16 11:05:44 +00:00
func (bc *MemoryCache) Incr(key string) error {
bc.lock.RLock()
defer bc.lock.RUnlock()
itm, ok := bc.items[key]
if !ok {
return errors.New("key not exist")
}
switch itm.val.(type) {
case int:
itm.val = itm.val.(int) + 1
case int64:
itm.val = itm.val.(int64) + 1
case int32:
itm.val = itm.val.(int32) + 1
case uint:
itm.val = itm.val.(uint) + 1
case uint32:
itm.val = itm.val.(uint32) + 1
case uint64:
itm.val = itm.val.(uint64) + 1
default:
return errors.New("item val is not int int64 int32")
}
return nil
}
2013-12-22 05:35:02 +00:00
// Decrease counter in memory.
2013-07-16 11:05:44 +00:00
func (bc *MemoryCache) Decr(key string) error {
bc.lock.RLock()
defer bc.lock.RUnlock()
itm, ok := bc.items[key]
if !ok {
return errors.New("key not exist")
}
switch itm.val.(type) {
case int:
itm.val = itm.val.(int) - 1
case int64:
itm.val = itm.val.(int64) - 1
case int32:
itm.val = itm.val.(int32) - 1
case uint:
if itm.val.(uint) > 0 {
itm.val = itm.val.(uint) - 1
} else {
return errors.New("item val is less than 0")
}
case uint32:
if itm.val.(uint32) > 0 {
itm.val = itm.val.(uint32) - 1
} else {
return errors.New("item val is less than 0")
}
case uint64:
if itm.val.(uint64) > 0 {
itm.val = itm.val.(uint64) - 1
} else {
return errors.New("item val is less than 0")
}
default:
return errors.New("item val is not int int64 int32")
}
return nil
}
2013-12-22 05:35:02 +00:00
// check cache exist in memory.
2013-04-22 10:56:30 +00:00
func (bc *MemoryCache) IsExist(name string) bool {
bc.lock.RLock()
defer bc.lock.RUnlock()
_, ok := bc.items[name]
return ok
}
2013-12-22 05:35:02 +00:00
// delete all cache in memory.
2013-04-22 10:56:30 +00:00
func (bc *MemoryCache) ClearAll() error {
bc.lock.Lock()
defer bc.lock.Unlock()
bc.items = make(map[string]*MemoryItem)
return nil
}
2013-12-22 05:35:02 +00:00
// start memory cache. it will check expiration in every clock time.
2013-04-22 10:56:30 +00:00
func (bc *MemoryCache) StartAndGC(config string) error {
var cf map[string]int
json.Unmarshal([]byte(config), &cf)
if _, ok := cf["interval"]; !ok {
2013-04-22 13:31:23 +00:00
cf = make(map[string]int)
2013-04-22 10:56:30 +00:00
cf["interval"] = DefaultEvery
}
dur, err := time.ParseDuration(fmt.Sprintf("%ds", cf["interval"]))
if err != nil {
return err
}
bc.Every = cf["interval"]
bc.dur = dur
go bc.vaccuum()
return nil
}
2013-12-22 05:35:02 +00:00
// check expiration.
2013-04-22 10:56:30 +00:00
func (bc *MemoryCache) vaccuum() {
if bc.Every < 1 {
return
}
for {
<-time.After(bc.dur)
2013-04-22 10:56:30 +00:00
if bc.items == nil {
return
}
for name, _ := range bc.items {
bc.item_expired(name)
}
}
}
// item_expired returns true if an item is expired.
func (bc *MemoryCache) item_expired(name string) bool {
bc.lock.Lock()
defer bc.lock.Unlock()
itm, ok := bc.items[name]
if !ok {
return true
}
sec := time.Now().Unix() - itm.Lastaccess.Unix()
if sec >= itm.expired {
2013-04-22 10:56:30 +00:00
delete(bc.items, name)
return true
}
return false
}
func init() {
Register("memory", NewMemoryCache())
}