mirror of
https://github.com/fish2018/pansou.git
synced 2025-11-25 03:14:59 +08:00
移除旧的二级缓存实现
This commit is contained in:
7
.github/workflows/docker_ci.yml
vendored
7
.github/workflows/docker_ci.yml
vendored
@@ -38,7 +38,7 @@ jobs:
|
||||
with:
|
||||
registry: ghcr.io
|
||||
username: ${{ github.repository_owner }}
|
||||
password: ${{ secrets.DOCKER }}
|
||||
password: ${{ secrets.DOCKER }}
|
||||
|
||||
- name: 提取Docker元数据
|
||||
id: meta
|
||||
@@ -57,7 +57,8 @@ jobs:
|
||||
uses: docker/build-push-action@v5
|
||||
with:
|
||||
context: .
|
||||
platforms: linux/amd64
|
||||
# 这是关键修改点
|
||||
platforms: linux/amd64,linux/arm64
|
||||
push: ${{ github.event_name != 'pull_request' }}
|
||||
tags: ${{ steps.meta.outputs.tags }}
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
||||
@@ -66,4 +67,4 @@ jobs:
|
||||
build-args: |
|
||||
BUILD_DATE=${{ github.event.repository.updated_at }}
|
||||
VCS_REF=${{ github.sha }}
|
||||
VERSION=${{ steps.meta.outputs.version }}
|
||||
VERSION=${{ steps.meta.outputs.version }}
|
||||
|
||||
@@ -21,8 +21,11 @@ ARG VERSION=dev
|
||||
ARG BUILD_DATE=unknown
|
||||
ARG VCS_REF=unknown
|
||||
|
||||
# 构建应用
|
||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags="-s -w -X main.Version=${VERSION} -X main.BuildDate=${BUILD_DATE} -X main.GitCommit=${VCS_REF} -extldflags '-static'" -o pansou .
|
||||
# 这是关键修改点:接收 buildx 自动传入的平台参数
|
||||
ARG TARGETARCH=amd64
|
||||
|
||||
# 构建应用 (注意这里的 GOARCH=${TARGETARCH} 修改)
|
||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} go build -ldflags="-s -w -X main.Version=${VERSION} -X main.BuildDate=${BUILD_DATE} -X main.GitCommit=${VCS_REF} -extldflags '-static'" -o pansou .
|
||||
|
||||
# 运行阶段
|
||||
FROM alpine:3.19
|
||||
@@ -68,4 +71,4 @@ LABEL org.opencontainers.image.title="PanSou" \
|
||||
maintainer="fish2018"
|
||||
|
||||
# 运行应用
|
||||
CMD ["/app/pansou"]
|
||||
CMD ["/app/pansou"]
|
||||
@@ -23,7 +23,6 @@ var priorityKeywords = []string{"合集", "系列", "全", "完", "最新", "附
|
||||
|
||||
// 全局缓存实例和缓存是否初始化标志
|
||||
var (
|
||||
twoLevelCache *cache.TwoLevelCache
|
||||
enhancedTwoLevelCache *cache.EnhancedTwoLevelCache
|
||||
cacheInitialized bool
|
||||
)
|
||||
@@ -32,15 +31,8 @@ var (
|
||||
func init() {
|
||||
if config.AppConfig != nil && config.AppConfig.CacheEnabled {
|
||||
var err error
|
||||
// 优先使用增强版缓存
|
||||
// 使用增强版缓存
|
||||
enhancedTwoLevelCache, err = cache.NewEnhancedTwoLevelCache()
|
||||
if err == nil {
|
||||
cacheInitialized = true
|
||||
return
|
||||
}
|
||||
|
||||
// 如果增强版缓存初始化失败,回退到原始缓存
|
||||
twoLevelCache, err = cache.NewTwoLevelCache()
|
||||
if err == nil {
|
||||
cacheInitialized = true
|
||||
}
|
||||
@@ -57,16 +49,10 @@ func NewSearchService(pluginManager *plugin.PluginManager) *SearchService {
|
||||
// 检查缓存是否已初始化,如果未初始化则尝试重新初始化
|
||||
if !cacheInitialized && config.AppConfig != nil && config.AppConfig.CacheEnabled {
|
||||
var err error
|
||||
// 优先使用增强版缓存
|
||||
// 使用增强版缓存
|
||||
enhancedTwoLevelCache, err = cache.NewEnhancedTwoLevelCache()
|
||||
if err == nil {
|
||||
cacheInitialized = true
|
||||
} else {
|
||||
// 如果增强版缓存初始化失败,回退到原始缓存
|
||||
twoLevelCache, err = cache.NewTwoLevelCache()
|
||||
if err == nil {
|
||||
cacheInitialized = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -815,7 +801,7 @@ func (s *SearchService) searchTG(keyword string, channels []string, forceRefresh
|
||||
var hit bool
|
||||
var err error
|
||||
|
||||
// 优先使用增强版缓存
|
||||
// 使用增强版缓存
|
||||
if enhancedTwoLevelCache != nil {
|
||||
data, hit, err = enhancedTwoLevelCache.Get(cacheKey)
|
||||
|
||||
@@ -826,16 +812,6 @@ func (s *SearchService) searchTG(keyword string, channels []string, forceRefresh
|
||||
return results, nil
|
||||
}
|
||||
}
|
||||
} else if twoLevelCache != nil {
|
||||
data, hit, err = twoLevelCache.Get(cacheKey)
|
||||
|
||||
if err == nil && hit {
|
||||
var results []model.SearchResult
|
||||
if err := cache.DeserializeWithPool(data, &results); err == nil {
|
||||
// 直接返回缓存数据,不检查新鲜度
|
||||
return results, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -872,19 +848,13 @@ func (s *SearchService) searchTG(keyword string, channels []string, forceRefresh
|
||||
go func(res []model.SearchResult) {
|
||||
ttl := time.Duration(config.AppConfig.CacheTTLMinutes) * time.Minute
|
||||
|
||||
// 优先使用增强版缓存
|
||||
// 使用增强版缓存
|
||||
if enhancedTwoLevelCache != nil {
|
||||
data, err := enhancedTwoLevelCache.GetSerializer().Serialize(res)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
enhancedTwoLevelCache.Set(cacheKey, data, ttl)
|
||||
} else if twoLevelCache != nil {
|
||||
data, err := cache.SerializeWithPool(res)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
twoLevelCache.Set(cacheKey, data, ttl)
|
||||
}
|
||||
}(results)
|
||||
}
|
||||
@@ -908,7 +878,7 @@ func (s *SearchService) searchPlugins(keyword string, plugins []string, forceRef
|
||||
var hit bool
|
||||
var err error
|
||||
|
||||
// 优先使用增强版缓存
|
||||
// 使用增强版缓存
|
||||
if enhancedTwoLevelCache != nil {
|
||||
|
||||
// 使用Get方法,它会检查磁盘缓存是否有更新
|
||||
@@ -922,16 +892,6 @@ func (s *SearchService) searchPlugins(keyword string, plugins []string, forceRef
|
||||
return results, nil
|
||||
}
|
||||
}
|
||||
} else if twoLevelCache != nil {
|
||||
data, hit, err = twoLevelCache.Get(cacheKey)
|
||||
|
||||
if err == nil && hit {
|
||||
var results []model.SearchResult
|
||||
if err := cache.DeserializeWithPool(data, &results); err == nil {
|
||||
// 返回缓存数据
|
||||
return results, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1031,19 +991,13 @@ func (s *SearchService) searchPlugins(keyword string, plugins []string, forceRef
|
||||
go func(res []model.SearchResult) {
|
||||
ttl := time.Duration(config.AppConfig.CacheTTLMinutes) * time.Minute
|
||||
|
||||
// 优先使用增强版缓存
|
||||
// 使用增强版缓存
|
||||
if enhancedTwoLevelCache != nil {
|
||||
data, err := enhancedTwoLevelCache.GetSerializer().Serialize(res)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
enhancedTwoLevelCache.Set(cacheKey, data, ttl)
|
||||
} else if twoLevelCache != nil {
|
||||
data, err := cache.SerializeWithPool(res)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
twoLevelCache.Set(cacheKey, data, ttl)
|
||||
}
|
||||
}(allResults)
|
||||
}
|
||||
|
||||
@@ -3,8 +3,6 @@ package cache
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"pansou/config"
|
||||
)
|
||||
|
||||
// 简单的内存缓存项
|
||||
@@ -187,93 +185,4 @@ func (c *MemoryCache) StartCleanupTask() {
|
||||
c.CleanExpired()
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// 两级缓存
|
||||
type TwoLevelCache struct {
|
||||
memCache *MemoryCache
|
||||
diskCache *DiskCache
|
||||
}
|
||||
|
||||
// 创建新的两级缓存
|
||||
func NewTwoLevelCache() (*TwoLevelCache, error) {
|
||||
// 内存缓存大小为磁盘缓存的60%
|
||||
memCacheMaxItems := 5000
|
||||
memCacheSizeMB := config.AppConfig.CacheMaxSizeMB * 3 / 5
|
||||
|
||||
memCache := NewMemoryCache(memCacheMaxItems, memCacheSizeMB)
|
||||
memCache.StartCleanupTask()
|
||||
|
||||
diskCache, err := NewDiskCache(config.AppConfig.CachePath, config.AppConfig.CacheMaxSizeMB)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &TwoLevelCache{
|
||||
memCache: memCache,
|
||||
diskCache: diskCache,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 设置缓存
|
||||
func (c *TwoLevelCache) Set(key string, data []byte, ttl time.Duration) error {
|
||||
// 先设置内存缓存(这是快速操作,直接在当前goroutine中执行)
|
||||
c.memCache.Set(key, data, ttl)
|
||||
|
||||
// 异步设置磁盘缓存(这是IO操作,可能较慢)
|
||||
go func(k string, d []byte, t time.Duration) {
|
||||
// 使用独立的goroutine写入磁盘,避免阻塞调用者
|
||||
_ = c.diskCache.Set(k, d, t)
|
||||
}(key, data, ttl)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// 获取缓存
|
||||
func (c *TwoLevelCache) Get(key string) ([]byte, bool, error) {
|
||||
// 优先检查内存缓存
|
||||
if data, found := c.memCache.Get(key); found {
|
||||
return data, true, nil
|
||||
}
|
||||
|
||||
// 内存未命中,检查磁盘缓存
|
||||
data, found, err := c.diskCache.Get(key)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
if found {
|
||||
// 磁盘命中,更新内存缓存
|
||||
ttl := time.Duration(config.AppConfig.CacheTTLMinutes) * time.Minute
|
||||
c.memCache.Set(key, data, ttl)
|
||||
return data, true, nil
|
||||
}
|
||||
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
// 删除缓存
|
||||
func (c *TwoLevelCache) Delete(key string) error {
|
||||
// 从内存缓存删除
|
||||
c.memCache.mutex.Lock()
|
||||
if item, exists := c.memCache.items[key]; exists {
|
||||
c.memCache.currSize -= int64(item.size)
|
||||
delete(c.memCache.items, key)
|
||||
}
|
||||
c.memCache.mutex.Unlock()
|
||||
|
||||
// 从磁盘缓存删除
|
||||
return c.diskCache.Delete(key)
|
||||
}
|
||||
|
||||
// 清空所有缓存
|
||||
func (c *TwoLevelCache) Clear() error {
|
||||
// 清空内存缓存
|
||||
c.memCache.mutex.Lock()
|
||||
c.memCache.items = make(map[string]*memoryCacheItem)
|
||||
c.memCache.currSize = 0
|
||||
c.memCache.mutex.Unlock()
|
||||
|
||||
// 清空磁盘缓存
|
||||
return c.diskCache.Clear()
|
||||
}
|
||||
Reference in New Issue
Block a user