Skip to content
This repository was archived by the owner on Nov 5, 2024. It is now read-only.

Commit f84ce5d

Browse files
committed
[proxy]文件缓存增加“是否自动创建目录”选项
1 parent 7f9111e commit f84ce5d

3 files changed

Lines changed: 25 additions & 7 deletions

File tree

teacache/file.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type FileManager struct {
3232

3333
looper *timers.Looper
3434
dir string
35+
autoCreate bool // 是否自动创建
3536
writeLocker sync.RWMutex
3637
}
3738

@@ -89,13 +90,18 @@ func (this *FileManager) SetOptions(options map[string]interface{}) {
8990
this.Life = 1800 * time.Second
9091
}
9192

92-
dir, found := options["dir"]
93-
if found {
93+
dir, ok := options["dir"]
94+
if ok {
9495
this.dir = types.String(dir)
9596
if !filepath.IsAbs(this.dir) {
9697
this.dir = Tea.Root + Tea.DS + this.dir
9798
}
9899
}
100+
101+
autoCreate, ok := options["autoCreate"]
102+
if ok {
103+
this.autoCreate = types.Bool(autoCreate)
104+
}
99105
}
100106

101107
// 写入
@@ -110,7 +116,15 @@ func (this *FileManager) Write(key string, data []byte) error {
110116

111117
dirFile := files.NewFile(this.dir)
112118
if !dirFile.IsDir() {
113-
return errors.New("cache dir should be a valid dir")
119+
// 自动创建
120+
if this.autoCreate {
121+
err := dirFile.MkdirAll()
122+
if err != nil {
123+
return errors.New("can not create cache dir: " + err.Error())
124+
}
125+
} else {
126+
return errors.New("cache dir should be a valid dir")
127+
}
114128
}
115129

116130
md5 := stringutil.Md5(key)

teaweb/actions/default/cache/createPolicy.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ func (this *CreatePolicyAction) RunPost(params struct {
4242
SkipSetCookie bool
4343
EnableRequestCachePragma bool
4444

45-
FileDir string
45+
FileDir string
46+
FileAutoCreate bool
4647

4748
RedisNetwork string
4849
RedisHost string
@@ -88,7 +89,8 @@ func (this *CreatePolicyAction) RunPost(params struct {
8889
Field("fileDir", params.FileDir).
8990
Require("请输入缓存存放目录")
9091
policy.Options = map[string]interface{}{
91-
"dir": params.FileDir,
92+
"dir": params.FileDir,
93+
"autoCreate": params.FileAutoCreate,
9294
}
9395
case "redis":
9496
params.Must.

teaweb/actions/default/cache/updatePolicy.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ func (this *UpdatePolicyAction) RunPost(params struct {
8080
SkipSetCookie bool
8181
EnableRequestCachePragma bool
8282

83-
FileDir string
83+
FileDir string
84+
FileAutoCreate bool
8485

8586
RedisNetwork string
8687
RedisHost string
@@ -129,7 +130,8 @@ func (this *UpdatePolicyAction) RunPost(params struct {
129130
Field("fileDir", params.FileDir).
130131
Require("请输入缓存存放目录")
131132
policy.Options = map[string]interface{}{
132-
"dir": params.FileDir,
133+
"dir": params.FileDir,
134+
"autoCreate": params.FileAutoCreate,
133135
}
134136
case "redis":
135137
params.Must.

0 commit comments

Comments
 (0)