refactor(internal): 优化 OSS 预签名 URL 缓存刷新任务和英雄数据缓存逻辑
- 注释掉 OSS预签名 URL 缓存刷新任务的定时执行代码 - 在 hero/hero.go 中增加对 Redis缓存和英雄数据集的非空校验 - 修改 OSS预签名 URL 生成逻辑,自动替换为 CDN 域名
This commit is contained in:
13
README.MD
13
README.MD
@@ -52,22 +52,13 @@ redis:
|
||||
|
||||
### 启动服务
|
||||
|
||||
#### Linux/Mac
|
||||
```bash
|
||||
chmod +x scripts/start.sh
|
||||
./scripts/start.sh
|
||||
```
|
||||
|
||||
#### Windows
|
||||
```cmd
|
||||
scripts\start.bat
|
||||
```
|
||||
|
||||
#### 手动启动
|
||||
```bash
|
||||
go run main.go
|
||||
```
|
||||
|
||||
|
||||
|
||||
### 访问服务
|
||||
|
||||
- **API服务**: http://localhost:8283
|
||||
|
||||
@@ -174,11 +174,11 @@ func (l *Logic) registerDefaultJobs(ctx context.Context) error {
|
||||
}
|
||||
|
||||
// 每30分钟执行一次OSS预签名URL缓存刷新任务
|
||||
if err := l.AddJob(ctx, "oss_presignurl_refresh", "0 0/30 * * * *", func() {
|
||||
l.refreshOssPresignUrlCacheJob(ctx)
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
//if err := l.AddJob(ctx, "oss_presignurl_refresh", "0 0/30 * * * *", func() {
|
||||
// l.refreshOssPresignUrlCacheJob(ctx)
|
||||
//}); err != nil {
|
||||
// return err
|
||||
//}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -130,6 +130,12 @@ func (l *Logic) GetHeroDetailByCode(ctx context.Context, code string) (*v1.HeroD
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if util.RedisCache == nil {
|
||||
panic("util.RedisCache is nil")
|
||||
}
|
||||
if fribbleHeroSet == nil {
|
||||
panic("fribbleHeroSet is nil")
|
||||
}
|
||||
// 写入 Redis 缓存,1小时
|
||||
util.RedisCache.Set(ctx, cacheKey, fribbleHeroSet.JsonContent, 0)
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -146,11 +147,12 @@ func RefreshOssPresignedUrlCache(ctx context.Context, keys []string, expire time
|
||||
}
|
||||
// 缓存到Redis,key可加前缀区分
|
||||
cacheKey := "oss:presignurl:" + key
|
||||
if err := RedisCache.Set(ctx, cacheKey, presignResult.URL, expire); err != nil {
|
||||
// 自动生成CDN域名的预签名可访问地址
|
||||
cdnPresignedUrl := strings.Replace(presignResult.URL, "https://"+bucket+".s3.bitiful.net", "https://bfoss.htoop.cn", 1)
|
||||
fmt.Printf("CDN预签名可访问地址: %s\n", cdnPresignedUrl)
|
||||
// 写入Redis时也用CDN域名的预签名地址
|
||||
if err := RedisCache.Set(ctx, cacheKey, cdnPresignedUrl, expire); err != nil {
|
||||
Error(ctx, "写入Redis失败", cacheKey, err)
|
||||
} else {
|
||||
// 打印预签名可访问地址
|
||||
fmt.Printf("预签名可访问地址: %s\n", presignResult.URL)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user