作者 yangfu

feat: table data cache v1

... ... @@ -9,6 +9,7 @@ import (
)
var DefaultCache = cache.New(12*time.Hour, 1*time.Hour)
var TableCacheExpire = 24 * 60 * 60 * 2 // 两天过期
func KeyCompanyUser(companyId int, userId int) string {
return fmt.Sprintf("local:cache:user:%d:%d", companyId, userId)
... ... @@ -26,15 +27,19 @@ func SetDataTable(tableId int, table *domain.DataTable) {
DefaultDataTableCacheService.SetDataTable(tableId, table)
}
var DefaultDataTableCacheService = &DataTableCacheService{}
// 1:优化点 redis 一万条数据占用内存 10M 无法大量使用,需要内存+本地文件形式
//var DefaultDataTableCacheService DataTableCacheService= &DataTableRedisCacheService{}
type DataTableCacheService struct {
var DefaultDataTableCacheService DataTableCacheService = &DataTableLocalCacheService{}
type DataTableLocalCacheService struct {
}
func (svr *DataTableCacheService) KeyDataTable(tableId int) string {
func (svr *DataTableLocalCacheService) KeyDataTable(tableId int) string {
return fmt.Sprintf("local:cache:table:%d", tableId)
}
func (svr *DataTableCacheService) GetDataTable(tableId int) (*domain.DataTable, bool) {
func (svr *DataTableLocalCacheService) GetDataTable(tableId int) (*domain.DataTable, bool) {
v, ok := DefaultCache.Get(KeyDataTable(tableId))
if !ok {
log.Logger.Info(fmt.Sprintf("【缓存】 miss cache %v", KeyDataTable(tableId)))
... ... @@ -43,11 +48,20 @@ func (svr *DataTableCacheService) GetDataTable(tableId int) (*domain.DataTable,
log.Logger.Info(fmt.Sprintf("【缓存】 hit cache %v", KeyDataTable(tableId)))
return v.(*domain.DataTable), true
}
func (svr *DataTableCacheService) SetDataTable(tableId int, table *domain.DataTable) {
func (svr *DataTableLocalCacheService) SetDataTable(tableId int, table *domain.DataTable) {
log.Logger.Info(fmt.Sprintf("【缓存】 set cache %v", KeyDataTable(tableId)))
DefaultCache.Set(KeyDataTable(tableId), table, 24*time.Hour*30)
DefaultCache.Set(KeyDataTable(tableId), table, time.Duration(TableCacheExpire)*time.Second)
}
func (svr *DataTableCacheService) DeleteDataTable(tableId int) {
func (svr *DataTableLocalCacheService) DeleteDataTable(tableId int) {
log.Logger.Info(fmt.Sprintf("【缓存】 delete cache %v", KeyDataTable(tableId)))
DefaultCache.Delete(KeyDataTable(tableId))
}
type DataTableCacheService interface {
KeyDataTable(tableId int) string
GetDataTable(tableId int) (*domain.DataTable, bool)
SetDataTable(tableId int, table *domain.DataTable)
DeleteDataTable(tableId int)
}
... ...
package cache
import (
"fmt"
"github.com/linmadan/egglib-go/utils/json"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/redis"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
)
type DataTableRedisCacheService struct {
}
func (svr *DataTableRedisCacheService) KeyDataTable(tableId int) string {
return fmt.Sprintf("%s:cache:table:%d", constant.CACHE_PREFIX, tableId)
}
func (svr *DataTableRedisCacheService) GetDataTable(tableId int) (*domain.DataTable, bool) {
key := svr.KeyDataTable(tableId)
data, err := redis.ZeroCoreRedis.Get(key)
if err == redis.NotFound || len(data) == 0 {
log.Logger.Info(fmt.Sprintf("【缓存】 miss cache %v", key))
return nil, false
}
var table = &domain.DataTable{}
err = json.UnmarshalFromString(data, table)
if err != nil {
log.Logger.Error(fmt.Sprintf("【缓存】 miss cache %v,err %v", key, err.Error()))
return nil, false
}
log.Logger.Info(fmt.Sprintf("【缓存】 hit cache %v", key))
return table, true
}
func (svr *DataTableRedisCacheService) SetDataTable(tableId int, table *domain.DataTable) {
key := svr.KeyDataTable(tableId)
log.Logger.Info(fmt.Sprintf("【缓存】 set cache %v", key))
redis.ZeroCoreRedis.Setex(key, json.MarshalToString(table), TableCacheExpire)
}
func (svr *DataTableRedisCacheService) DeleteDataTable(tableId int) {
key := svr.KeyDataTable(tableId)
log.Logger.Info(fmt.Sprintf("【缓存】 delete cache %v", key))
redis.ZeroCoreRedis.Del(key)
}
... ...
... ... @@ -13,6 +13,8 @@ func KeyExportTable(ctx *domain.Context, tableId int) string {
var ZeroCoreRedis *redis.Redis
var NotFound = redis.ErrEmptyKey
func InitZeroCoreRedis() {
ZeroCoreRedis = redis.New(constant.REDIS_HOST+":"+constant.REDIS_PORT, redis.WithPass(constant.REDIS_AUTH))
}
... ...