作者 yangfu

feat: table data cache v1

@@ -9,6 +9,7 @@ import ( @@ -9,6 +9,7 @@ import (
9 ) 9 )
10 10
11 var DefaultCache = cache.New(12*time.Hour, 1*time.Hour) 11 var DefaultCache = cache.New(12*time.Hour, 1*time.Hour)
  12 +var TableCacheExpire = 24 * 60 * 60 * 2 // 两天过期
12 13
13 func KeyCompanyUser(companyId int, userId int) string { 14 func KeyCompanyUser(companyId int, userId int) string {
14 return fmt.Sprintf("local:cache:user:%d:%d", companyId, userId) 15 return fmt.Sprintf("local:cache:user:%d:%d", companyId, userId)
@@ -26,15 +27,19 @@ func SetDataTable(tableId int, table *domain.DataTable) { @@ -26,15 +27,19 @@ func SetDataTable(tableId int, table *domain.DataTable) {
26 DefaultDataTableCacheService.SetDataTable(tableId, table) 27 DefaultDataTableCacheService.SetDataTable(tableId, table)
27 } 28 }
28 29
29 -var DefaultDataTableCacheService = &DataTableCacheService{} 30 +// 1:优化点 redis 一万条数据占用内存 10M 无法大量使用,需要内存+本地文件形式
  31 +//var DefaultDataTableCacheService DataTableCacheService= &DataTableRedisCacheService{}
30 32
31 -type DataTableCacheService struct { 33 +var DefaultDataTableCacheService DataTableCacheService = &DataTableLocalCacheService{}
  34 +
  35 +type DataTableLocalCacheService struct {
32 } 36 }
33 37
34 -func (svr *DataTableCacheService) KeyDataTable(tableId int) string { 38 +func (svr *DataTableLocalCacheService) KeyDataTable(tableId int) string {
35 return fmt.Sprintf("local:cache:table:%d", tableId) 39 return fmt.Sprintf("local:cache:table:%d", tableId)
36 } 40 }
37 -func (svr *DataTableCacheService) GetDataTable(tableId int) (*domain.DataTable, bool) { 41 +
  42 +func (svr *DataTableLocalCacheService) GetDataTable(tableId int) (*domain.DataTable, bool) {
38 v, ok := DefaultCache.Get(KeyDataTable(tableId)) 43 v, ok := DefaultCache.Get(KeyDataTable(tableId))
39 if !ok { 44 if !ok {
40 log.Logger.Info(fmt.Sprintf("【缓存】 miss cache %v", KeyDataTable(tableId))) 45 log.Logger.Info(fmt.Sprintf("【缓存】 miss cache %v", KeyDataTable(tableId)))
@@ -43,11 +48,20 @@ func (svr *DataTableCacheService) GetDataTable(tableId int) (*domain.DataTable, @@ -43,11 +48,20 @@ func (svr *DataTableCacheService) GetDataTable(tableId int) (*domain.DataTable,
43 log.Logger.Info(fmt.Sprintf("【缓存】 hit cache %v", KeyDataTable(tableId))) 48 log.Logger.Info(fmt.Sprintf("【缓存】 hit cache %v", KeyDataTable(tableId)))
44 return v.(*domain.DataTable), true 49 return v.(*domain.DataTable), true
45 } 50 }
46 -func (svr *DataTableCacheService) SetDataTable(tableId int, table *domain.DataTable) { 51 +
  52 +func (svr *DataTableLocalCacheService) SetDataTable(tableId int, table *domain.DataTable) {
47 log.Logger.Info(fmt.Sprintf("【缓存】 set cache %v", KeyDataTable(tableId))) 53 log.Logger.Info(fmt.Sprintf("【缓存】 set cache %v", KeyDataTable(tableId)))
48 - DefaultCache.Set(KeyDataTable(tableId), table, 24*time.Hour*30) 54 + DefaultCache.Set(KeyDataTable(tableId), table, time.Duration(TableCacheExpire)*time.Second)
49 } 55 }
50 -func (svr *DataTableCacheService) DeleteDataTable(tableId int) { 56 +
  57 +func (svr *DataTableLocalCacheService) DeleteDataTable(tableId int) {
51 log.Logger.Info(fmt.Sprintf("【缓存】 delete cache %v", KeyDataTable(tableId))) 58 log.Logger.Info(fmt.Sprintf("【缓存】 delete cache %v", KeyDataTable(tableId)))
52 DefaultCache.Delete(KeyDataTable(tableId)) 59 DefaultCache.Delete(KeyDataTable(tableId))
53 } 60 }
  61 +
  62 +type DataTableCacheService interface {
  63 + KeyDataTable(tableId int) string
  64 + GetDataTable(tableId int) (*domain.DataTable, bool)
  65 + SetDataTable(tableId int, table *domain.DataTable)
  66 + DeleteDataTable(tableId int)
  67 +}
  1 +package cache
  2 +
  3 +import (
  4 + "fmt"
  5 + "github.com/linmadan/egglib-go/utils/json"
  6 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant"
  7 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
  8 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/infrastructure/redis"
  9 + "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
  10 +)
  11 +
  12 +type DataTableRedisCacheService struct {
  13 +}
  14 +
  15 +func (svr *DataTableRedisCacheService) KeyDataTable(tableId int) string {
  16 + return fmt.Sprintf("%s:cache:table:%d", constant.CACHE_PREFIX, tableId)
  17 +}
  18 +
  19 +func (svr *DataTableRedisCacheService) GetDataTable(tableId int) (*domain.DataTable, bool) {
  20 + key := svr.KeyDataTable(tableId)
  21 + data, err := redis.ZeroCoreRedis.Get(key)
  22 + if err == redis.NotFound || len(data) == 0 {
  23 + log.Logger.Info(fmt.Sprintf("【缓存】 miss cache %v", key))
  24 + return nil, false
  25 + }
  26 + var table = &domain.DataTable{}
  27 + err = json.UnmarshalFromString(data, table)
  28 + if err != nil {
  29 + log.Logger.Error(fmt.Sprintf("【缓存】 miss cache %v,err %v", key, err.Error()))
  30 + return nil, false
  31 + }
  32 + log.Logger.Info(fmt.Sprintf("【缓存】 hit cache %v", key))
  33 + return table, true
  34 +}
  35 +
  36 +func (svr *DataTableRedisCacheService) SetDataTable(tableId int, table *domain.DataTable) {
  37 + key := svr.KeyDataTable(tableId)
  38 + log.Logger.Info(fmt.Sprintf("【缓存】 set cache %v", key))
  39 + redis.ZeroCoreRedis.Setex(key, json.MarshalToString(table), TableCacheExpire)
  40 +}
  41 +
  42 +func (svr *DataTableRedisCacheService) DeleteDataTable(tableId int) {
  43 + key := svr.KeyDataTable(tableId)
  44 + log.Logger.Info(fmt.Sprintf("【缓存】 delete cache %v", key))
  45 + redis.ZeroCoreRedis.Del(key)
  46 +}
@@ -13,6 +13,8 @@ func KeyExportTable(ctx *domain.Context, tableId int) string { @@ -13,6 +13,8 @@ func KeyExportTable(ctx *domain.Context, tableId int) string {
13 13
14 var ZeroCoreRedis *redis.Redis 14 var ZeroCoreRedis *redis.Redis
15 15
  16 +var NotFound = redis.ErrEmptyKey
  17 +
16 func InitZeroCoreRedis() { 18 func InitZeroCoreRedis() {
17 ZeroCoreRedis = redis.New(constant.REDIS_HOST+":"+constant.REDIS_PORT, redis.WithPass(constant.REDIS_AUTH)) 19 ZeroCoreRedis = redis.New(constant.REDIS_HOST+":"+constant.REDIS_PORT, redis.WithPass(constant.REDIS_AUTH))
18 } 20 }