From ffa4a6410720f64c3c4aa1fcbdce730901752e03 Mon Sep 17 00:00:00 2001 From: yanweidong Date: Wed, 8 Apr 2026 20:54:32 +0800 Subject: [PATCH] fix memory cache. --- conf/types.go | 9 ++++++++- go.mod | 2 +- with/memory.go | 30 +++++++++--------------------- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/conf/types.go b/conf/types.go index 999343e..b5cd613 100644 --- a/conf/types.go +++ b/conf/types.go @@ -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"` +} diff --git a/go.mod b/go.mod index 1aa22f2..f035346 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module git.apinb.com/bsm-sdk/core -go 1.26.0 +go 1.26.1 diff --git a/with/memory.go b/with/memory.go index c52f5ce..bbf5495 100644 --- a/with/memory.go +++ b/with/memory.go @@ -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 }