local_cache.go
2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package cache
import (
"fmt"
cache "github.com/patrickmn/go-cache"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/domain"
"gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/log"
"time"
)
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)
}
func KeyDataTable(tableId int) string {
return DefaultDataTableCacheService.KeyDataTable(tableId)
}
func GetDataTable(tableId int) (*domain.DataTable, bool) {
return DefaultDataTableCacheService.GetDataTable(tableId)
}
func SetDataTable(tableId int, table *domain.DataTable) {
DefaultDataTableCacheService.SetDataTable(tableId, table)
}
// 1:优化点 redis 一万条数据占用内存 10M 无法大量使用,需要内存+本地文件形式
//var DefaultDataTableCacheService DataTableCacheService= &DataTableRedisCacheService{}
var DefaultDataTableCacheService DataTableCacheService = &DataTableLocalCacheService{}
type DataTableLocalCacheService struct {
}
func (svr *DataTableLocalCacheService) KeyDataTable(tableId int) string {
return fmt.Sprintf("local:cache:table:%d", tableId)
}
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)))
return nil, false
}
log.Logger.Info(fmt.Sprintf("【缓存】 hit cache %v", KeyDataTable(tableId)))
return v.(*domain.DataTable), true
}
func (svr *DataTableLocalCacheService) SetDataTable(tableId int, table *domain.DataTable) {
log.Logger.Info(fmt.Sprintf("【缓存】 set cache %v", KeyDataTable(tableId)))
DefaultCache.Set(KeyDataTable(tableId), table, time.Duration(TableCacheExpire)*time.Second)
}
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)
}