query_set.go 1.6 KB
package redis

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

const (
	TableQuerySetExpire = 3600 * 24
)

func KeyTableQuerySet(tableId int) string {
	return fmt.Sprintf("%v:queryset:temporary:%v", constant.CACHE_PREFIX, tableId)
}

type TableQuerySetCache struct {
	QuerySetId      int                      `json:"querySetId"`
	TableId         int                      `json:"tableId"`
	QueryComponents []*domain.QueryComponent `json:"queryComponents"`
}

func NewTableQuerySetCache(querySetId int, tableId int, queryComponents []*domain.QueryComponent) TableQuerySetCache {
	return TableQuerySetCache{
		QuerySetId:      querySetId,
		TableId:         tableId,
		QueryComponents: queryComponents,
	}
}

type TableQuerySetCacheService struct {
}

func (s *TableQuerySetCacheService) SetTableQuerySetCache(key string, cache TableQuerySetCache) error {
	err := ZeroCoreRedis.Setex(key, json.MarshalToString(cache), TemporaryFileExpire)
	return err
}

func (s *TableQuerySetCacheService) GetTableQuerySetCache(key string) (*TableQuerySetCache, error) {
	var res = &TableQuerySetCache{}
	ok, err := ZeroCoreRedis.Exists(key)
	if !ok {
		return nil, domain.ErrorNotFound
	}
	if err != nil {
		return nil, err
	}
	data, err := ZeroCoreRedis.Get(key)
	if err != nil {
		return nil, err
	}
	err = json.UnmarshalFromString(data, res)
	if err != nil {
		return nil, err
	}
	return res, nil
}

func NewTableQuerySetCacheService() *TableQuerySetCacheService {
	return &TableQuerySetCacheService{}
}