代码拉取完成,页面将自动刷新
package dbCache
import (
"github.com/Peakchen/xgameCommon/akLog"
"github.com/Peakchen/xgameCommon/ado"
"github.com/Peakchen/xgameCommon/ado/service"
"github.com/Peakchen/xgameCommon/public"
"github.com/globalsign/mgo/bson"
"sync"
)
/*
redis or db cache with protects, prevent breakdown.
*/
type TModelOper struct {
buff []byte
opers int
}
type TDBCache struct {
users sync.Map // key: identify, value: map[string]*TModelOper
dbprovider *service.TDBProvider
}
var (
_dbCache *TDBCache
)
func InitDBCache(dbProvider *service.TDBProvider) {
_dbCache = &TDBCache{
dbprovider: dbProvider,
}
}
func GetDBCache() *TDBCache {
return _dbCache
}
func (this *TDBCache) loadOrAddUser(identify string) (modeldata map[string]*TModelOper) {
modeldata = nil
value, _ := this.users.LoadOrStore(identify, make(map[string]*TModelOper, 0))
if value == nil {
akLog.Error("can not load cache model.")
return
}
if value == nil {
akLog.Error("cache model invalid.")
return
}
var ok bool
modeldata, ok = value.(map[string]*TModelOper)
if !ok {
akLog.Error("cache model invalid data type.")
return
}
return
}
func (this *TDBCache) push(identify string, model string) {
modeldata := this.loadOrAddUser(identify)
if modeldata == nil {
return
}
Moper, ok := modeldata[model]
if !ok {
modeldata[model] = &TModelOper{
opers: 1,
}
} else {
Moper.opers++
}
}
func (this *TDBCache) hasExist(identify string, model string) (exist bool) {
modeldata := this.loadOrAddUser(identify)
if modeldata == nil {
return
}
_, ok := modeldata[model]
if !ok {
return
}
exist = true
return
}
func (this *TDBCache) getCache(identify string, model string, Output public.IDBCache) {
modeldata := this.loadOrAddUser(identify)
if modeldata == nil {
return
}
m, ok := modeldata[model]
if !ok {
return
}
err := bson.Unmarshal(m.buff, Output)
if err != nil {
return
}
}
func (this *TDBCache) updateCache(identify string, model string, data []byte) (succ bool) {
modeldata := this.loadOrAddUser(identify)
if modeldata == nil {
return
}
m, ok := modeldata[model]
if !ok {
return
}
m.buff = data
m.opers++
succ = true
return
}
func (this *TDBCache) pop(identify string) {
modeldata := this.loadOrAddUser(identify)
if modeldata == nil {
return
}
modeldata = map[string]*TModelOper{}
}
func (this *TDBCache) updateDB(identify string) {
modeldata := this.loadOrAddUser(identify)
if modeldata == nil {
return
}
if len(modeldata) == 0 {
return
}
for smodel, Operdata := range modeldata {
RedisKey := smodel + "." + identify
err := this.dbprovider.RediSave(identify, RedisKey, Operdata.buff, ado.EDBOper_Update)
if err != nil {
akLog.ErrorIDCard(identify, "update redis fail, model: ", smodel, ", err: ", err)
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。