1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-22 18:10:48 +00:00
Beego/cache/memory.go

257 lines
5.4 KiB
Go
Raw Normal View History

2020-07-22 14:50:08 +00:00
// Copyright 2014 beego Author. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cache
import (
"encoding/json"
"errors"
"sync"
"time"
)
var (
// DefaultEvery means the clock time of recycling the expired cache items in memory.
2020-07-22 14:50:08 +00:00
DefaultEvery = 60 // 1 minute
)
// MemoryItem store memory cache item.
2020-07-22 14:50:08 +00:00
type MemoryItem struct {
val interface{}
createdTime time.Time
lifespan time.Duration
}
func (mi *MemoryItem) isExpire() bool {
// 0 means forever
if mi.lifespan == 0 {
return false
}
return time.Now().Sub(mi.createdTime) > mi.lifespan
}
// MemoryCache is Memory cache adapter.
// it contains a RW locker for safe map storage.
2020-07-22 14:50:08 +00:00
type MemoryCache struct {
sync.RWMutex
dur time.Duration
items map[string]*MemoryItem
Every int // run an expiration check Every clock time
}
// NewMemoryCache returns a new MemoryCache.
func NewMemoryCache() Cache {
cache := MemoryCache{items: make(map[string]*MemoryItem)}
return &cache
}
// Get cache from memory.
// if non-existed or expired, return nil.
func (bc *MemoryCache) Get(name string) interface{} {
2020-07-22 14:50:08 +00:00
bc.RLock()
defer bc.RUnlock()
if itm, ok := bc.items[name]; ok {
2020-07-22 14:50:08 +00:00
if itm.isExpire() {
return nil
2020-07-22 14:50:08 +00:00
}
return itm.val
2020-07-22 14:50:08 +00:00
}
return nil
2020-07-22 14:50:08 +00:00
}
// GetMulti gets caches from memory.
// if non-existed or expired, return nil.
func (bc *MemoryCache) GetMulti(names []string) []interface{} {
2020-07-22 14:50:08 +00:00
var rc []interface{}
for _, name := range names {
rc = append(rc, bc.Get(name))
2020-07-22 14:50:08 +00:00
}
return rc
2020-07-22 14:50:08 +00:00
}
// Put cache to memory.
// if lifespan is 0, it will be forever till restart.
func (bc *MemoryCache) Put(name string, value interface{}, lifespan time.Duration) error {
2020-07-22 14:50:08 +00:00
bc.Lock()
defer bc.Unlock()
bc.items[name] = &MemoryItem{
val: value,
2020-07-22 14:50:08 +00:00
createdTime: time.Now(),
lifespan: lifespan,
2020-07-22 14:50:08 +00:00
}
return nil
}
// Delete cache in memory.
func (bc *MemoryCache) Delete(name string) error {
2020-07-22 14:50:08 +00:00
bc.Lock()
defer bc.Unlock()
if _, ok := bc.items[name]; !ok {
2020-07-22 14:50:08 +00:00
return errors.New("key not exist")
}
delete(bc.items, name)
if _, ok := bc.items[name]; ok {
2020-07-22 14:50:08 +00:00
return errors.New("delete key error")
}
return nil
}
// Incr increase cache counter in memory.
// it supports int,int32,int64,uint,uint32,uint64.
func (bc *MemoryCache) Incr(key string) error {
2020-07-22 14:50:08 +00:00
bc.Lock()
defer bc.Unlock()
itm, ok := bc.items[key]
if !ok {
return errors.New("key not exist")
}
switch val := itm.val.(type) {
case int:
itm.val = val + 1
case int32:
itm.val = val + 1
case int64:
itm.val = val + 1
case uint:
itm.val = val + 1
case uint32:
itm.val = val + 1
case uint64:
itm.val = val + 1
default:
return errors.New("item val is not (u)int (u)int32 (u)int64")
}
return nil
}
// Decr decrease counter in memory.
func (bc *MemoryCache) Decr(key string) error {
2020-07-22 14:50:08 +00:00
bc.Lock()
defer bc.Unlock()
itm, ok := bc.items[key]
if !ok {
return errors.New("key not exist")
}
switch val := itm.val.(type) {
case int:
itm.val = val - 1
case int64:
itm.val = val - 1
case int32:
itm.val = val - 1
case uint:
if val > 0 {
itm.val = val - 1
} else {
return errors.New("item val is less than 0")
}
case uint32:
if val > 0 {
itm.val = val - 1
} else {
return errors.New("item val is less than 0")
}
case uint64:
if val > 0 {
itm.val = val - 1
} else {
return errors.New("item val is less than 0")
}
default:
return errors.New("item val is not int int64 int32")
}
return nil
}
// IsExist check cache exist in memory.
func (bc *MemoryCache) IsExist(name string) bool {
2020-07-22 14:50:08 +00:00
bc.RLock()
defer bc.RUnlock()
if v, ok := bc.items[name]; ok {
return !v.isExpire()
2020-07-22 14:50:08 +00:00
}
return false
2020-07-22 14:50:08 +00:00
}
// ClearAll will delete all cache in memory.
func (bc *MemoryCache) ClearAll() error {
2020-07-22 14:50:08 +00:00
bc.Lock()
defer bc.Unlock()
bc.items = make(map[string]*MemoryItem)
return nil
}
// StartAndGC start memory cache. it will check expiration in every clock time.
2020-07-22 14:50:08 +00:00
func (bc *MemoryCache) StartAndGC(config string) error {
var cf map[string]int
json.Unmarshal([]byte(config), &cf)
if _, ok := cf["interval"]; !ok {
cf = make(map[string]int)
cf["interval"] = DefaultEvery
}
dur := time.Duration(cf["interval"]) * time.Second
bc.Every = cf["interval"]
bc.dur = dur
go bc.vacuum()
return nil
}
// check expiration.
func (bc *MemoryCache) vacuum() {
bc.RLock()
every := bc.Every
bc.RUnlock()
if every < 1 {
return
}
for {
<-time.After(bc.dur)
bc.RLock()
if bc.items == nil {
bc.RUnlock()
return
}
bc.RUnlock()
if keys := bc.expiredKeys(); len(keys) != 0 {
bc.clearItems(keys)
}
}
}
// expiredKeys returns key list which are expired.
2020-07-22 14:50:08 +00:00
func (bc *MemoryCache) expiredKeys() (keys []string) {
bc.RLock()
defer bc.RUnlock()
for key, itm := range bc.items {
if itm.isExpire() {
keys = append(keys, key)
}
}
return
}
// clearItems removes all the items which key in keys.
2020-07-22 14:50:08 +00:00
func (bc *MemoryCache) clearItems(keys []string) {
bc.Lock()
defer bc.Unlock()
for _, key := range keys {
delete(bc.items, key)
}
}
func init() {
Register("memory", NewMemoryCache)
}