Feat: Introdue cache interface.

Merged
KA
4mo ago
+201-0

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] with Get(key) (value, found), Put(key, value, expiration), and Delete(key).
    • Implemented MemoryCacheStore using sync.Map, a 3s sweep interval, and context-aware shutdown.
    • Added tests with github.com/onsi/ginkgo/v2 and github.com/onsi/gomega for expiration, non-expiring entries, delete, multi-key, and overwrite.