Feat: Introdue cache interface.
Merged
KA
4mo ago+201+201+012345678901234567890123456789-0-0-0123456789
This PR introduces a generic Cache interface (Put/Get/Delete) package, decoupling consumers from concrete implementations and making a reusable package across KubeVela repo. This enables swapping backends (sync.Map, hashicorp/golang-lru, etc.) without touching consumer code.
The first implementation, MemoryCache, uses sync.Map with per-entry TTL and a background eviction goroutine (migrated from the kubevela/kubevela/pkg/utils/cache.go with identical behaviour).
Summary by cubic
Introduce a generic cache.Cache and in-memory MemoryCacheStore with per-entry TTL and background eviction. Uses a simple Get/Put/Delete API with found semantics and context-aware cleanup.
- New Features
- Added
cache.Cache[K comparable]withGet(key) (value, found),Put(key, value, expiration), andDelete(key). - Implemented
MemoryCacheStoreusingsync.Map, a 3s sweep interval, and context-aware shutdown. - Added tests with
github.com/onsi/ginkgo/v2andgithub.com/onsi/gomegafor expiration, non-expiring entries, delete, multi-key, and overwrite.
- Added