|
@@ -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
|
+} |