74 lines
1.3 KiB
Go
74 lines
1.3 KiB
Go
package cache
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strconv"
|
|
|
|
"git.coopgo.io/coopgo-apps/parcoursmob/core/utils/storage"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
type CacheService struct {
|
|
cache storage.CacheHandler
|
|
}
|
|
|
|
func NewCacheService(cache storage.CacheHandler) *CacheService {
|
|
return &CacheService{cache: cache}
|
|
}
|
|
|
|
type GetCacheResult struct {
|
|
Data []byte
|
|
}
|
|
|
|
func (s *CacheService) GetCacheData(cacheID string, limitsMin, limitsMax *int) (*GetCacheResult, error) {
|
|
d, err := s.cache.Get(cacheID)
|
|
if err != nil {
|
|
log.Error().Err(err).Msg("")
|
|
return nil, err
|
|
}
|
|
|
|
var data []any
|
|
if val, ok := d.([]any); ok {
|
|
data = val
|
|
} else {
|
|
data = []any{d}
|
|
}
|
|
|
|
result := data
|
|
if limitsMin != nil {
|
|
min := *limitsMin
|
|
if limitsMax != nil {
|
|
max := *limitsMax
|
|
if max > len(data) {
|
|
result = data[min:]
|
|
} else {
|
|
result = data[min:max]
|
|
}
|
|
} else {
|
|
result = data[min:]
|
|
}
|
|
}
|
|
|
|
j, err := json.Marshal(result)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &GetCacheResult{
|
|
Data: j,
|
|
}, nil
|
|
}
|
|
|
|
func ParseLimits(limitsMinStr, limitsMaxStr string) (limitsMin, limitsMax *int) {
|
|
if limitsMinStr != "" {
|
|
if min, err := strconv.Atoi(limitsMinStr); err == nil {
|
|
limitsMin = &min
|
|
}
|
|
}
|
|
if limitsMaxStr != "" {
|
|
if max, err := strconv.Atoi(limitsMaxStr); err == nil {
|
|
limitsMax = &max
|
|
}
|
|
}
|
|
return
|
|
} |