fix memory cache.
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package conf
|
||||
|
||||
import "git.apinb.com/bsm-sdk/core/vars"
|
||||
import (
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
)
|
||||
|
||||
type Base struct {
|
||||
Service string `yaml:"Service"` // 服务名称
|
||||
@@ -93,3 +95,8 @@ type LogConf struct {
|
||||
File bool `yaml:"File"`
|
||||
Remote bool `yaml:"Remote"`
|
||||
}
|
||||
|
||||
type MemoryCacheConf struct {
|
||||
DefaultExpiration int `yaml:"DefaultExpiration"`
|
||||
CleanupInterval int `yaml:"CleanupInterval"`
|
||||
}
|
||||
|
||||
@@ -1,36 +1,24 @@
|
||||
package with
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"git.apinb.com/bsm-sdk/core/conf"
|
||||
"git.apinb.com/bsm-sdk/core/printer"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
"github.com/allegro/bigcache/v3"
|
||||
cache "github.com/patrickmn/go-cache"
|
||||
)
|
||||
|
||||
func Memory(opts *bigcache.Config) (cli *bigcache.BigCache) {
|
||||
func Memory(opts *conf.MemoryCacheConf) *cache.Cache {
|
||||
if opts == nil {
|
||||
opts = &bigcache.Config{
|
||||
Shards: 1024,
|
||||
LifeWindow: 10 * time.Minute,
|
||||
CleanWindow: 5 * time.Minute,
|
||||
MaxEntriesInWindow: 1000 * 10 * 60,
|
||||
MaxEntrySize: 500,
|
||||
Verbose: true,
|
||||
HardMaxCacheSize: 8192,
|
||||
OnRemove: nil,
|
||||
OnRemoveWithReason: nil,
|
||||
opts = &conf.MemoryCacheConf{
|
||||
DefaultExpiration: 60 * 60, // 1 hour
|
||||
CleanupInterval: 24 * 60 * 60, // 1 day
|
||||
}
|
||||
}
|
||||
|
||||
var err error
|
||||
cli, err = bigcache.New(context.Background(), *opts)
|
||||
if err != nil {
|
||||
printer.Error("Memory Cache Fatal Error")
|
||||
panic(err)
|
||||
}
|
||||
printer.Success("[BSM - %s] Memory Cache: DefaultExpiration=%d, CleanupInterval=%d", vars.ServiceKey, opts.DefaultExpiration, opts.CleanupInterval)
|
||||
|
||||
return cache.New(time.Duration(opts.DefaultExpiration)*time.Second, time.Duration(opts.CleanupInterval)*time.Second)
|
||||
|
||||
printer.Success("[BSM - %s] Memory Cache: Shards=%d, MaxEntrySize=%d", vars.ServiceKey, opts.Shards, opts.MaxEntrySize)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user