iedc-go/cache/cache_memory.go

159 lines
2.4 KiB
Go
Raw Permalink Normal View History

2021-05-23 23:27:41 +00:00
package cache
2017-08-04 08:20:59 +00:00
import (
2022-03-12 17:12:29 +00:00
. "code.hoteas.com/golang/hotime/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 {
TimeOut int64
DbSet bool
SessionSet bool
2017-08-04 08:20:59 +00:00
Map
*Error
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
}
2022-03-12 17:48:54 +00:00
func (that *CacheMemory) GetError() *Error {
2022-03-12 17:48:54 +00:00
return that.Error
}
2022-03-12 17:48:54 +00:00
func (that *CacheMemory) SetError(err *Error) {
that.Error = err
}
2017-08-04 08:20:59 +00:00
//获取Cache键只能为string类型
2022-03-12 17:48:54 +00:00
func (that *CacheMemory) get(key string) interface{} {
that.Error.SetError(nil)
if that.Map == nil {
that.Map = Map{}
}
2022-03-12 17:48:54 +00:00
if that.Map[key] == nil {
2017-08-04 08:20:59 +00:00
return nil
}
2022-03-12 17:48:54 +00:00
data := that.Map.Get(key, that.Error).(cacheData)
if that.Error.GetError() != nil {
2017-08-04 08:20:59 +00:00
return nil
}
2018-06-13 11:10:13 +00:00
if data.time < time.Now().Unix() {
2022-03-12 17:48:54 +00:00
delete(that.Map, key)
2017-08-04 08:20:59 +00:00
return nil
}
return data.data
}
2022-03-12 17:48:54 +00:00
func (that *CacheMemory) refreshMap() {
2018-04-04 18:44:00 +00:00
go func() {
2022-03-12 17:48:54 +00:00
that.mutex.Lock()
defer that.mutex.Unlock()
for key, v := range that.Map {
2018-06-13 11:10:13 +00:00
data := v.(cacheData)
2018-04-04 18:44:00 +00:00
if data.time <= time.Now().Unix() {
2022-03-12 17:48:54 +00:00
delete(that.Map, key)
2018-04-04 18:44:00 +00:00
}
}
}()
}
2017-08-04 08:20:59 +00:00
//key value ,时间为时间戳
2022-03-12 17:48:54 +00:00
func (that *CacheMemory) set(key string, value interface{}, time int64) {
that.Error.SetError(nil)
2017-08-04 08:20:59 +00:00
var data cacheData
2022-03-12 17:48:54 +00:00
if that.Map == nil {
that.Map = Map{}
}
2022-03-12 17:48:54 +00:00
dd := that.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
2022-03-12 17:48:54 +00:00
that.Map.Put(key, data)
2017-08-04 08:20:59 +00:00
}
2022-03-12 17:48:54 +00:00
func (that *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)
2022-03-12 17:48:54 +00:00
for k, _ := range that.Map {
2018-06-13 11:10:13 +00:00
if strings.Index(k, key) != -1 {
2022-03-12 17:48:54 +00:00
delete(that.Map, k)
2018-04-09 17:16:24 +00:00
}
}
2018-06-13 11:10:13 +00:00
} else {
2022-03-12 17:48:54 +00:00
delete(that.Map, key)
2018-04-09 17:16:24 +00:00
}
2017-08-04 08:20:59 +00:00
}
2022-03-12 17:48:54 +00:00
func (that *CacheMemory) Cache(key string, data ...interface{}) *Obj {
2017-08-04 08:20:59 +00:00
2018-06-13 11:10:13 +00:00
x := RandX(1, 100000)
if x > 99950 {
2022-03-12 17:48:54 +00:00
that.refreshMap()
2018-06-13 11:10:13 +00:00
}
2022-03-12 17:48:54 +00:00
if that.mutex == nil {
that.mutex = &sync.RWMutex{}
2018-06-13 11:10:13 +00:00
}
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 {
2022-03-12 17:48:54 +00:00
that.mutex.RLock()
reData.Data = that.get(key)
that.mutex.RUnlock()
2018-06-13 11:10:13 +00:00
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 {
2022-03-12 17:48:54 +00:00
that.mutex.Lock()
that.delete(key)
that.mutex.Unlock()
2018-06-13 11:10:13 +00:00
return reData
}
2017-08-04 08:20:59 +00:00
2018-06-13 11:10:13 +00:00
if len(data) == 1 {
2017-08-04 08:20:59 +00:00
2022-03-12 17:48:54 +00:00
tim = tim + that.TimeOut
2018-06-13 11:10:13 +00:00
}
if len(data) == 2 {
2022-03-12 17:48:54 +00:00
that.Error.SetError(nil)
tempt := ObjToInt64(data[1], that.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
2022-03-12 17:48:54 +00:00
} else if that.Error.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
}
2022-03-12 17:48:54 +00:00
that.mutex.Lock()
that.set(key, data[0], tim)
that.mutex.Unlock()
2018-06-13 11:10:13 +00:00
return reData
2017-08-04 08:20:59 +00:00
}