refactor(backend): 添加 service 缓存端口

This commit is contained in:
Forest
2025-12-19 23:39:28 +08:00
parent ef81aeb463
commit 7bbf621490
27 changed files with 906 additions and 468 deletions

View File

@@ -18,7 +18,7 @@ import (
"strings"
"time"
"github.com/redis/go-redis/v9"
"sub2api/internal/service/ports"
)
const (
@@ -36,15 +36,15 @@ const (
// UpdateService handles software updates
type UpdateService struct {
rdb *redis.Client
cache ports.UpdateCache
currentVersion string
buildType string // "source" for manual builds, "release" for CI builds
}
// NewUpdateService creates a new UpdateService
func NewUpdateService(rdb *redis.Client, version, buildType string) *UpdateService {
func NewUpdateService(cache ports.UpdateCache, version, buildType string) *UpdateService {
return &UpdateService{
rdb: rdb,
cache: cache,
currentVersion: version,
buildType: buildType,
}
@@ -533,7 +533,7 @@ func (s *UpdateService) extractBinary(archivePath, destPath string) error {
}
func (s *UpdateService) getFromCache(ctx context.Context) (*UpdateInfo, error) {
data, err := s.rdb.Get(ctx, updateCacheKey).Result()
data, err := s.cache.GetUpdateInfo(ctx)
if err != nil {
return nil, err
}
@@ -573,7 +573,7 @@ func (s *UpdateService) saveToCache(ctx context.Context, info *UpdateInfo) {
}
data, _ := json.Marshal(cacheData)
s.rdb.Set(ctx, updateCacheKey, data, time.Duration(updateCacheTTL)*time.Second)
s.cache.SetUpdateInfo(ctx, string(data), time.Duration(updateCacheTTL)*time.Second)
}
// compareVersions compares two semantic versions