cache_center.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package cache_center
  2. import (
  3. "fmt"
  4. "github.com/allanpk716/ChineseSubFinder/internal/pkg/cache_center/models"
  5. "github.com/allanpk716/ChineseSubFinder/internal/pkg/my_folder"
  6. "github.com/allanpk716/ChineseSubFinder/internal/pkg/my_util"
  7. "github.com/allanpk716/ChineseSubFinder/internal/pkg/settings"
  8. "github.com/sirupsen/logrus"
  9. "gorm.io/driver/sqlite"
  10. "gorm.io/gorm"
  11. "os"
  12. "path/filepath"
  13. "sync"
  14. )
  15. type CacheCenter struct {
  16. Settings *settings.Settings
  17. Log *logrus.Logger
  18. centerFolder string
  19. downloadFileSaveRootPath string
  20. taskQueueSaveRootPath string
  21. dbFPath string
  22. cacheName string
  23. db *gorm.DB
  24. locker sync.Mutex
  25. }
  26. func NewCacheCenter(cacheName string, Settings *settings.Settings, Log *logrus.Logger) *CacheCenter {
  27. c := CacheCenter{}
  28. c.Settings = Settings
  29. c.Log = Log
  30. var err error
  31. c.centerFolder, err = my_folder.GetRootCacheCenterFolder()
  32. if err != nil {
  33. panic(err)
  34. }
  35. c.taskQueueSaveRootPath = filepath.Join(c.centerFolder, taskQueueFolderName, cacheName)
  36. c.downloadFileSaveRootPath = filepath.Join(c.centerFolder, downloadFilesFolderName, cacheName)
  37. c.dbFPath = filepath.Join(c.centerFolder, cacheName+"_"+dbFileName)
  38. c.db, err = gorm.Open(sqlite.Open(c.dbFPath), &gorm.Config{})
  39. if err != nil {
  40. panic(fmt.Sprintf("failed to connect database, %s", err.Error()))
  41. }
  42. // 迁移 schema
  43. err = c.db.AutoMigrate(&models.DailyDownloadInfo{}, &models.TaskQueueInfo{}, &models.DownloadFileInfo{})
  44. if err != nil {
  45. panic(fmt.Sprintf("db AutoMigrate error, %s", err.Error()))
  46. }
  47. return &c
  48. }
  49. func (c *CacheCenter) GetName() string {
  50. return c.cacheName
  51. }
  52. func (c *CacheCenter) Close() {
  53. defer c.locker.Unlock()
  54. c.locker.Lock()
  55. sqlDB, err := c.db.DB()
  56. if err != nil {
  57. return
  58. }
  59. err = sqlDB.Close()
  60. if err != nil {
  61. return
  62. }
  63. }
  64. func DelDb(cacheName string) {
  65. centerFolder, err := my_folder.GetRootCacheCenterFolder()
  66. if err != nil {
  67. return
  68. }
  69. dbFPath := filepath.Join(centerFolder, cacheName+"_"+dbFileName)
  70. if my_util.IsFile(dbFPath) == true {
  71. err = os.Remove(dbFPath)
  72. if err != nil {
  73. return
  74. }
  75. }
  76. taskQueueSaveRootPath := filepath.Join(centerFolder, taskQueueFolderName, cacheName)
  77. err = my_folder.ClearFolder(taskQueueSaveRootPath)
  78. if err != nil {
  79. return
  80. }
  81. downloadFileSaveRootPath := filepath.Join(centerFolder, downloadFilesFolderName, cacheName)
  82. err = my_folder.ClearFolder(downloadFileSaveRootPath)
  83. if err != nil {
  84. return
  85. }
  86. }
  87. const (
  88. taskQueueFolderName = "task_queue"
  89. downloadFilesFolderName = "download_files"
  90. dbFileName = "cache_center.db"
  91. )