2014-08-18 08:41:43 +00:00
|
|
|
// Copyright 2014 beego Author. All Rights Reserved.
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// 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
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-07-03 15:40:21 +00:00
|
|
|
//
|
2014-08-18 08:41:43 +00:00
|
|
|
// 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.
|
|
|
|
|
2013-04-22 10:56:30 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2015-09-08 16:15:03 +00:00
|
|
|
// DefaultEvery means the clock time of recycling the expired cache items in memory.
|
|
|
|
DefaultEvery = 60 // 1 minute
|
2013-04-22 10:56:30 +00:00
|
|
|
)
|
|
|
|
|
2016-01-07 01:37:50 +00:00
|
|
|
// MemoryItem store memory cache item.
|
2013-04-22 10:56:30 +00:00
|
|
|
type MemoryItem struct {
|
2016-01-07 01:37:50 +00:00
|
|
|
val interface{}
|
|
|
|
createdTime time.Time
|
2016-01-08 05:47:14 +00:00
|
|
|
lifespan time.Duration
|
2013-04-22 10:56:30 +00:00
|
|
|
}
|
|
|
|
|
2016-01-07 01:13:47 +00:00
|
|
|
func (mi *MemoryItem) isExpire() bool {
|
2016-01-07 01:25:06 +00:00
|
|
|
// 0 means forever
|
|
|
|
if mi.lifespan == 0 {
|
2016-01-07 01:13:47 +00:00
|
|
|
return false
|
|
|
|
}
|
2016-01-08 05:47:14 +00:00
|
|
|
return time.Now().Sub(mi.createdTime) > mi.lifespan
|
2016-01-07 01:13:47 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 16:15:03 +00:00
|
|
|
// MemoryCache is Memory cache adapter.
|
2013-12-22 05:35:02 +00:00
|
|
|
// it contains a RW locker for safe map storage.
|
2013-04-22 10:56:30 +00:00
|
|
|
type MemoryCache struct {
|
2016-01-07 01:08:00 +00:00
|
|
|
sync.RWMutex
|
2013-04-22 10:56:30 +00:00
|
|
|
dur time.Duration
|
|
|
|
items map[string]*MemoryItem
|
2013-12-24 13:56:48 +00:00
|
|
|
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.
|
2016-01-04 02:50:04 +00:00
|
|
|
func NewMemoryCache() Cache {
|
2013-04-22 10:56:30 +00:00
|
|
|
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{} {
|
2016-01-07 01:08:00 +00:00
|
|
|
bc.RLock()
|
|
|
|
defer bc.RUnlock()
|
2014-07-11 02:01:49 +00:00
|
|
|
if itm, ok := bc.items[name]; ok {
|
2016-01-07 01:13:47 +00:00
|
|
|
if itm.isExpire() {
|
2014-07-11 02:01:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return itm.val
|
2013-07-04 05:02:11 +00:00
|
|
|
}
|
2014-07-11 02:01:49 +00:00
|
|
|
return nil
|
2013-04-22 10:56:30 +00:00
|
|
|
}
|
|
|
|
|
2015-06-07 13:33:01 +00:00
|
|
|
// GetMulti gets caches from memory.
|
|
|
|
// if non-existed or expired, return nil.
|
|
|
|
func (bc *MemoryCache) GetMulti(names []string) []interface{} {
|
|
|
|
var rc []interface{}
|
|
|
|
for _, name := range names {
|
|
|
|
rc = append(rc, bc.Get(name))
|
|
|
|
}
|
|
|
|
return rc
|
|
|
|
}
|
|
|
|
|
2013-12-22 05:35:02 +00:00
|
|
|
// Put cache to memory.
|
2016-01-07 01:36:23 +00:00
|
|
|
// if lifespan is 0, it will be forever till restart.
|
2016-01-08 05:47:14 +00:00
|
|
|
func (bc *MemoryCache) Put(name string, value interface{}, lifespan time.Duration) error {
|
2016-01-07 01:08:00 +00:00
|
|
|
bc.Lock()
|
|
|
|
defer bc.Unlock()
|
2014-07-11 02:01:49 +00:00
|
|
|
bc.items[name] = &MemoryItem{
|
2016-01-17 15:57:07 +00:00
|
|
|
val: value,
|
2016-01-07 01:37:50 +00:00
|
|
|
createdTime: time.Now(),
|
2016-01-17 15:57:07 +00:00
|
|
|
lifespan: lifespan,
|
2013-07-04 05:02:11 +00:00
|
|
|
}
|
2013-04-22 10:56:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-08 16:15:03 +00:00
|
|
|
// Delete cache in memory.
|
2013-04-22 10:56:30 +00:00
|
|
|
func (bc *MemoryCache) Delete(name string) error {
|
2016-01-07 01:08:00 +00:00
|
|
|
bc.Lock()
|
|
|
|
defer bc.Unlock()
|
2013-04-22 10:56:30 +00:00
|
|
|
if _, ok := bc.items[name]; !ok {
|
|
|
|
return errors.New("key not exist")
|
|
|
|
}
|
|
|
|
delete(bc.items, name)
|
2014-07-11 02:01:49 +00:00
|
|
|
if _, ok := bc.items[name]; ok {
|
2013-04-22 10:56:30 +00:00
|
|
|
return errors.New("delete key error")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-08 16:15:03 +00:00
|
|
|
// Incr increase cache counter in memory.
|
2016-01-06 07:12:25 +00:00
|
|
|
// it supports int,int32,int64,uint,uint32,uint64.
|
2013-07-16 11:05:44 +00:00
|
|
|
func (bc *MemoryCache) Incr(key string) error {
|
2016-01-07 01:08:00 +00:00
|
|
|
bc.RLock()
|
|
|
|
defer bc.RUnlock()
|
2013-07-16 11:05:44 +00:00
|
|
|
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 int32:
|
|
|
|
itm.val = itm.val.(int32) + 1
|
2016-01-06 07:12:25 +00:00
|
|
|
case int64:
|
|
|
|
itm.val = itm.val.(int64) + 1
|
2013-07-16 11:05:44 +00:00
|
|
|
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:
|
2016-01-06 07:12:25 +00:00
|
|
|
return errors.New("item val is not (u)int (u)int32 (u)int64")
|
2013-07-16 11:05:44 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-08 16:15:03 +00:00
|
|
|
// Decr decrease counter in memory.
|
2013-07-16 11:05:44 +00:00
|
|
|
func (bc *MemoryCache) Decr(key string) error {
|
2016-01-07 01:08:00 +00:00
|
|
|
bc.RLock()
|
|
|
|
defer bc.RUnlock()
|
2013-07-16 11:05:44 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-09-08 16:15:03 +00:00
|
|
|
// IsExist check cache exist in memory.
|
2013-04-22 10:56:30 +00:00
|
|
|
func (bc *MemoryCache) IsExist(name string) bool {
|
2016-01-07 01:08:00 +00:00
|
|
|
bc.RLock()
|
|
|
|
defer bc.RUnlock()
|
2016-01-07 01:36:23 +00:00
|
|
|
if v, ok := bc.items[name]; ok {
|
|
|
|
return !v.isExpire()
|
|
|
|
}
|
|
|
|
return false
|
2013-04-22 10:56:30 +00:00
|
|
|
}
|
|
|
|
|
2015-09-08 16:15:03 +00:00
|
|
|
// ClearAll will delete all cache in memory.
|
2013-04-22 10:56:30 +00:00
|
|
|
func (bc *MemoryCache) ClearAll() error {
|
2016-01-07 01:08:00 +00:00
|
|
|
bc.Lock()
|
|
|
|
defer bc.Unlock()
|
2013-04-22 10:56:30 +00:00
|
|
|
bc.items = make(map[string]*MemoryItem)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-09-08 16:15:03 +00:00
|
|
|
// StartAndGC 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)
|
2013-07-04 05:02:11 +00:00
|
|
|
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
|
|
|
|
}
|
2016-01-17 15:57:07 +00:00
|
|
|
dur := time.Duration(cf["interval"]) * time.Second
|
2013-04-22 10:56:30 +00:00
|
|
|
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 {
|
2013-07-04 05:02:11 +00:00
|
|
|
<-time.After(bc.dur)
|
2013-04-22 10:56:30 +00:00
|
|
|
if bc.items == nil {
|
|
|
|
return
|
|
|
|
}
|
2017-06-15 17:26:55 +00:00
|
|
|
if keys := bc.expiredKeys(); len(keys) != 0 {
|
|
|
|
bc.clearItems(keys)
|
2013-04-22 10:56:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-15 17:26:55 +00:00
|
|
|
// expiredKeys returns key list which are expired.
|
|
|
|
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.
|
|
|
|
func (bc *MemoryCache) clearItems(keys []string) {
|
2016-01-07 01:08:00 +00:00
|
|
|
bc.Lock()
|
|
|
|
defer bc.Unlock()
|
2017-06-15 17:26:55 +00:00
|
|
|
for _, key := range keys {
|
|
|
|
delete(bc.items, key)
|
2013-04-22 10:56:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-04 02:50:04 +00:00
|
|
|
Register("memory", NewMemoryCache)
|
2013-04-22 10:56:30 +00:00
|
|
|
}
|