From 697aaa1b2a41dfdc2ca27945eabc3fc3990f0ac0 Mon Sep 17 00:00:00 2001 From: kash2104 Date: Wed, 8 Jul 2026 09:06:42 +0530 Subject: [PATCH 1/5] feat: Introduce cache interface to define implementation methods. Signed-off-by: kash2104 --- cache/cache.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 cache/cache.go diff --git a/cache/cache.go b/cache/cache.go new file mode 100644 index 0000000..b0e06f5 --- /dev/null +++ b/cache/cache.go @@ -0,0 +1,15 @@ +package cache + +import "time" + +// Cache interface defines the methods for a cache implementation. +type Cache interface { + // Get retrieves a value from the cache by key. + Get(key interface{}) (value interface{}) + + // Put adds a value to the cache with the specified key and expiration time. + Put(key, value interface{}, expiration time.Duration) + + // Delete removes a value from the cache by key. + Delete(key interface{}) +} From ca1746f398cdca66e5279ade1dfd481e00714b2a Mon Sep 17 00:00:00 2001 From: kash2104 Date: Wed, 8 Jul 2026 09:07:59 +0530 Subject: [PATCH 2/5] feat: Cache implementation using map. Signed-off-by: kash2104 --- cache/map.go | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 cache/map.go diff --git a/cache/map.go b/cache/map.go new file mode 100644 index 0000000..5bf555f --- /dev/null +++ b/cache/map.go @@ -0,0 +1,91 @@ +package cache + +import ( + "context" + "sync" + "time" +) + +const ( + // DefaultSweepInterval Cache default sweep interval + DefaultSweepInterval = time.Second * 3 +) + +// memoryCache memory cache, support time expired +type memoryCache struct { + data interface{} + cacheDuration time.Duration + startTime time.Time +} + +// NewMemoryCache new memory cache instance +func NewMemoryCache(data interface{}, cacheDuration time.Duration) *memoryCache { + mc := &memoryCache{data: data, cacheDuration: cacheDuration, startTime: time.Now()} + return mc +} + +// IsExpired whether the cache data expires +func (m *memoryCache) IsExpired() bool { + if m.cacheDuration <= 0 { + return false + } + return time.Now().After(m.startTime.Add(m.cacheDuration)) +} + +// GetData get cache data +func (m *memoryCache) GetData() interface{} { + return m.data +} + +// MemoryCacheStore memory cache store +type MemoryCacheStore struct { + store sync.Map +} + +// NewMemoryCacheStore memory cache store +func NewMemoryCacheStore(ctx context.Context) *MemoryCacheStore { + mcs := &MemoryCacheStore{ + store: sync.Map{}, + } + go mcs.run(ctx) + return mcs +} + +// run start a goroutine to clear expired cache data +func (m *MemoryCacheStore) run(ctx context.Context) { + ticker := time.NewTicker(DefaultSweepInterval) + defer ticker.Stop() + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + m.store.Range(func(key, value interface{}) bool { + if value.(*memoryCache).IsExpired() { + m.store.Delete(key) + } + return true + }) + } + } +} + +// Get cache data from store, if cache data is expired, return nil +func (m *MemoryCacheStore) Get(key interface{}) (value interface{}) { + mc, ok := m.store.Load(key) + if ok && !mc.(*memoryCache).IsExpired() { + return mc.(*memoryCache).GetData() + } + return nil +} + +// Put cache data, if cacheDuration>0, store will clear data after timeout. +func (m *MemoryCacheStore) Put(key, value interface{}, cacheDuration time.Duration) { + mc := NewMemoryCache(value, cacheDuration) + m.store.Store(key, mc) +} + +// Delete cache data from store +func (m *MemoryCacheStore) Delete(key interface{}) { + m.store.Delete(key) +} From 778fb8120cc3ae3447897114a533ddb45208515c Mon Sep 17 00:00:00 2001 From: kash2104 Date: Sat, 11 Jul 2026 13:41:30 +0530 Subject: [PATCH 3/5] Test: Add tests for cache implemented using map. Signed-off-by: kash2104 --- cache/map_test.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 cache/map_test.go diff --git a/cache/map_test.go b/cache/map_test.go new file mode 100644 index 0000000..1dea005 --- /dev/null +++ b/cache/map_test.go @@ -0,0 +1,78 @@ +package cache + +/* +Copyright 2021 The KubeVela Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import ( + "context" + "fmt" + "testing" + "time" + + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" +) + +var _ = Describe("Test cache utils", func() { + It("should return false for IsExpired()", func() { + c := NewMemoryCache("test", 10*time.Hour) + Expect(c.IsExpired()).Should(BeFalse()) + }) + + It("test cache store", func() { + store := NewMemoryCacheStore(context.TODO()) + store.Put("test", "test data", time.Second*2) + store.Put("test2", "test data", 0) + store.Put("test3", "test data", -1) + time.Sleep(3 * time.Second) + Expect(store.Get("test")).Should(BeNil()) + Expect(store.Get("test2")).Should(Equal("test data")) + Expect(store.Get("test3")).Should(Equal("test data")) + }) + + It("test cache store delete key", func() { + store := NewMemoryCacheStore(context.TODO()) + store.Put("test", "test data", time.Minute*2) + store.Delete("test") + Expect(store.Get("test")).Should(BeNil()) + }) +}) + +var store *MemoryCacheStore + +// BenchmarkWrite +func BenchmarkWrite(b *testing.B) { + store = NewMemoryCacheStore(context.TODO()) + + for i := 0; i < b.N; i++ { + store.Put(fmt.Sprintf("%d", i), i, 0) + } +} + +// BenchmarkRead +func BenchmarkRead(b *testing.B) { + for i := 0; i < b.N; i++ { + store.Get(fmt.Sprintf("%d", i)) + } +} + +// BenchmarkRW +func BenchmarkRW(b *testing.B) { + for i := 0; i < b.N; i++ { + store.Put(fmt.Sprintf("%d", i), i, 1) + store.Get(fmt.Sprintf("%d", i)) + } +} From ac6254262298a8eccb2341662929a3200427f6c8 Mon Sep 17 00:00:00 2001 From: kash2104 Date: Sun, 12 Jul 2026 23:07:18 +0530 Subject: [PATCH 4/5] Refactor: Make cache K comparable. Signed-off-by: kash2104 --- cache/cache.go | 8 ++--- cache/map.go | 18 +++++------ cache/map_test.go | 77 +++++++++++++++++++++++++++++------------------ 3 files changed, 60 insertions(+), 43 deletions(-) diff --git a/cache/cache.go b/cache/cache.go index b0e06f5..e6ed347 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -3,13 +3,13 @@ package cache import "time" // Cache interface defines the methods for a cache implementation. -type Cache interface { +type Cache[K comparable] interface { // Get retrieves a value from the cache by key. - Get(key interface{}) (value interface{}) + Get(key K) (value interface{}, expired bool) // Put adds a value to the cache with the specified key and expiration time. - Put(key, value interface{}, expiration time.Duration) + Put(key K, value interface{}, expiration time.Duration) // Delete removes a value from the cache by key. - Delete(key interface{}) + Delete(key K) } diff --git a/cache/map.go b/cache/map.go index 5bf555f..8f35dec 100644 --- a/cache/map.go +++ b/cache/map.go @@ -38,13 +38,13 @@ func (m *memoryCache) GetData() interface{} { } // MemoryCacheStore memory cache store -type MemoryCacheStore struct { +type MemoryCacheStore[K comparable] struct { store sync.Map } // NewMemoryCacheStore memory cache store -func NewMemoryCacheStore(ctx context.Context) *MemoryCacheStore { - mcs := &MemoryCacheStore{ +func NewMemoryCacheStore[K comparable](ctx context.Context) *MemoryCacheStore[K] { + mcs := &MemoryCacheStore[K]{ store: sync.Map{}, } go mcs.run(ctx) @@ -52,7 +52,7 @@ func NewMemoryCacheStore(ctx context.Context) *MemoryCacheStore { } // run start a goroutine to clear expired cache data -func (m *MemoryCacheStore) run(ctx context.Context) { +func (m *MemoryCacheStore[K]) run(ctx context.Context) { ticker := time.NewTicker(DefaultSweepInterval) defer ticker.Stop() for { @@ -71,21 +71,21 @@ func (m *MemoryCacheStore) run(ctx context.Context) { } // Get cache data from store, if cache data is expired, return nil -func (m *MemoryCacheStore) Get(key interface{}) (value interface{}) { +func (m *MemoryCacheStore[K]) Get(key K) (value interface{}, expired bool) { mc, ok := m.store.Load(key) if ok && !mc.(*memoryCache).IsExpired() { - return mc.(*memoryCache).GetData() + return mc.(*memoryCache).GetData(), false } - return nil + return nil, true } // Put cache data, if cacheDuration>0, store will clear data after timeout. -func (m *MemoryCacheStore) Put(key, value interface{}, cacheDuration time.Duration) { +func (m *MemoryCacheStore[K]) Put(key K, value interface{}, cacheDuration time.Duration) { mc := NewMemoryCache(value, cacheDuration) m.store.Store(key, mc) } // Delete cache data from store -func (m *MemoryCacheStore) Delete(key interface{}) { +func (m *MemoryCacheStore[K]) Delete(key K) { m.store.Delete(key) } diff --git a/cache/map_test.go b/cache/map_test.go index 1dea005..5965a27 100644 --- a/cache/map_test.go +++ b/cache/map_test.go @@ -18,7 +18,7 @@ limitations under the License. import ( "context" - "fmt" + "strconv" "testing" "time" @@ -26,6 +26,11 @@ import ( . "github.com/onsi/gomega" ) +func TestCache(t *testing.T) { + RegisterFailHandler(Fail) + RunSpecs(t, "Cache Suite") +} + var _ = Describe("Test cache utils", func() { It("should return false for IsExpired()", func() { c := NewMemoryCache("test", 10*time.Hour) @@ -33,46 +38,58 @@ var _ = Describe("Test cache utils", func() { }) It("test cache store", func() { - store := NewMemoryCacheStore(context.TODO()) + store := NewMemoryCacheStore[string](context.TODO()) store.Put("test", "test data", time.Second*2) store.Put("test2", "test data", 0) store.Put("test3", "test data", -1) time.Sleep(3 * time.Second) - Expect(store.Get("test")).Should(BeNil()) - Expect(store.Get("test2")).Should(Equal("test data")) - Expect(store.Get("test3")).Should(Equal("test data")) + value, expired := store.Get("test") + Expect(value).Should(BeNil()) + Expect(expired).Should(BeTrue()) + + value, expired = store.Get("test2") + Expect(value).Should(Equal("test data")) + Expect(expired).Should(BeFalse()) + + value, expired = store.Get("test3") + Expect(value).Should(Equal("test data")) + Expect(expired).Should(BeFalse()) }) It("test cache store delete key", func() { - store := NewMemoryCacheStore(context.TODO()) + store := NewMemoryCacheStore[string](context.TODO()) store.Put("test", "test data", time.Minute*2) store.Delete("test") - Expect(store.Get("test")).Should(BeNil()) + value, expired := store.Get("test") + Expect(value).Should(BeNil()) + Expect(expired).Should(BeTrue()) }) -}) - -var store *MemoryCacheStore -// BenchmarkWrite -func BenchmarkWrite(b *testing.B) { - store = NewMemoryCacheStore(context.TODO()) + It("test cache store with multiple keys", func() { + store := NewMemoryCacheStore[string](context.TODO()) + for i := 0; i < 100; i++ { + key := "key-" + strconv.Itoa(i) + store.Put(key, i, 0) + } - for i := 0; i < b.N; i++ { - store.Put(fmt.Sprintf("%d", i), i, 0) - } -} + for i := 0; i < 100; i++ { + key := "key-" + strconv.Itoa(i) + value, expired := store.Get(key) + Expect(expired).Should(BeFalse()) + Expect(value).Should(Equal(i)) + } + }) -// BenchmarkRead -func BenchmarkRead(b *testing.B) { - for i := 0; i < b.N; i++ { - store.Get(fmt.Sprintf("%d", i)) - } -} + It("test cache store overwrite value", func() { + store := NewMemoryCacheStore[string](context.TODO()) + store.Put("rw", "v1", time.Second) + value, expired := store.Get("rw") + Expect(expired).Should(BeFalse()) + Expect(value).Should(Equal("v1")) -// BenchmarkRW -func BenchmarkRW(b *testing.B) { - for i := 0; i < b.N; i++ { - store.Put(fmt.Sprintf("%d", i), i, 1) - store.Get(fmt.Sprintf("%d", i)) - } -} + store.Put("rw", "v2", time.Second) + value, expired = store.Get("rw") + Expect(expired).Should(BeFalse()) + Expect(value).Should(Equal("v2")) + }) +}) From d0f4239d37291d92e13322936e1a8a080afa9fe8 Mon Sep 17 00:00:00 2001 From: kash2104 Date: Tue, 21 Jul 2026 14:16:52 +0530 Subject: [PATCH 5/5] Perf: Switch from expired to found for Get contract. Signed-off-by: kash2104 --- cache/cache.go | 2 +- cache/map.go | 8 ++++---- cache/map_test.go | 28 ++++++++++++++-------------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/cache/cache.go b/cache/cache.go index e6ed347..60df253 100644 --- a/cache/cache.go +++ b/cache/cache.go @@ -5,7 +5,7 @@ import "time" // Cache interface defines the methods for a cache implementation. type Cache[K comparable] interface { // Get retrieves a value from the cache by key. - Get(key K) (value interface{}, expired bool) + Get(key K) (value interface{}, found bool) // Put adds a value to the cache with the specified key and expiration time. Put(key K, value interface{}, expiration time.Duration) diff --git a/cache/map.go b/cache/map.go index 8f35dec..6b7a6f0 100644 --- a/cache/map.go +++ b/cache/map.go @@ -62,7 +62,7 @@ func (m *MemoryCacheStore[K]) run(ctx context.Context) { case <-ticker.C: m.store.Range(func(key, value interface{}) bool { if value.(*memoryCache).IsExpired() { - m.store.Delete(key) + m.store.CompareAndDelete(key, value) } return true }) @@ -71,12 +71,12 @@ func (m *MemoryCacheStore[K]) run(ctx context.Context) { } // Get cache data from store, if cache data is expired, return nil -func (m *MemoryCacheStore[K]) Get(key K) (value interface{}, expired bool) { +func (m *MemoryCacheStore[K]) Get(key K) (value interface{}, found bool) { mc, ok := m.store.Load(key) if ok && !mc.(*memoryCache).IsExpired() { - return mc.(*memoryCache).GetData(), false + return mc.(*memoryCache).GetData(), true } - return nil, true + return nil, false } // Put cache data, if cacheDuration>0, store will clear data after timeout. diff --git a/cache/map_test.go b/cache/map_test.go index 5965a27..69b0bbf 100644 --- a/cache/map_test.go +++ b/cache/map_test.go @@ -43,26 +43,26 @@ var _ = Describe("Test cache utils", func() { store.Put("test2", "test data", 0) store.Put("test3", "test data", -1) time.Sleep(3 * time.Second) - value, expired := store.Get("test") + value, found := store.Get("test") Expect(value).Should(BeNil()) - Expect(expired).Should(BeTrue()) + Expect(found).Should(BeFalse()) - value, expired = store.Get("test2") + value, found = store.Get("test2") Expect(value).Should(Equal("test data")) - Expect(expired).Should(BeFalse()) + Expect(found).Should(BeTrue()) - value, expired = store.Get("test3") + value, found = store.Get("test3") Expect(value).Should(Equal("test data")) - Expect(expired).Should(BeFalse()) + Expect(found).Should(BeTrue()) }) It("test cache store delete key", func() { store := NewMemoryCacheStore[string](context.TODO()) store.Put("test", "test data", time.Minute*2) store.Delete("test") - value, expired := store.Get("test") + value, found := store.Get("test") Expect(value).Should(BeNil()) - Expect(expired).Should(BeTrue()) + Expect(found).Should(BeFalse()) }) It("test cache store with multiple keys", func() { @@ -74,8 +74,8 @@ var _ = Describe("Test cache utils", func() { for i := 0; i < 100; i++ { key := "key-" + strconv.Itoa(i) - value, expired := store.Get(key) - Expect(expired).Should(BeFalse()) + value, found := store.Get(key) + Expect(found).Should(BeTrue()) Expect(value).Should(Equal(i)) } }) @@ -83,13 +83,13 @@ var _ = Describe("Test cache utils", func() { It("test cache store overwrite value", func() { store := NewMemoryCacheStore[string](context.TODO()) store.Put("rw", "v1", time.Second) - value, expired := store.Get("rw") - Expect(expired).Should(BeFalse()) + value, found := store.Get("rw") + Expect(found).Should(BeTrue()) Expect(value).Should(Equal("v1")) store.Put("rw", "v2", time.Second) - value, expired = store.Get("rw") - Expect(expired).Should(BeFalse()) + value, found = store.Get("rw") + Expect(found).Should(BeTrue()) Expect(value).Should(Equal("v2")) }) })