From b9d144353eb378f15e5fc162f4884ed5e2db895b Mon Sep 17 00:00:00 2001 From: yanweidong Date: Sat, 20 Sep 2025 10:55:06 +0800 Subject: [PATCH] =?UTF-8?q?```=20refactor(cache):=20=E7=A7=BB=E9=99=A4?= =?UTF-8?q?=E5=86=85=E5=AD=98=E7=BC=93=E5=AD=98=E5=92=8CRedis=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E7=9A=84=E5=88=9D=E5=A7=8B=E5=8C=96=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 移除了mem包中内存缓存的初始化函数,以及with包中Redis缓存的初始化函数。 这些缓存初始化逻辑将被重构到其他位置或采用新的实现方式。 ``` --- cache/mem/mem.go | 14 ------------- with/memory.go | 39 +++++++++++++++++++++++++++++++++++++ with/{cache.go => redis.go} | 0 3 files changed, 39 insertions(+), 14 deletions(-) delete mode 100644 cache/mem/mem.go create mode 100644 with/memory.go rename with/{cache.go => redis.go} (100%) diff --git a/cache/mem/mem.go b/cache/mem/mem.go deleted file mode 100644 index 0de3b98..0000000 --- a/cache/mem/mem.go +++ /dev/null @@ -1,14 +0,0 @@ -package mem - -import ( - "git.apinb.com/bsm-sdk/core/vars" - "github.com/FishGoddess/cachego" -) - -func New() cachego.Cache { - return cachego.NewCache( - cachego.WithGC(vars.MemGcDuration), - cachego.WithShardings(vars.MemShardings), - cachego.WithLFU(vars.MemLRUMaxNumber), - ) -} diff --git a/with/memory.go b/with/memory.go new file mode 100644 index 0000000..caf8eef --- /dev/null +++ b/with/memory.go @@ -0,0 +1,39 @@ +package with + +import ( + "context" + "encoding/json" + "time" + + "git.apinb.com/bsm-sdk/core/print" + "git.apinb.com/bsm-sdk/core/vars" + "github.com/allegro/bigcache/v3" +) + +func Memory(opts *bigcache.Config, cli *bigcache.BigCache) { + config := 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, + } + + if opts != nil { + config = *opts + } + + var err error + cli, err = bigcache.New(context.Background(), config) + if err != nil { + print.Error("Memory Cache Fatal Error") + panic(err) + } + + jsonBytes, _ := json.Marshal(config) + print.Success("[BSM - %s] Memory Cache: %s", vars.ServiceKey, jsonBytes) +} diff --git a/with/cache.go b/with/redis.go similarity index 100% rename from with/cache.go rename to with/redis.go