Skip to content

Commit 7d6d3b8

Browse files
varg1714jyxjjj
andauthored
feat(fs): Support customizing the cache time for a specific path (#1533)
* feat(fs): Support customizing the cache time for a specific path * feat(fs): Get the cache rule for driver information. * feat(fs): Support globbing. * feat(fs): Add log. --------- Signed-off-by: ShenLin <773933146@qq.com> Co-authored-by: ShenLin <773933146@qq.com>
1 parent 5480d61 commit 7d6d3b8

File tree

5 files changed

+53
-14
lines changed

5 files changed

+53
-14
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ require (
1919
github.com/avast/retry-go v3.0.0+incompatible
2020
github.com/aws/aws-sdk-go v1.55.7
2121
github.com/blevesearch/bleve/v2 v2.5.2
22+
github.com/bmatcuk/doublestar/v4 v4.9.1
2223
github.com/caarlos0/env/v9 v9.0.0
2324
github.com/charmbracelet/bubbles v0.21.0
2425
github.com/charmbracelet/bubbletea v1.3.6

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ github.com/blevesearch/zapx/v15 v15.4.2 h1:sWxpDE0QQOTjyxYbAVjt3+0ieu8NCE0fDRaFx
180180
github.com/blevesearch/zapx/v15 v15.4.2/go.mod h1:1pssev/59FsuWcgSnTa0OeEpOzmhtmr/0/11H0Z8+Nw=
181181
github.com/blevesearch/zapx/v16 v16.2.4 h1:tGgfvleXTAkwsD5mEzgM3zCS/7pgocTCnO1oyAUjlww=
182182
github.com/blevesearch/zapx/v16 v16.2.4/go.mod h1:Rti/REtuuMmzwsI8/C/qIzRaEoSK/wiFYw5e5ctUKKs=
183+
github.com/bmatcuk/doublestar/v4 v4.9.1 h1:X8jg9rRZmJd4yRy7ZeNDRnM+T3ZfHv15JiBJ/avrEXE=
184+
github.com/bmatcuk/doublestar/v4 v4.9.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
183185
github.com/bodgit/plumbing v1.3.0 h1:pf9Itz1JOQgn7vEOE7v7nlEfBykYqvUYioC61TwWCFU=
184186
github.com/bodgit/plumbing v1.3.0/go.mod h1:JOTb4XiRu5xfnmdnDJo6GmSbSbtSyufrsyZFByMtKEs=
185187
github.com/bodgit/sevenzip v1.6.1 h1:kikg2pUMYC9ljU7W9SaqHXhym5HyKm8/M/jd31fYan4=

internal/model/storage.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ import (
55
)
66

77
type Storage struct {
8-
ID uint `json:"id" gorm:"primaryKey"` // unique key
9-
MountPath string `json:"mount_path" gorm:"unique" binding:"required"` // must be standardized
10-
Order int `json:"order"` // use to sort
11-
Driver string `json:"driver"` // driver used
12-
CacheExpiration int `json:"cache_expiration"` // cache expire time
13-
Status string `json:"status"`
14-
Addition string `json:"addition" gorm:"type:text"` // Additional information, defined in the corresponding driver
15-
Remark string `json:"remark"`
16-
Modified time.Time `json:"modified"`
17-
Disabled bool `json:"disabled"` // if disabled
18-
DisableIndex bool `json:"disable_index"`
19-
EnableSign bool `json:"enable_sign"`
8+
ID uint `json:"id" gorm:"primaryKey"` // unique key
9+
MountPath string `json:"mount_path" gorm:"unique" binding:"required"` // must be standardized
10+
Order int `json:"order"` // use to sort
11+
Driver string `json:"driver"` // driver used
12+
CacheExpiration int `json:"cache_expiration"` // cache expire time
13+
CustomCachePolicies string `json:"custom_cache_policies" gorm:"type:text"`
14+
Status string `json:"status"`
15+
Addition string `json:"addition" gorm:"type:text"` // Additional information, defined in the corresponding driver
16+
Remark string `json:"remark"`
17+
Modified time.Time `json:"modified"`
18+
Disabled bool `json:"disabled"` // if disabled
19+
DisableIndex bool `json:"disable_index"`
20+
EnableSign bool `json:"enable_sign"`
2021
Sort
2122
Proxy
2223
}

internal/op/driver.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ func getMainItems(config driver.Config) []driver.Item {
8080
Required: true,
8181
Help: "The cache expiration time for this storage",
8282
})
83+
items = append(items, driver.Item{
84+
Name: "custom_cache_policies",
85+
Type: conf.TypeText,
86+
Default: "",
87+
Required: false,
88+
Help: "The cache expiration rules for this storage",
89+
})
8390
}
8491
if config.MustProxy() {
8592
items = append(items, driver.Item{

internal/op/fs.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
stdpath "path"
66
"strconv"
7+
"strings"
78
"time"
89

910
"github.com/OpenListTeam/OpenList/v4/internal/conf"
@@ -13,6 +14,7 @@ import (
1314
"github.com/OpenListTeam/OpenList/v4/internal/stream"
1415
"github.com/OpenListTeam/OpenList/v4/pkg/singleflight"
1516
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
17+
"github.com/bmatcuk/doublestar/v4"
1618
"github.com/pkg/errors"
1719
log "github.com/sirupsen/logrus"
1820
"golang.org/x/time/rate"
@@ -71,8 +73,34 @@ func List(ctx context.Context, storage driver.Driver, path string, args model.Li
7173
if !storage.Config().NoCache {
7274
if len(files) > 0 {
7375
log.Debugf("set cache: %s => %+v", key, files)
74-
ttl := time.Minute * time.Duration(storage.GetStorage().CacheExpiration)
75-
Cache.dirCache.SetWithTTL(key, newDirectoryCache(files), ttl)
76+
77+
ttl := storage.GetStorage().CacheExpiration
78+
79+
customCachePolicies := storage.GetStorage().CustomCachePolicies
80+
if len(customCachePolicies) > 0 {
81+
configPolicies := strings.Split(customCachePolicies, "\n")
82+
for _, configPolicy := range configPolicies {
83+
policy := strings.Split(strings.TrimSpace(configPolicy), ":")
84+
if len(policy) != 2 {
85+
log.Warnf("Malformed custom cache policy entry: %s in storage %s for path %s. Expected format: pattern:ttl", configPolicy, storage.GetStorage().MountPath, path)
86+
continue
87+
}
88+
if match, err1 := doublestar.Match(policy[0], path); err1 != nil {
89+
log.Warnf("Invalid glob pattern in custom cache policy: %s, error: %v", policy[0], err1)
90+
continue
91+
} else if !match {
92+
continue
93+
}
94+
95+
if configTtl, err1 := strconv.ParseInt(policy[1], 10, 64); err1 == nil {
96+
ttl = int(configTtl)
97+
break
98+
}
99+
}
100+
}
101+
102+
duration := time.Minute * time.Duration(ttl)
103+
Cache.dirCache.SetWithTTL(key, newDirectoryCache(files), duration)
76104
} else {
77105
log.Debugf("del cache: %s", key)
78106
Cache.deleteDirectoryTree(key)

0 commit comments

Comments
 (0)