From 4ff7dbd7b27170824fe2125ba77f460b05b47776 Mon Sep 17 00:00:00 2001 From: xxxsen Date: Tue, 28 Jan 2025 20:54:41 +0800 Subject: [PATCH 1/2] feat: auto delete expired data --- store/sqlite_storage.go | 42 ++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/store/sqlite_storage.go b/store/sqlite_storage.go index 9f81acc..90e54b8 100644 --- a/store/sqlite_storage.go +++ b/store/sqlite_storage.go @@ -11,19 +11,32 @@ import ( _ "github.com/glebarez/go-sqlite" ) +const ( + defaultExpireTime = 90 * 24 * time.Hour //默认存储3个月, 超过就删了吧, 实在没想到有啥东西需要永久存储的? +) + type sqliteStore struct { db *sql.DB } -func (s *sqliteStore) init() error { - createTable := `CREATE TABLE IF NOT EXISTS cache_tab ( - key TEXT PRIMARY KEY, - value BLOB, - expire_at INTEGER - );` - _, err := s.db.Exec(createTable) - if err != nil { - return err +func (s *sqliteStore) init(ctx context.Context) error { + execList := []struct { + sql string + args []interface{} + }{ + {`CREATE TABLE IF NOT EXISTS cache_tab ( + key TEXT PRIMARY KEY, + value BLOB, + expire_at INTEGER + );`, nil}, + {"CREATE INDEX if not exists idx_expireat on cache_tab(expire_at);", nil}, + {"DELETE from cache_tab where expire_at <= ?", []interface{}{time.Now().Unix()}}, + } + for _, item := range execList { + _, err := s.db.ExecContext(ctx, item.sql, item.args...) + if err != nil { + return err + } } return nil } @@ -31,7 +44,7 @@ func (s *sqliteStore) init() error { func (s *sqliteStore) GetData(ctx context.Context, key string) ([]byte, error) { var val []byte now := time.Now().Unix() - err := s.db.QueryRow("SELECT value FROM cache_tab WHERE key = ? and (expire_at > ? or expire_at = 0)", key, now).Scan(&val) + err := s.db.QueryRowContext(ctx, "SELECT value FROM cache_tab WHERE key = ? and expire_at > ?", key, now).Scan(&val) if err != nil { return nil, err } @@ -40,10 +53,13 @@ func (s *sqliteStore) GetData(ctx context.Context, key string) ([]byte, error) { func (s *sqliteStore) PutData(ctx context.Context, key string, value []byte, expire time.Duration) error { var expireAt int64 = 0 + if expire == 0 { + expire = defaultExpireTime //use default expire time + } if expire > 0 { expireAt = time.Now().Add(expire).Unix() } - _, err := s.db.Exec("INSERT OR REPLACE INTO cache_tab (key, value, expire_at) VALUES (?, ?, ?)", key, value, expireAt) + _, err := s.db.ExecContext(ctx, "INSERT OR REPLACE INTO cache_tab (key, value, expire_at) VALUES (?, ?, ?)", key, value, expireAt) if err != nil { return err } @@ -53,7 +69,7 @@ func (s *sqliteStore) PutData(ctx context.Context, key string, value []byte, exp func (s *sqliteStore) IsDataExist(ctx context.Context, key string) (bool, error) { var cnt int64 now := time.Now().Unix() - err := s.db.QueryRow("SELECT count(*) FROM cache_tab WHERE key = ? and (expire_at > ? or expire_at = 0)", key, now).Scan(&cnt) + err := s.db.QueryRowContext(ctx, "SELECT count(*) FROM cache_tab WHERE key = ? and expire_at > ?", key, now).Scan(&cnt) if errors.Is(err, sql.ErrNoRows) { return false, nil } @@ -76,7 +92,7 @@ func NewSqliteStorage(path string) (IStorage, error) { return nil, err } s := &sqliteStore{db: db} - if err := s.init(); err != nil { + if err := s.init(context.Background()); err != nil { return nil, err } return s, nil From 4f2b9135b2110e40e8d9dd42b0bac19fd5b346f1 Mon Sep 17 00:00:00 2001 From: xxxsen Date: Tue, 28 Jan 2025 21:01:43 +0800 Subject: [PATCH 2/2] refactor: actor naming --- capture/capture.go | 5 +---- utils/name_utils.go | 17 +++++++++++++++-- utils/name_utils_test.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 utils/name_utils_test.go diff --git a/capture/capture.go b/capture/capture.go index 8806d05..61ba937 100644 --- a/capture/capture.go +++ b/capture/capture.go @@ -143,10 +143,7 @@ func (c *Capture) resolveSaveDir(fc *model.FileContext) error { date := ts.Format(time.DateOnly) year := fmt.Sprintf("%d", ts.Year()) month := fmt.Sprintf("%d", ts.Month()) - actor := "佚名" - if len(fc.Meta.Actors) > 0 { - actor = utils.BuildAuthorsName(fc.Meta.Actors, 256) - } + actor := utils.BuildAuthorsName(fc.Meta.Actors) m := map[string]interface{}{ NamingReleaseDate: date, NamingReleaseYear: year, diff --git a/utils/name_utils.go b/utils/name_utils.go index a5894df..80e8f9c 100644 --- a/utils/name_utils.go +++ b/utils/name_utils.go @@ -2,13 +2,26 @@ package utils import "bytes" -func BuildAuthorsName(acts []string, maxLength int) string { +const ( + defaultNonActorName = "佚名" + defaultMultiActorLimit = 3 + defaultMultiActorAsName = "多人作品" + defaultMaxActorCharactor = 256 +) + +func BuildAuthorsName(acts []string) string { + if len(acts) == 0 { + return defaultNonActorName + } + if len(acts) >= defaultMultiActorLimit { + return defaultMultiActorAsName + } buf := bytes.NewBuffer(nil) for idx, item := range acts { if idx != 0 { buf.WriteString(",") } - if buf.Len()+1+len(item) > maxLength { + if buf.Len()+1+len(item) > defaultMaxActorCharactor { break } buf.WriteString(item) diff --git a/utils/name_utils_test.go b/utils/name_utils_test.go new file mode 100644 index 0000000..722f6b2 --- /dev/null +++ b/utils/name_utils_test.go @@ -0,0 +1,33 @@ +package utils + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +type testSt struct { + acts []string + result string +} + +func TestName(t *testing.T) { + testList := []testSt{ + { + acts: []string{"hello", "world"}, + result: "hello,world", + }, + { + acts: []string{}, + result: defaultNonActorName, + }, + { + acts: []string{"1", "2", "3", "4", "5"}, + result: defaultMultiActorAsName, + }, + } + for _, item := range testList { + name := BuildAuthorsName(item.acts) + assert.Equal(t, item.result, name) + } +}