Skip to content

Commit

Permalink
🔧 Custom cache duration
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Dec 30, 2021
1 parent cf25069 commit 582f7bb
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
4 changes: 2 additions & 2 deletions alist.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func main() {
base := fmt.Sprintf("%s:%d", conf.Conf.Address, conf.Conf.Port)
log.Infof("start server @ %s", base)
var err error
if conf.Conf.Https {
err = r.RunTLS(base, conf.Conf.CertFile, conf.Conf.KeyFile)
if conf.Conf.Scheme.Https {
err = r.RunTLS(base, conf.Conf.Scheme.CertFile, conf.Conf.Scheme.KeyFile)
} else {
err = r.Run(base)
}
Expand Down
6 changes: 5 additions & 1 deletion bootstrap/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import (
// InitCache init cache
func InitCache() {
log.Infof("init cache...")
goCacheClient := goCache.New(60*time.Minute, 120*time.Minute)
c := conf.Conf.Cache
if c.Expiration == 0 {
c.Expiration, c.CleanupInterval = 60, 120
}
goCacheClient := goCache.New(time.Duration(c.Expiration)*time.Minute, time.Duration(c.CleanupInterval)*time.Minute)
goCacheStore := store.NewGoCache(goCacheClient, nil)
conf.Cache = cache.New(goCacheStore)
}
29 changes: 22 additions & 7 deletions conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,25 @@ type Database struct {
TablePrefix string `json:"table_prefix"`
DBFile string `json:"db_file"`
}

type Scheme struct {
Https bool `json:"https"`
CertFile string `json:"cert_file"`
KeyFile string `json:"key_file"`
}

type CacheConfig struct {
Expiration int64 `json:"expiration"`
CleanupInterval int64 `json:"cleanup_interval"`
}

type Config struct {
Address string `json:"address"`
Port int `json:"port"`
Database Database `json:"database"`
Https bool `json:"https"`
CertFile string `json:"cert_file"`
KeyFile string `json:"key_file"`
Local bool `json:"local"`
Address string `json:"address"`
Port int `json:"port"`
Local bool `json:"local"`
Database Database `json:"database"`
Scheme Scheme `json:"scheme"`
Cache CacheConfig `json:"cache"`
}

func DefaultConfig() *Config {
Expand All @@ -30,5 +41,9 @@ func DefaultConfig() *Config {
TablePrefix: "x_",
DBFile: "data/data.db",
},
Cache: CacheConfig{
Expiration: 60,
CleanupInterval: 120,
},
}
}

0 comments on commit 582f7bb

Please sign in to comment.