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 {
|
2021-05-28 14:52:22 +00:00
|
|
|
TimeOut int64
|
|
|
|
DbSet bool
|
|
|
|
SessionSet bool
|
2017-08-04 08:20:59 +00:00
|
|
|
Map
|
2021-05-28 14:52:22 +00:00
|
|
|
*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
|
|
|
}
|
|
|
|
|
2021-05-28 14:52:22 +00:00
|
|
|
func (this *CacheMemory) GetError() *Error {
|
|
|
|
|
|
|
|
return this.Error
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (this *CacheMemory) SetError(err *Error) {
|
|
|
|
this.Error = err
|
|
|
|
}
|
|
|
|
|
2017-08-04 08:20:59 +00:00
|
|
|
//获取Cache键只能为string类型
|
|
|
|
func (this *CacheMemory) get(key string) interface{} {
|
|
|
|
this.Error.SetError(nil)
|
2018-07-30 18:09:42 +00:00
|
|
|
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
|
|
|
|
}
|
2021-05-28 14:52:22 +00:00
|
|
|
data := this.Map.Get(key, this.Error).(cacheData)
|
|
|
|
if this.Error.GetError() != nil {
|
2017-08-04 08:20:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-07-30 18:09:42 +00:00
|
|
|
|
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
|
2018-07-30 18:09:42 +00:00
|
|
|
|
|
|
|
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 {
|
2021-08-28 22:08:19 +00:00
|
|
|
delete(this.Map, k)
|
|
|
|
|
2018-04-09 17:16:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2017-08-04 08:20:59 +00:00
|
|
|
|
2021-05-28 14:52:22 +00:00
|
|
|
tim = tim + this.TimeOut
|
2018-07-30 18:09:42 +00:00
|
|
|
|
2018-06-13 11:10:13 +00:00
|
|
|
}
|
|
|
|
if len(data) == 2 {
|
|
|
|
this.Error.SetError(nil)
|
2021-05-28 14:52:22 +00:00
|
|
|
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
|
2021-05-28 14:52:22 +00:00
|
|
|
} else if this.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
|
|
|
}
|
|
|
|
this.mutex.Lock()
|
|
|
|
this.set(key, data[0], tim)
|
|
|
|
this.mutex.Unlock()
|
|
|
|
return reData
|
2017-08-04 08:20:59 +00:00
|
|
|
|
|
|
|
}
|