Compare commits

..

1 Commits
v0.1.9 ... main

Author SHA1 Message Date
ffa4a64107 fix memory cache. 2026-04-08 20:54:32 +08:00
3 changed files with 18 additions and 23 deletions

View File

@@ -1,6 +1,8 @@
package conf package conf
import "git.apinb.com/bsm-sdk/core/vars" import (
"git.apinb.com/bsm-sdk/core/vars"
)
type Base struct { type Base struct {
Service string `yaml:"Service"` // 服务名称 Service string `yaml:"Service"` // 服务名称
@@ -93,3 +95,8 @@ type LogConf struct {
File bool `yaml:"File"` File bool `yaml:"File"`
Remote bool `yaml:"Remote"` Remote bool `yaml:"Remote"`
} }
type MemoryCacheConf struct {
DefaultExpiration int `yaml:"DefaultExpiration"`
CleanupInterval int `yaml:"CleanupInterval"`
}

2
go.mod
View File

@@ -1,3 +1,3 @@
module git.apinb.com/bsm-sdk/core module git.apinb.com/bsm-sdk/core
go 1.26.0 go 1.26.1

View File

@@ -1,36 +1,24 @@
package with package with
import ( import (
"context"
"time" "time"
"git.apinb.com/bsm-sdk/core/conf"
"git.apinb.com/bsm-sdk/core/printer" "git.apinb.com/bsm-sdk/core/printer"
"git.apinb.com/bsm-sdk/core/vars" "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 { if opts == nil {
opts = &bigcache.Config{ opts = &conf.MemoryCacheConf{
Shards: 1024, DefaultExpiration: 60 * 60, // 1 hour
LifeWindow: 10 * time.Minute, CleanupInterval: 24 * 60 * 60, // 1 day
CleanWindow: 5 * time.Minute,
MaxEntriesInWindow: 1000 * 10 * 60,
MaxEntrySize: 500,
Verbose: true,
HardMaxCacheSize: 8192,
OnRemove: nil,
OnRemoveWithReason: nil,
} }
} }
var err error printer.Success("[BSM - %s] Memory Cache: DefaultExpiration=%d, CleanupInterval=%d", vars.ServiceKey, opts.DefaultExpiration, opts.CleanupInterval)
cli, err = bigcache.New(context.Background(), *opts)
if err != nil { return cache.New(time.Duration(opts.DefaultExpiration)*time.Second, time.Duration(opts.CleanupInterval)*time.Second)
printer.Error("Memory Cache Fatal Error")
panic(err)
}
printer.Success("[BSM - %s] Memory Cache: Shards=%d, MaxEntrySize=%d", vars.ServiceKey, opts.Shards, opts.MaxEntrySize)
return
} }