local_cache.go 1.8 KB
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)

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)
}

var DefaultDataTableCacheService = &DataTableCacheService{}

type DataTableCacheService struct {
}

func (svr *DataTableCacheService) KeyDataTable(tableId int) string {
	return fmt.Sprintf("local:cache:table:%d", tableId)
}
func (svr *DataTableCacheService) 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 *DataTableCacheService) 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)
}
func (svr *DataTableCacheService) DeleteDataTable(tableId int) {
	log.Logger.Info(fmt.Sprintf("【缓存】 delete cache %v", KeyDataTable(tableId)))
	DefaultCache.Delete(KeyDataTable(tableId))
}