Skip to content

Commit e69edd2

Browse files
committed
Make default cache lifetime configurable
1 parent 9ae5e86 commit e69edd2

File tree

8 files changed

+46
-16
lines changed

8 files changed

+46
-16
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/pelletier/go-toml/v2 v2.2.2
1010
github.com/tidwall/gjson v1.18.0
1111
github.com/triole/logseal v0.0.0-20240105053125-57d1f8179998
12+
github.com/xhit/go-str2duration/v2 v2.1.0
1213
github.com/yuin/goldmark v1.7.4
1314
github.com/yuin/goldmark-meta v1.1.0
1415
gopkg.in/yaml.v2 v2.3.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
3737
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
3838
github.com/triole/logseal v0.0.0-20240105053125-57d1f8179998 h1:n0ZH/b04bCbXkBtg1uq72EgSD05pPFu6ET5HkG/lsV0=
3939
github.com/triole/logseal v0.0.0-20240105053125-57d1f8179998/go.mod h1:Kd42uqPXKnS9vC5MW5FpuS2vIZ1QOe7y185uqBTtFlQ=
40+
github.com/xhit/go-str2duration/v2 v2.1.0 h1:lxklc02Drh6ynqX+DdPyp5pCKLUQpRT8bp8Ydu2Bstc=
41+
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
4042
github.com/yuin/goldmark v1.7.4 h1:BDXOHExt+A7gwPCJgPIIq7ENvceR7we7rOS9TNoLZeg=
4143
github.com/yuin/goldmark v1.7.4/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
4244
github.com/yuin/goldmark-meta v1.1.0 h1:pWw+JLHGZe8Rk0EGsMVssiNb/AaPMHfSRszZeUeiOUc=

src/conf/conf.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ func (conf *Conf) readConfig() {
4343
},
4444
)
4545
}
46+
47+
conf.DefaultCacheLifetime, err = conf.Util.Str2Dur(
48+
content.DefaultCacheLifetimeStr,
49+
)
50+
conf.Lg.IfErrFatal(
51+
"can not parse cache lifetime setting",
52+
logseal.F{"error": err},
53+
)
54+
4655
val.EpURL = key
4756
conf.API[key] = val
4857
}

src/conf/struct.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
package conf
22

33
import (
4+
"time"
45
"tyson-tap/src/util"
56

67
"github.com/triole/logseal"
78
)
89

910
type Conf struct {
10-
FileName string
11-
Threads int
12-
Port int
13-
API map[string]Endpoint
14-
Util util.Util
15-
Lg logseal.Logseal
11+
FileName string
12+
Threads int
13+
Port int
14+
DefaultCacheLifetime time.Duration
15+
API map[string]Endpoint
16+
Util util.Util
17+
Lg logseal.Logseal
1618
}
1719

1820
type ConfContent struct {
19-
Port int `yaml:"port"`
20-
API map[string]Endpoint `yaml:"api"`
21+
Port int `yaml:"port"`
22+
API map[string]Endpoint `yaml:"api"`
23+
DefaultCacheLifetimeStr string `yaml:"default_cache_lifetime"`
2124
}
2225

2326
type Endpoint struct {

src/indexer/init.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package indexer
22

33
import (
4-
"time"
54
"tyson-tap/src/conf"
65
"tyson-tap/src/util"
76

@@ -23,10 +22,13 @@ type Indexer struct {
2322

2423
func Init(conf conf.Conf, util util.Util, lg logseal.Logseal) (idx Indexer) {
2524
idx = Indexer{
26-
Cache: cache.New(5*time.Minute, 10*time.Minute),
27-
Conf: conf,
28-
Util: util,
29-
Lg: lg,
25+
Cache: cache.New(
26+
conf.DefaultCacheLifetime,
27+
conf.DefaultCacheLifetime*2,
28+
),
29+
Conf: conf,
30+
Util: util,
31+
Lg: lg,
3032
}
3133
return idx
3234
}

src/indexer/server.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ import (
1717
func (ind Indexer) RunServer() {
1818
http.HandleFunc("/", ind.ServeContent)
1919
portstr := strconv.Itoa(ind.Conf.Port)
20-
ind.Lg.Info("run server, listen at :" + portstr + "/")
20+
ind.Lg.Info(
21+
"run server, listen at :"+portstr+"/",
22+
logseal.F{"default_cache_lifetime": ind.Conf.DefaultCacheLifetime},
23+
)
2124
err := http.ListenAndServe(":"+portstr, nil)
2225
if err != nil {
2326
panic(err)

src/util/util.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import (
88
"regexp"
99
"runtime"
1010
"strings"
11+
"time"
1112
"unicode/utf8"
1213

1314
"github.com/triole/logseal"
15+
str2duration "github.com/xhit/go-str2duration/v2"
1416
yaml "gopkg.in/yaml.v2"
1517
)
1618

@@ -207,6 +209,11 @@ func (ut Util) RxReplaceAll(basestring, regex, newstring string) (r string) {
207209
return
208210
}
209211

212+
func (ut Util) Str2Dur(s string) (dur time.Duration, err error) {
213+
dur, err = str2duration.ParseDuration(s)
214+
return
215+
}
216+
210217
func (ut Util) StringifySliceOfInterfaces(itf []interface{}) (r []string) {
211218
for _, el := range itf {
212219
r = append(r, el.(string))

testdata/conf.yaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
---
22
port: 17777
3+
# use like: 180s, 3m... 0 for no caching
4+
default_cache_lifetime: 5m
5+
36
api:
47
file.json:
58
source: "{{.confdir}}/dump/html/1.html"
@@ -90,13 +93,13 @@ api:
9093
content: true
9194

9295
# http part
93-
simple_return:
96+
simple_serve.json:
9497
source: |
9598
hello
9699
there
97100
method: serve
98101

99-
repos_unmod:
102+
repos_unmod.json:
100103
source: http://localhost/repos
101104
method: http_get
102105
return:

0 commit comments

Comments
 (0)