集成Manifest缓存,增加镜像拉取速度
This commit is contained in:
@@ -4,49 +4,105 @@ import (
|
||||
"crypto/md5"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// CachedToken 缓存的Token信息
|
||||
type CachedToken struct {
|
||||
Token string
|
||||
ExpiresAt time.Time
|
||||
// CachedItem 通用缓存项,支持Token和Manifest
|
||||
type CachedItem struct {
|
||||
Data []byte // 缓存数据(token字符串或manifest字节)
|
||||
ContentType string // 内容类型
|
||||
Headers map[string]string // 额外的响应头
|
||||
ExpiresAt time.Time // 过期时间
|
||||
}
|
||||
|
||||
// SimpleTokenCache 极简Token缓存
|
||||
type SimpleTokenCache struct {
|
||||
// UniversalCache 通用缓存,支持Token和Manifest
|
||||
type UniversalCache struct {
|
||||
cache sync.Map // 线程安全的并发映射
|
||||
}
|
||||
|
||||
var globalTokenCache = &SimpleTokenCache{}
|
||||
var globalCache = &UniversalCache{}
|
||||
|
||||
// Get 获取缓存的token,如果不存在或过期返回空字符串
|
||||
func (c *SimpleTokenCache) Get(key string) string {
|
||||
// Get 获取缓存项,如果不存在或过期返回nil
|
||||
func (c *UniversalCache) Get(key string) *CachedItem {
|
||||
if v, ok := c.cache.Load(key); ok {
|
||||
if cached := v.(*CachedToken); time.Now().Before(cached.ExpiresAt) {
|
||||
return cached.Token
|
||||
if cached := v.(*CachedItem); time.Now().Before(cached.ExpiresAt) {
|
||||
return cached
|
||||
}
|
||||
// 自动清理过期token,保持内存整洁
|
||||
// 自动清理过期项,保持内存整洁
|
||||
c.cache.Delete(key)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Set 设置缓存项
|
||||
func (c *UniversalCache) Set(key string, data []byte, contentType string, headers map[string]string, ttl time.Duration) {
|
||||
c.cache.Store(key, &CachedItem{
|
||||
Data: data,
|
||||
ContentType: contentType,
|
||||
Headers: headers,
|
||||
ExpiresAt: time.Now().Add(ttl),
|
||||
})
|
||||
}
|
||||
|
||||
// GetToken 获取缓存的token(向后兼容)
|
||||
func (c *UniversalCache) GetToken(key string) string {
|
||||
if item := c.Get(key); item != nil {
|
||||
return string(item.Data)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Set 设置token缓存,自动计算过期时间
|
||||
func (c *SimpleTokenCache) Set(key, token string, ttl time.Duration) {
|
||||
c.cache.Store(key, &CachedToken{
|
||||
Token: token,
|
||||
ExpiresAt: time.Now().Add(ttl),
|
||||
})
|
||||
// SetToken 设置token缓存(向后兼容)
|
||||
func (c *UniversalCache) SetToken(key, token string, ttl time.Duration) {
|
||||
c.Set(key, []byte(token), "application/json", nil, ttl)
|
||||
}
|
||||
|
||||
// buildCacheKey 构建稳定的缓存key
|
||||
func buildCacheKey(query string) string {
|
||||
func buildCacheKey(prefix, query string) string {
|
||||
// 使用MD5确保key的一致性和简洁性
|
||||
return fmt.Sprintf("token:%x", md5.Sum([]byte(query)))
|
||||
return fmt.Sprintf("%s:%x", prefix, md5.Sum([]byte(query)))
|
||||
}
|
||||
|
||||
// buildTokenCacheKey 构建token缓存key(向后兼容)
|
||||
func buildTokenCacheKey(query string) string {
|
||||
return buildCacheKey("token", query)
|
||||
}
|
||||
|
||||
// buildManifestCacheKey 构建manifest缓存key
|
||||
func buildManifestCacheKey(imageRef, reference string) string {
|
||||
key := fmt.Sprintf("%s:%s", imageRef, reference)
|
||||
return buildCacheKey("manifest", key)
|
||||
}
|
||||
|
||||
// getManifestTTL 根据引用类型智能确定TTL
|
||||
func getManifestTTL(reference string) time.Duration {
|
||||
cfg := GetConfig()
|
||||
defaultTTL := 30 * time.Minute
|
||||
if cfg.TokenCache.DefaultTTL != "" {
|
||||
if parsed, err := time.ParseDuration(cfg.TokenCache.DefaultTTL); err == nil {
|
||||
defaultTTL = parsed
|
||||
}
|
||||
}
|
||||
|
||||
// 智能TTL策略
|
||||
if strings.HasPrefix(reference, "sha256:") {
|
||||
// immutable digest: 长期缓存
|
||||
return 24 * time.Hour
|
||||
}
|
||||
|
||||
// mutable tag的智能判断
|
||||
if reference == "latest" || reference == "main" || reference == "master" ||
|
||||
reference == "dev" || reference == "develop" {
|
||||
// 热门可变标签: 短期缓存
|
||||
return 10 * time.Minute
|
||||
}
|
||||
|
||||
// 普通tag: 中等缓存时间
|
||||
return defaultTTL
|
||||
}
|
||||
|
||||
// extractTTLFromResponse 从响应中智能提取TTL
|
||||
@@ -69,15 +125,36 @@ func extractTTLFromResponse(responseBody []byte) time.Duration {
|
||||
return defaultTTL
|
||||
}
|
||||
|
||||
// writeTokenResponse 写入token响应
|
||||
// writeTokenResponse 写入token响应(向后兼容)
|
||||
func writeTokenResponse(c *gin.Context, cachedBody string) {
|
||||
// 直接返回缓存的完整响应体,保持格式一致性
|
||||
c.Header("Content-Type", "application/json")
|
||||
c.String(200, cachedBody)
|
||||
}
|
||||
|
||||
// isTokenCacheEnabled 检查token缓存是否启用
|
||||
func isTokenCacheEnabled() bool {
|
||||
// writeCachedResponse 写入缓存响应
|
||||
func writeCachedResponse(c *gin.Context, item *CachedItem) {
|
||||
// 设置内容类型
|
||||
if item.ContentType != "" {
|
||||
c.Header("Content-Type", item.ContentType)
|
||||
}
|
||||
|
||||
// 设置额外的响应头
|
||||
for key, value := range item.Headers {
|
||||
c.Header(key, value)
|
||||
}
|
||||
|
||||
// 返回数据
|
||||
c.Data(200, item.ContentType, item.Data)
|
||||
}
|
||||
|
||||
// isCacheEnabled 检查缓存是否启用
|
||||
func isCacheEnabled() bool {
|
||||
cfg := GetConfig()
|
||||
return cfg.TokenCache.Enabled
|
||||
}
|
||||
|
||||
// isTokenCacheEnabled 检查token缓存是否启用(向后兼容)
|
||||
func isTokenCacheEnabled() bool {
|
||||
return isCacheEnabled()
|
||||
}
|
||||
Reference in New Issue
Block a user