Skip to content

Commit

Permalink
Merge pull request #20 from xxxsen/xxxsen/feature/refactor_storage_clean
Browse files Browse the repository at this point in the history
Xxxsen/feature/refactor storage clean
  • Loading branch information
xxxsen authored Jan 28, 2025
2 parents 641c2fa + 4f2b913 commit 9db8bed
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 19 deletions.
5 changes: 1 addition & 4 deletions capture/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
42 changes: 29 additions & 13 deletions store/sqlite_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,40 @@ 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
}

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
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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
Expand Down
17 changes: 15 additions & 2 deletions utils/name_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions utils/name_utils_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 9db8bed

Please sign in to comment.