hotime/cache/cache_memory.go

148 lines
2.3 KiB
Go
Raw Normal View History

2021-05-23 23:27:41 +00:00
package cache
2017-08-04 08:20:59 +00:00
import (
2021-05-23 23:27:41 +00:00
. "../common"
2018-04-09 17:16:24 +00:00
"strings"
2018-06-13 11:10:13 +00:00
"sync"
"time"
2017-08-04 08:20:59 +00:00
)
type CacheMemory struct {
Time int64
Map
2021-05-23 23:27:41 +00:00
ContextBase
2018-04-03 17:54:27 +00:00
mutex *sync.RWMutex
2017-08-04 08:20:59 +00:00
}
//获取Cache键只能为string类型
func (this *CacheMemory) get(key string) interface{} {
this.Error.SetError(nil)
if this.Map == nil {
this.Map = Map{}
}
2017-08-23 02:05:47 +00:00
if this.Map[key] == nil {
2017-08-04 08:20:59 +00:00
return nil
}
data := this.Map.Get(key, &this.Error).(cacheData)
if this.GetError() != nil {
return nil
}
2018-06-13 11:10:13 +00:00
if data.time < time.Now().Unix() {
2017-08-04 08:20:59 +00:00
delete(this.Map, key)
return nil
}
return data.data
}
2018-06-13 11:10:13 +00:00
func (this *CacheMemory) refreshMap() {
2018-04-04 18:44:00 +00:00
go func() {
this.mutex.Lock()
defer this.mutex.Unlock()
2018-06-13 11:10:13 +00:00
for key, v := range this.Map {
data := v.(cacheData)
2018-04-04 18:44:00 +00:00
if data.time <= time.Now().Unix() {
delete(this.Map, key)
}
}
}()
}
2017-08-04 08:20:59 +00:00
//key value ,时间为时间戳
func (this *CacheMemory) set(key string, value interface{}, time int64) {
this.Error.SetError(nil)
var data cacheData
if this.Map == nil {
this.Map = Map{}
}
dd := this.Map[key]
2017-08-04 08:20:59 +00:00
if dd == nil {
data = cacheData{}
} else {
data = dd.(cacheData)
}
data.time = time
data.data = value
2017-08-23 02:05:47 +00:00
this.Map.Put(key, data)
2017-08-04 08:20:59 +00:00
}
func (this *CacheMemory) delete(key string) {
2018-06-13 11:10:13 +00:00
del := strings.Index(key, "*")
2018-04-09 17:16:24 +00:00
//如果通配删除
2018-06-13 11:10:13 +00:00
if del != -1 {
key = Substr(key, 0, del)
for k, _ := range this.Map {
if strings.Index(k, key) != -1 {
2018-04-09 17:16:24 +00:00
delete(this.Map, key)
}
}
2018-06-13 11:10:13 +00:00
} else {
2018-04-09 17:16:24 +00:00
delete(this.Map, key)
}
2017-08-04 08:20:59 +00:00
}
func (this *CacheMemory) Cache(key string, data ...interface{}) *Obj {
2018-06-13 11:10:13 +00:00
x := RandX(1, 100000)
if x > 99950 {
this.refreshMap()
}
if this.mutex == nil {
this.mutex = &sync.RWMutex{}
}
2018-04-04 18:44:00 +00:00
2018-06-13 11:10:13 +00:00
reData := &Obj{Data: nil}
2018-04-03 17:54:27 +00:00
2018-06-13 11:10:13 +00:00
if len(data) == 0 {
this.mutex.RLock()
reData.Data = this.get(key)
this.mutex.RUnlock()
return reData
}
tim := time.Now().Unix()
2018-04-03 17:54:27 +00:00
2018-06-13 11:10:13 +00:00
if len(data) == 1 && data[0] == nil {
this.mutex.Lock()
this.delete(key)
this.mutex.Unlock()
return reData
}
2017-08-04 08:20:59 +00:00
2018-06-13 11:10:13 +00:00
if len(data) == 1 {
if this.Time == 0 {
2021-05-23 23:27:41 +00:00
//this.Time = Config.GetInt64("cacheShortTime")
2017-08-04 08:20:59 +00:00
}
2018-06-13 11:10:13 +00:00
tim = tim + this.Time
2018-06-13 11:10:13 +00:00
}
if len(data) == 2 {
this.Error.SetError(nil)
tempt := ObjToInt64(data[1], &this.Error)
2017-08-04 08:20:59 +00:00
2018-06-13 11:10:13 +00:00
if tempt > tim {
2017-08-04 08:20:59 +00:00
2018-06-13 11:10:13 +00:00
tim = tempt
} else if this.GetError() == nil {
2017-08-04 08:20:59 +00:00
2018-06-13 11:10:13 +00:00
tim = tim + tempt
2017-08-04 08:20:59 +00:00
}
2018-06-13 11:10:13 +00:00
}
this.mutex.Lock()
this.set(key, data[0], tim)
this.mutex.Unlock()
return reData
2017-08-04 08:20:59 +00:00
}