Browse Source

替换掉下载文件缓存的 badger 实现

Signed-off-by: allan716 <[email protected]>
allan716 3 years ago
parent
commit
176fe3e73e

+ 2 - 1
cmd/chinesesubfinder/main.go

@@ -6,6 +6,7 @@ import (
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/cron_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/file_downloader"
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/pre_job"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/cache_center"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/global_value"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
@@ -91,7 +92,7 @@ func main() {
 		}
 	}
 	// ----------------------------------------------
-	fileDownloader := file_downloader.NewFileDownloader(settings.GetSettings(), loggerBase)
+	fileDownloader := file_downloader.NewFileDownloader(cache_center.NewCacheCenter(settings.GetSettings(), loggerBase))
 	cronHelper := cron_helper.NewCronHelper(fileDownloader)
 	if settings.GetSettings().UserInfo.Username == "" || settings.GetSettings().UserInfo.Password == "" {
 		// 如果没有完成,那么就不开启

+ 14 - 14
internal/logic/file_downloader/downloader_hub.go

@@ -3,8 +3,7 @@ package file_downloader
 import (
 	"crypto/sha256"
 	"fmt"
-	"github.com/allanpk716/ChineseSubFinder/internal/logic/task_queue"
-	"github.com/allanpk716/ChineseSubFinder/internal/pkg/download_file_cache"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/cache_center"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
 	"github.com/allanpk716/ChineseSubFinder/internal/types/language"
@@ -15,14 +14,15 @@ import (
 )
 
 type FileDownloader struct {
-	Settings          *settings.Settings
-	Log               *logrus.Logger
-	downloadFileCache *download_file_cache.DownloadFileCache
+	Settings    *settings.Settings
+	Log         *logrus.Logger
+	cacheCenter *cache_center.CacheCenter
 }
 
-func NewFileDownloader(settings *settings.Settings, log *logrus.Logger) *FileDownloader {
-	return &FileDownloader{Settings: settings, Log: log,
-		downloadFileCache: download_file_cache.NewDownloadFileCache(settings)}
+func NewFileDownloader(cacheCenter *cache_center.CacheCenter) *FileDownloader {
+	return &FileDownloader{Settings: cacheCenter.Settings, Log: cacheCenter.Log,
+		cacheCenter: cacheCenter,
+	}
 }
 
 // Get supplierName 这个参数一定得是字幕源的名称,通过 s.GetSupplierName() 获取,否则后续的字幕源今日下载量将不能正确统计和判断
@@ -32,7 +32,7 @@ func (f *FileDownloader) Get(supplierName string, topN int64, videoFileName stri
 
 	fileUID := fmt.Sprintf("%x", sha256.Sum256([]byte(fileDownloadUrl)))
 
-	found, subInfo, err := f.downloadFileCache.Get(fileUID)
+	found, subInfo, err := f.cacheCenter.GetDownloadFile(fileUID)
 	if err != nil {
 		return nil, err
 	}
@@ -43,7 +43,7 @@ func (f *FileDownloader) Get(supplierName string, topN int64, videoFileName stri
 			return nil, err
 		}
 		// 下载成功需要统计到今天的次数中
-		_, err = task_queue.AddDailyDownloadCount(supplierName,
+		_, err = f.cacheCenter.AddDailyDownloadCount(supplierName,
 			my_util.GetPublicIP(f.Log, f.Settings.AdvancedSettings.TaskQueue, f.Settings.AdvancedSettings.ProxySettings))
 		if err != nil {
 			f.Log.Warningln(supplierName, "FileDownloader.Get.AddDailyDownloadCount", err)
@@ -57,7 +57,7 @@ func (f *FileDownloader) Get(supplierName string, topN int64, videoFileName stri
 		}
 		// 默认存入都是简体中文的语言类型,后续取出来的时候需要再次调用 SubParser 进行解析
 		inSubInfo := supplier.NewSubInfo(supplierName, topN, videoFileName, language.ChineseSimple, fileDownloadUrl, score, offset, ext, fileData)
-		err = f.downloadFileCache.Add(inSubInfo)
+		err = f.cacheCenter.AddDownloadFile(inSubInfo)
 		if err != nil {
 			return nil, err
 		}
@@ -74,7 +74,7 @@ func (f *FileDownloader) Get(supplierName string, topN int64, videoFileName stri
 func (f *FileDownloader) GetEx(supplierName string, browser *rod.Browser, subDownloadPageUrl string, TopN int64, Season, Episode int, downFileFunc func(browser *rod.Browser, subDownloadPageUrl string, TopN int64, Season, Episode int) (*supplier.SubInfo, error)) (*supplier.SubInfo, error) {
 
 	fileUID := fmt.Sprintf("%x", sha256.Sum256([]byte(subDownloadPageUrl)))
-	found, subInfo, err := f.downloadFileCache.Get(fileUID)
+	found, subInfo, err := f.cacheCenter.GetDownloadFile(fileUID)
 	if err != nil {
 		return nil, err
 	}
@@ -86,13 +86,13 @@ func (f *FileDownloader) GetEx(supplierName string, browser *rod.Browser, subDow
 			return nil, err
 		}
 		// 下载成功需要统计到今天的次数中
-		_, err = task_queue.AddDailyDownloadCount(supplierName,
+		_, err = f.cacheCenter.AddDailyDownloadCount(supplierName,
 			my_util.GetPublicIP(f.Log, f.Settings.AdvancedSettings.TaskQueue, f.Settings.AdvancedSettings.ProxySettings))
 		if err != nil {
 			f.Log.Warningln(supplierName, "FileDownloader.GetEx.AddDailyDownloadCount", err)
 		}
 		// 默认存入都是简体中文的语言类型,后续取出来的时候需要再次调用 SubParser 进行解析
-		err = f.downloadFileCache.Add(subInfo)
+		err = f.cacheCenter.AddDownloadFile(subInfo)
 		if err != nil {
 			return nil, err
 		}

+ 2 - 1
internal/logic/sub_supplier/shooter/shooter_test.go

@@ -2,6 +2,7 @@ package shooter
 
 import (
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/file_downloader"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/cache_center"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/unit_test_helper"
@@ -27,7 +28,7 @@ func TestNewSupplier(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
-	shooter := NewSupplier(file_downloader.NewFileDownloader(settings.NewSettings(), log_helper.GetLogger4Tester()))
+	shooter := NewSupplier(file_downloader.NewFileDownloader(cache_center.NewCacheCenter(settings.NewSettings(), log_helper.GetLogger4Tester())))
 	outList, err := shooter.getSubListFromFile(gVideoFPath)
 	if err != nil {
 		t.Error(err)

+ 4 - 3
internal/logic/sub_supplier/subhd/subhd_test.go

@@ -4,6 +4,7 @@ import (
 	"fmt"
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/file_downloader"
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/series_helper"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/cache_center"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/something_static"
@@ -28,7 +29,7 @@ func TestSupplier_GetSubListFromFile(t *testing.T) {
 	rootDir := unit_test_helper.GetTestDataResourceRootPath([]string{"sub_spplier"}, 5, true)
 	movie1 := filepath.Join(rootDir, "zimuku", "movies", "消失爱人 (2016)", "消失爱人 (2016) 720p AAC.rmvb")
 
-	subhd := NewSupplier(file_downloader.NewFileDownloader(settings.NewSettings(), log_helper.GetLogger4Tester()))
+	subhd := NewSupplier(file_downloader.NewFileDownloader(cache_center.NewCacheCenter(settings.NewSettings(), log_helper.GetLogger4Tester())))
 	outList, err := subhd.getSubListFromFile4Movie(movie1)
 	if err != nil {
 		t.Error(err)
@@ -65,7 +66,7 @@ func TestSupplier_GetSubListFromFile4Series(t *testing.T) {
 	if err != nil {
 		t.Fatal(err)
 	}
-	s := NewSupplier(file_downloader.NewFileDownloader(settings.NewSettings(), log_helper.GetLogger4Tester()))
+	s := NewSupplier(file_downloader.NewFileDownloader(cache_center.NewCacheCenter(settings.NewSettings(), log_helper.GetLogger4Tester())))
 	outList, err := s.GetSubListFromFile4Series(seriesInfo)
 	if err != nil {
 		t.Fatal(err)
@@ -87,7 +88,7 @@ func TestSupplier_getSubListFromKeyword4Movie(t *testing.T) {
 	//imdbID := "tt15299712" // 云南虫谷
 	//imdbID := "tt3626476" // Vacation Friends (2021)
 	getCode()
-	subhd := NewSupplier(file_downloader.NewFileDownloader(settings.NewSettings(), log_helper.GetLogger4Tester()))
+	subhd := NewSupplier(file_downloader.NewFileDownloader(cache_center.NewCacheCenter(settings.NewSettings(), log_helper.GetLogger4Tester())))
 	subInfos, err := subhd.getSubListFromKeyword4Movie(imdbID)
 	if err != nil {
 		t.Fatal(err)

+ 2 - 1
internal/logic/sub_supplier/xunlei/xunlei_test.go

@@ -2,6 +2,7 @@ package xunlei
 
 import (
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/file_downloader"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/cache_center"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/unit_test_helper"
@@ -29,7 +30,7 @@ func TestGetList(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	xunlie := NewSupplier(file_downloader.NewFileDownloader(settings.NewSettings(), log_helper.GetLogger4Tester()))
+	xunlie := NewSupplier(file_downloader.NewFileDownloader(cache_center.NewCacheCenter(settings.NewSettings(), log_helper.GetLogger4Tester())))
 	outList, err := xunlie.getSubListFromFile(gVideoFPath)
 	if err != nil {
 		t.Error(err)

+ 6 - 5
internal/logic/sub_supplier/zimuku/zimuku_test.go

@@ -3,6 +3,7 @@ package zimuku
 import (
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/file_downloader"
 	"github.com/allanpk716/ChineseSubFinder/internal/logic/series_helper"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/cache_center"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/log_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/rod_helper"
 	"github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
@@ -24,7 +25,7 @@ func TestSupplier_GetSubListFromKeyword(t *testing.T) {
 
 	//imdbId1 := "tt3228774"
 	videoName := "黑白魔女库伊拉"
-	s := NewSupplier(file_downloader.NewFileDownloader(settings.NewSettings(), log_helper.GetLogger4Tester()))
+	s := NewSupplier(file_downloader.NewFileDownloader(cache_center.NewCacheCenter(settings.NewSettings(), log_helper.GetLogger4Tester())))
 	outList, err := s.getSubListFromKeyword(browser, videoName)
 	if err != nil {
 		t.Error(err)
@@ -54,7 +55,7 @@ func TestSupplier_GetSubListFromFile(t *testing.T) {
 
 	rootDir := unit_test_helper.GetTestDataResourceRootPath([]string{"sub_spplier"}, 5, true)
 	movie1 := filepath.Join(rootDir, "zimuku", "movies", "The Devil All the Time (2020)", "The Devil All the Time (2020) WEBDL-1080p.mkv")
-	s := NewSupplier(file_downloader.NewFileDownloader(settings.NewSettings(), log_helper.GetLogger4Tester()))
+	s := NewSupplier(file_downloader.NewFileDownloader(cache_center.NewCacheCenter(settings.NewSettings(), log_helper.GetLogger4Tester())))
 	outList, err := s.getSubListFromMovie(browser, movie1)
 	if err != nil {
 		t.Error(err)
@@ -90,7 +91,7 @@ func TestSupplier_GetSubListFromFile4Series(t *testing.T) {
 		t.Fatal(err)
 	}
 
-	s := NewSupplier(file_downloader.NewFileDownloader(settings.NewSettings(), log_helper.GetLogger4Tester()))
+	s := NewSupplier(file_downloader.NewFileDownloader(cache_center.NewCacheCenter(settings.NewSettings(), log_helper.GetLogger4Tester())))
 	outList, err := s.GetSubListFromFile4Series(seriesInfo)
 	if err != nil {
 		t.Fatal(err)
@@ -128,7 +129,7 @@ func TestSupplier_getSubListFromKeyword(t *testing.T) {
 	//imdbID := "tt15299712" // 云南虫谷
 	//imdbID := "tt3626476"  // Vacation Friends (2021)
 	imdbID := "tt11192306" // Superman.and.Lois
-	zimuku := NewSupplier(file_downloader.NewFileDownloader(settings.NewSettings(), log_helper.GetLogger4Tester()))
+	zimuku := NewSupplier(file_downloader.NewFileDownloader(cache_center.NewCacheCenter(settings.NewSettings(), log_helper.GetLogger4Tester())))
 	subInfos, err := zimuku.getSubListFromKeyword(browser, imdbID)
 	if err != nil {
 		t.Fatal(err)
@@ -153,7 +154,7 @@ func TestSupplier_step3(t *testing.T) {
 
 func TestSupplier_CheckAlive(t *testing.T) {
 
-	s := NewSupplier(file_downloader.NewFileDownloader(settings.NewSettings(), log_helper.GetLogger4Tester()))
+	s := NewSupplier(file_downloader.NewFileDownloader(cache_center.NewCacheCenter(settings.NewSettings(), log_helper.GetLogger4Tester())))
 	alive, _ := s.CheckAlive()
 	if alive == false {
 		t.Fatal("CheckAlive == false")

+ 0 - 127
internal/logic/task_queue/daily_download_limit.go

@@ -1,127 +0,0 @@
-package task_queue
-
-import (
-	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
-	"github.com/dgraph-io/badger/v3"
-	"time"
-)
-
-// GetDailyDownloadCount 根据字幕提供者的名称,获取今日下载计数的次数,仅仅统计次数,并不确认是那个视频的字幕下载
-// whichDay nowTime.Format("2006-01-02")
-func GetDailyDownloadCount(supplierName string, publicIP string, whichDay ...string) (int, error) {
-
-	KeyName := ""
-	if len(whichDay) > 0 {
-		KeyName = supplierName + "_" + whichDay[0] + "_" + publicIP
-	} else {
-		nowTime := time.Now()
-		KeyName = supplierName + "_" + nowTime.Format("2006-01-02") + "_" + publicIP
-	}
-
-	outCount := 0
-	err := GetDb().View(
-		func(tx *badger.Txn) error {
-			var err error
-			tx.Discard()
-			key := []byte(MergeBucketAndKeyName(BucketNamePrefixSupplierDailyDownloadCounter, KeyName))
-			e, err := tx.Get(key)
-			if err != nil {
-				if err == badger.ErrKeyNotFound {
-					return nil
-				} else {
-					return err
-				}
-			}
-			valCopy, err := e.ValueCopy(nil)
-			if err != nil {
-				return err
-			}
-			outCount, err = my_util.BytesToInt(valCopy)
-			if err != nil {
-				return err
-			}
-			return nil
-		})
-	if err != nil {
-		return 0, err
-	}
-
-	return outCount, nil
-}
-
-// AddDailyDownloadCount 根据字幕提供者的名称,今日下载计数的次数+1,仅仅统计次数,并不确认是那个视频的字幕下载。TTL 多加 5s 确保今天过去(暂时去除 TTL)
-func AddDailyDownloadCount(supplierName string, publicIP string, whichDay ...string) (int, error) {
-
-	nowTime := time.Now()
-	// 今天剩余的时间(s)
-	//restOfDaySecond := my_util.GetRestOfDaySec()
-	KeyName := supplierName + "_" + nowTime.Format("2006-01-02") + "_" + publicIP
-
-	dailyDownloadCount, err := GetDailyDownloadCount(supplierName, publicIP, whichDay...)
-	if err != nil {
-		return 0, err
-	}
-
-	err = GetDb().Update(
-		func(tx *badger.Txn) error {
-
-			var err error
-			key := []byte(MergeBucketAndKeyName(BucketNamePrefixSupplierDailyDownloadCounter, KeyName))
-			dailyDownloadCount += 1
-			value, err := my_util.IntToBytes(dailyDownloadCount)
-			if err != nil {
-				return err
-			}
-			// 因为已经查询了一次,确保一定存在,所以直接更新+1,TTL 多加 5s 确保今天过去,暂时去除 TTL uint32(restOfDaySecond.Seconds())+5
-			// .WithTTL(2 * time.Second)
-			e := badger.NewEntry(key, value)
-			err = tx.SetEntry(e)
-			if err != nil {
-				return err
-			}
-			return nil
-		})
-	if err != nil {
-		return 0, err
-	}
-
-	return dailyDownloadCount, nil
-}
-
-// DelDailyDownloadCount 不是定版。这里需要搜索前缀去删除,因为引入了 Public IP 的分类
-func DelDailyDownloadCount(supplierName string, whichDay ...string) error {
-
-	KeyName := ""
-	if len(whichDay) > 0 {
-		KeyName = supplierName + "_" + whichDay[0]
-	} else {
-		nowTime := time.Now()
-		KeyName = supplierName + "_" + nowTime.Format("2006-01-02")
-	}
-
-	err := GetDb().Update(
-		func(tx *badger.Txn) error {
-
-			var err error
-			key := []byte(MergeBucketAndKeyName(BucketNamePrefixSupplierDailyDownloadCounter, KeyName))
-			// 因为已经查询了一次,确保一定存在,所以直接更新+1,TTL 多加 5s 确保今天过去,暂时去除 TTL uint32(restOfDaySecond.Seconds())+5
-			if err = tx.Delete(key); err != nil {
-				return err
-			}
-			return nil
-		})
-	if err != nil {
-		return err
-	}
-
-	return nil
-}
-
-// GetDailyVideoSubDownloadCount 今日有那些视频进行了字幕的下载
-func GetDailyVideoSubDownloadCount() {
-
-}
-
-func getDailyVideoSubDownloadCount(tx *badger.Txn, KeyName string) {
-
-}

+ 0 - 121
internal/logic/task_queue/daily_download_limit_test.go

@@ -1,121 +0,0 @@
-package task_queue
-
-import (
-	"testing"
-)
-
-const supplieName = "testtest"
-
-func TestGetDailyDownloadCount(t *testing.T) {
-	type args struct {
-		supplierName string
-		whichDay     []string
-	}
-	tests := []struct {
-		name    string
-		args    args
-		want    int
-		wantErr bool
-	}{
-		{name: "00", args: args{
-			supplierName: supplieName,
-			whichDay:     nil,
-		}, want: 0, wantErr: false},
-		{name: "01", args: args{
-			supplierName: supplieName,
-			whichDay:     []string{"2022-04-01"},
-		}, want: 0, wantErr: false},
-	}
-	for _, tt := range tests {
-		t.Run(tt.name, func(t *testing.T) {
-
-			got, err := GetDailyDownloadCount(tt.args.supplierName, "", tt.args.whichDay...)
-			if (err != nil) != tt.wantErr {
-				t.Errorf("GetDailyDownloadCount() error = %v, wantErr %v", err, tt.wantErr)
-				return
-			}
-			if got != tt.want {
-				t.Errorf("GetDailyDownloadCount() got = %v, want %v", got, tt.want)
-			}
-		})
-	}
-}
-
-func TestAddDailyDownloadCount(t *testing.T) {
-	type args struct {
-		supplierName string
-	}
-	tests := []struct {
-		name    string
-		args    args
-		want    int
-		wantErr bool
-	}{
-		{name: "00", args: args{
-			supplierName: supplieName,
-		}, want: 1, wantErr: false},
-	}
-	for _, tt := range tests {
-		t.Run(tt.name, func(t *testing.T) {
-
-			got, err := AddDailyDownloadCount(tt.args.supplierName, "")
-			if (err != nil) != tt.wantErr {
-				t.Errorf("AddDailyDownloadCount() error = %v, wantErr %v", err, tt.wantErr)
-				return
-			}
-			if got != tt.want {
-				t.Errorf("AddDailyDownloadCount() got = %v, want %v", got, tt.want)
-			}
-
-			got, err = AddDailyDownloadCount(tt.args.supplierName, "")
-			if (err != nil) != tt.wantErr {
-				t.Errorf("AddDailyDownloadCount() error = %v, wantErr %v", err, tt.wantErr)
-				return
-			}
-			if got != tt.want+1 {
-				t.Errorf("AddDailyDownloadCount() got = %v, want %v", got, tt.want)
-			}
-		})
-	}
-
-	err := DelDb()
-	if err != nil {
-		return
-	}
-}
-
-func TestAddGetDailyDownloadCount(t *testing.T) {
-
-	addCount, err := AddDailyDownloadCount(supplieName, "")
-	if err != nil {
-		t.Fatalf(err.Error())
-	}
-
-	getDailyDownloadCount, err := GetDailyDownloadCount(supplieName, "")
-	if err != nil {
-		t.Fatalf(err.Error())
-	}
-
-	if addCount != getDailyDownloadCount {
-		t.Fatalf("not the same")
-	}
-
-	addCount, err = AddDailyDownloadCount(supplieName, "")
-	if err != nil {
-		t.Fatalf(err.Error())
-	}
-
-	getDailyDownloadCount, err = GetDailyDownloadCount(supplieName, "")
-	if err != nil {
-		t.Fatalf(err.Error())
-	}
-
-	if addCount != getDailyDownloadCount {
-		t.Fatalf("not the same")
-	}
-
-	err = DelDb()
-	if err != nil {
-		return
-	}
-}

+ 50 - 0
internal/pkg/cache_center/cache_center.go

@@ -0,0 +1,50 @@
+package cache_center
+
+import (
+	"fmt"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/cache_center/models"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_folder"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
+	"github.com/sirupsen/logrus"
+	"gorm.io/driver/sqlite"
+	"gorm.io/gorm"
+	"path/filepath"
+	"sync"
+)
+
+type CacheCenter struct {
+	Settings                 *settings.Settings
+	Log                      *logrus.Logger
+	centerFolder             string
+	downloadFileSaveRootPath string
+	db                       *gorm.DB
+	locker                   sync.Mutex
+}
+
+func NewCacheCenter(Settings *settings.Settings, Log *logrus.Logger) *CacheCenter {
+
+	c := CacheCenter{}
+	c.Settings = Settings
+	c.Log = Log
+	var err error
+	c.centerFolder, err = my_folder.GetRootCacheCenterFolder()
+	if err != nil {
+		panic(err)
+	}
+	c.downloadFileSaveRootPath = filepath.Join(c.centerFolder, "download_files")
+	dbFPath := filepath.Join(c.centerFolder, dbFileName)
+	c.db, err = gorm.Open(sqlite.Open(dbFPath), &gorm.Config{})
+	if err != nil {
+		panic(fmt.Sprintf("failed to connect database, %s", err.Error()))
+	}
+	// 迁移 schema
+	err = c.db.AutoMigrate(&models.DailyDownloadInfo{}, &models.DailyDownloadInfo{})
+	if err != nil {
+		panic(fmt.Sprintf("db AutoMigrate error, %s", err.Error()))
+	}
+	return &c
+}
+
+const (
+	dbFileName = "cache_center.db"
+)

+ 60 - 0
internal/pkg/cache_center/daily_download_info.go

@@ -0,0 +1,60 @@
+package cache_center
+
+import (
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/cache_center/models"
+	"time"
+)
+
+// GetDailyDownloadCount 根据字幕提供者的名称,获取今日下载计数的次数,仅仅统计次数,并不确认是那个视频的字幕下载
+// whichDay nowTime.Format("2006-01-02")
+func (c *CacheCenter) GetDailyDownloadCount(supplierName string, publicIP string, whichDay ...string) (int, error) {
+	defer c.locker.Unlock()
+	c.locker.Lock()
+
+	var dailyDownloadInfos []models.DailyDownloadInfo
+	whichDayStr := ""
+	if len(whichDay) > 0 {
+		whichDayStr = whichDay[0]
+	} else {
+		nowTime := time.Now()
+		whichDayStr = nowTime.Format("2006-01-02")
+	}
+	c.db.Where("supplier_name = ? AND public_ip = ? AND  which_day = ", supplierName, publicIP, whichDayStr).Find(&dailyDownloadInfos)
+
+	if len(dailyDownloadInfos) == 0 {
+		// 不存在
+		return 0, nil
+	}
+
+	return dailyDownloadInfos[0].Count, nil
+}
+
+// AddDailyDownloadCount 根据字幕提供者的名称,今日下载计数的次数+1,仅仅统计次数,并不确认是哪个视频的字幕下载
+func (c *CacheCenter) AddDailyDownloadCount(supplierName string, publicIP string, whichDay ...string) (int, error) {
+	defer c.locker.Unlock()
+	c.locker.Lock()
+
+	dailyDownloadCount, err := c.GetDailyDownloadCount(supplierName, publicIP, whichDay...)
+	if err != nil {
+		return 0, err
+	}
+
+	whichDayStr := ""
+	if len(whichDay) > 0 {
+		whichDayStr = whichDay[0]
+	} else {
+		nowTime := time.Now()
+		whichDayStr = nowTime.Format("2006-01-02")
+	}
+
+	dailyDownloadCount += 1
+	dd := models.DailyDownloadInfo{
+		SupplierName: supplierName,
+		PublicIP:     publicIP,
+		Count:        dailyDownloadCount,
+		WhichDay:     whichDayStr,
+	}
+	c.db.Save(&dd)
+
+	return dailyDownloadCount, nil
+}

+ 86 - 0
internal/pkg/cache_center/download_file_info.go

@@ -0,0 +1,86 @@
+package cache_center
+
+import (
+	"encoding/json"
+	"errors"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/cache_center/models"
+	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
+	"github.com/allanpk716/ChineseSubFinder/internal/types/supplier"
+	"os"
+	"path/filepath"
+	"time"
+)
+
+func (c *CacheCenter) AddDownloadFile(subInfo *supplier.SubInfo) error {
+	defer c.locker.Unlock()
+	c.locker.Lock()
+
+	if subInfo.FileUrl == "" {
+		return errors.New("subInfo FileUrl is empty")
+	}
+
+	// 只支持秒或者小时为单位
+	tmpTTL := time.Duration(c.Settings.AdvancedSettings.DownloadFileCache.TTL) * time.Second
+	if c.Settings.AdvancedSettings.DownloadFileCache.Unit == "hour" {
+		tmpTTL = time.Duration(c.Settings.AdvancedSettings.DownloadFileCache.TTL) * time.Hour
+	} else {
+		tmpTTL = time.Duration(c.Settings.AdvancedSettings.DownloadFileCache.TTL) * time.Second
+	}
+
+	b, err := json.Marshal(subInfo)
+	if err != nil {
+		return err
+	}
+
+	// 保存到本地文件
+	todayString := time.Now().Format("2006-01-02")
+	saveFPath := filepath.Join(c.downloadFileSaveRootPath, todayString, subInfo.GetUID())
+	err = my_util.WriteFile(saveFPath, b)
+	if err != nil {
+		return err
+	}
+	relPath, err := filepath.Rel(c.downloadFileSaveRootPath, saveFPath)
+	if err != nil {
+		return err
+	}
+
+	df := models.DownloadFileInfo{
+		UID:            subInfo.GetUID(),
+		RelPath:        relPath,
+		ExpirationTime: time.Now().Add(tmpTTL),
+	}
+
+	c.db.Save(&df)
+
+	return nil
+}
+
+func (c *CacheCenter) GetDownloadFile(fileUrlUID string) (bool, *supplier.SubInfo, error) {
+	defer c.locker.Unlock()
+	c.locker.Lock()
+
+	var dfs []models.DownloadFileInfo
+	c.db.Where("uid = ?", fileUrlUID).Find(&dfs)
+
+	if len(dfs) == 0 {
+		return false, nil, nil
+	}
+
+	localFileFPath := filepath.Join(c.downloadFileSaveRootPath, dfs[0].RelPath)
+	if my_util.IsFile(localFileFPath) == false {
+		return false, nil, nil
+	}
+
+	bytes, err := os.ReadFile(localFileFPath)
+	if err != nil {
+		return false, nil, err
+	}
+
+	var subInfo supplier.SubInfo
+	err = json.Unmarshal(bytes, &subInfo)
+	if err != nil {
+		return false, nil, err
+	}
+
+	return true, &subInfo, nil
+}

+ 9 - 0
internal/pkg/cache_center/models/daily_download_info.go

@@ -0,0 +1,9 @@
+package models
+
+// DailyDownloadInfo 今日下载统计,每个下载源使用了多少的次数
+type DailyDownloadInfo struct {
+	SupplierName string `gorm:"column:supplier_name;type:varchar(255);not null" json:"supplier_name"`
+	PublicIP     string `gorm:"column:public_ip;type:varchar(255);not null" json:"public_ip"`
+	WhichDay     string `gorm:"column:which_day;type:varchar(255);not null" json:"which_day"`
+	Count        int    `gor:"column:count;type:int;not null;default:0" json:"count"`
+}

+ 9 - 0
internal/pkg/cache_center/models/download_file_info.go

@@ -0,0 +1,9 @@
+package models
+
+import "time"
+
+type DownloadFileInfo struct {
+	UID            string    `gorm:"column:uid;primary_key"`
+	RelPath        string    `gorm:"column:rel_path"`
+	ExpirationTime time.Time `gorm:"column:expiration_time"`
+}

+ 0 - 92
internal/pkg/download_file_cache/download_file_cahce.go

@@ -1,92 +0,0 @@
-package download_file_cache
-
-import (
-	"encoding/json"
-	"github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
-	"github.com/allanpk716/ChineseSubFinder/internal/types/supplier"
-	"github.com/dgraph-io/badger/v3"
-	"time"
-)
-
-type DownloadFileCache struct {
-	settings *settings.Settings
-}
-
-func NewDownloadFileCache(settings *settings.Settings) *DownloadFileCache {
-	return &DownloadFileCache{settings: settings}
-}
-
-func (d *DownloadFileCache) Get(fileUrlUID string) (bool, *supplier.SubInfo, error) {
-
-	var subInfo supplier.SubInfo
-	err := GetDb().View(
-		func(tx *badger.Txn) error {
-			var err error
-
-			key := []byte(fileUrlUID)
-			e, err := tx.Get(key)
-			if err != nil {
-
-				if err == badger.ErrKeyNotFound {
-					return nil
-				} else {
-					return err
-				}
-			}
-			valCopy, err := e.ValueCopy(nil)
-			if err != nil {
-				return err
-			}
-
-			err = json.Unmarshal(valCopy, &subInfo)
-			if err != nil {
-				return err
-			}
-			return nil
-		})
-	if err != nil {
-		return false, nil, err
-	}
-
-	if subInfo.GetUID() == "" {
-		return false, nil, nil
-	}
-	return true, &subInfo, nil
-}
-
-// Add 新增一个下载好的文件到缓存种,TTL 时间需要从 Settings 中去配置
-func (d DownloadFileCache) Add(subInfo *supplier.SubInfo) error {
-
-	err := GetDb().Update(
-		func(tx *badger.Txn) error {
-
-			var err error
-			key := []byte(subInfo.GetUID())
-			if err != nil {
-				return err
-			}
-
-			b, err := json.Marshal(subInfo)
-			if err != nil {
-				return err
-			}
-			// 只支持秒或者小时为单位
-			tmpTTL := time.Duration(d.settings.AdvancedSettings.DownloadFileCache.TTL) * time.Second
-			if d.settings.AdvancedSettings.DownloadFileCache.Unit == "hour" {
-				tmpTTL = time.Duration(d.settings.AdvancedSettings.DownloadFileCache.TTL) * time.Hour
-			} else {
-				tmpTTL = time.Duration(d.settings.AdvancedSettings.DownloadFileCache.TTL) * time.Second
-			}
-
-			e := badger.NewEntry(key, b).WithTTL(tmpTTL)
-			err = tx.SetEntry(e)
-			if err != nil {
-				return err
-			}
-			return nil
-		})
-	if err != nil {
-		return err
-	}
-	return nil
-}

+ 0 - 85
internal/pkg/download_file_cache/download_file_cahce_test.go

@@ -1,85 +0,0 @@
-package download_file_cache
-
-import (
-	"bytes"
-	"github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
-	"github.com/allanpk716/ChineseSubFinder/internal/types/language"
-	"github.com/allanpk716/ChineseSubFinder/internal/types/supplier"
-	"testing"
-	"time"
-)
-
-func TestDownloadFileCache_Get(t *testing.T) {
-
-	dfc := NewDownloadFileCache(settings.GetSettings())
-	found, _, err := dfc.Get("asd")
-	if err != nil {
-		t.Error(err)
-	}
-
-	if found == true {
-		t.Error("Found sub")
-	}
-}
-
-func TestDownloadFileCache_Add(t *testing.T) {
-
-	dfc := NewDownloadFileCache(settings.GetSettings())
-
-	inSubInfo := supplier.NewSubInfo("local", 1, "testfile", language.ChineseSimple, "2esd12c31c123asecqwec", 3, 0, ".srt", []byte{1, 2, 3, 4})
-	err := dfc.Add(inSubInfo)
-	if err != nil {
-		t.Error(err)
-	}
-
-	fileUID := inSubInfo.GetUID()
-
-	found, gotSubInfo, err := dfc.Get(fileUID)
-	if err != nil {
-		t.Error(err)
-	}
-
-	if found == false {
-		t.Error("Not Found sub")
-	}
-
-	if gotSubInfo.GetUID() != fileUID {
-		t.Error("Wrong UID")
-	}
-
-	if gotSubInfo.FileUrl != inSubInfo.FileUrl {
-		t.Error("Wrong FileUrl")
-	}
-
-	if bytes.Equal(gotSubInfo.Data, inSubInfo.Data) == false {
-		t.Error("Wrong Data")
-	}
-
-}
-
-func TestDownloadFileCache_AddTTL(t *testing.T) {
-
-	nowSettings := settings.GetSettings()
-	nowSettings.AdvancedSettings.DownloadFileCache.TTL = 3
-	nowSettings.AdvancedSettings.DownloadFileCache.Unit = "second"
-	dfc := NewDownloadFileCache(nowSettings)
-
-	inSubInfo := supplier.NewSubInfo("local", 1, "testfile", language.ChineseSimple, "2esd12c31c123asecqwec", 3, 0, ".srt", []byte{1, 2, 3, 4})
-	err := dfc.Add(inSubInfo)
-	if err != nil {
-		t.Error(err)
-	}
-
-	fileUID := inSubInfo.GetUID()
-
-	time.Sleep(time.Second * 4)
-
-	found, _, err := dfc.Get(fileUID)
-	if err != nil {
-		t.Error(err)
-	}
-
-	if found == true {
-		t.Error("Found sub")
-	}
-}

+ 0 - 59
internal/pkg/download_file_cache/init.go

@@ -1,59 +0,0 @@
-package download_file_cache
-
-import (
-	"github.com/allanpk716/ChineseSubFinder/internal/pkg/my_folder"
-	"github.com/dgraph-io/badger/v3"
-	"path/filepath"
-	"time"
-)
-
-func GetDb() *badger.DB {
-
-	if dbBase == nil {
-		var err error
-		opt := badger.DefaultOptions(getDbName())
-		// 1MB
-		opt.ValueLogFileSize = 1 << 20
-		// 10 MB
-		opt.MemTableSize = 10 << 20
-		opt.BlockCacheSize = 20 << 20
-		// 这边数据库会自动创建这个目录文件
-		dbBase, err = badger.Open(opt)
-		if err != nil {
-			panic(err)
-		}
-
-		go badgerGC(dbBase)
-	}
-	return dbBase
-}
-
-func DelDb() error {
-
-	if dbBase != nil {
-		_ = dbBase.Close()
-	}
-	return my_folder.ClearFolder(getDbName())
-}
-
-func getDbName() string {
-	return filepath.Join(my_folder.GetConfigRootDirFPath(), dbFileName)
-}
-
-func badgerGC(_dbBase *badger.DB) {
-	ticker := time.NewTicker(5 * time.Second)
-	defer ticker.Stop()
-	for range ticker.C {
-	again:
-		err := _dbBase.RunValueLogGC(0.5)
-		if err == nil {
-			goto again
-		}
-	}
-}
-
-const (
-	dbFileName = "download_sub_cache"
-)
-
-var dbBase *badger.DB

+ 16 - 0
internal/pkg/my_folder/folder.go

@@ -259,6 +259,21 @@ func GetRootSubFixCacheFolder() (string, error) {
 	return nowProcessRoot, err
 }
 
+// GetRootCacheCenterFolder 下载缓存、队列缓存、下载次数缓存的文件夹
+func GetRootCacheCenterFolder() (string, error) {
+
+	nowProcessRoot, err := os.Getwd()
+	if err != nil {
+		return "", err
+	}
+	nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, CacheCenterFloder)
+	err = os.MkdirAll(nowProcessRoot, os.ModePerm)
+	if err != nil {
+		return "", err
+	}
+	return nowProcessRoot, err
+}
+
 // GetSubFixCacheFolderByName 获取缓存的文件夹,没有则新建
 func GetSubFixCacheFolderByName(folderName string) (string, error) {
 	rootPath, err := GetRootSubFixCacheFolder()
@@ -498,6 +513,7 @@ const (
 	DebugFolder         = "CSF-DebugThings"   // 调试相关的文件夹
 	SubFixCacheFolder   = "CSF-SubFixCache"   // 字幕时间校正的缓存文件夹,一般可以不清理
 	ShareSubFileCache   = "CSF-ShareSubCache" // 字幕共享的缓存目录,不建议删除
+	CacheCenterFloder   = "CSF-CacheCenter"   // 下载缓存、队列缓存、下载次数缓存的文件夹
 )
 
 const (