Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 45 additions & 1 deletion cache/cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cache

import "time"
import (
"time"
)

// Cache interface defines the methods for a cache implementation.
type Cache[K comparable] interface {
Expand All @@ -13,3 +15,45 @@ type Cache[K comparable] interface {
// Delete removes a value from the cache by key.
Delete(key K)
}

// LRUCache interface defines the methods for a Least Recently Used (LRU) cache implementation.
type LRUCache[K comparable, V any] interface {
// Get retrieves a value from the cache by key.
Get(key K) (value V, found bool)

// Put adds a value to the cache with the specified key and expiration time.
Put(key K, value V, expiration time.Duration)

// Delete removes a value from the cache by key.
Delete(key K)
}

// EvictionReason represents the reason for evicting an item from the cache.
type EvictionReason string

const (
// EvictCapacity indicates that the item was evicted due to reaching the maximum capacity of the cache.
EvictCapacity EvictionReason = "capacity"
// EvictTTL indicates that the item was evicted due to exceeding its time-to-live (TTL).
EvictTTL EvictionReason = "TTL"
// EvictReplace indicates that the item was evicted due to being replaced by a new value.
EvictReplace EvictionReason = "replace"
// EvictDelete indicates that the item was evicted due to being explicitly deleted from the cache.
EvictDelete EvictionReason = "delete"
// EvictPurge indicates that the item was evicted due to a purge operation on the cache.
EvictPurge EvictionReason = "purge"
)

// Options struct defines the configuration options for the LRU cache.
type Options[K comparable, V any] struct {
// MaxSize defines the maximum number of items the cache can hold.
MaxSize int // 0 = unlimited count
// MaxBytes defines the maximum bytes the cache can hold.
MaxBytes int64 // 0 = unlimited bytes
// SizeOf is a function that calculates the byte usage of the value
SizeOf func(key K, value V) int64 // required if MaxBytes > 0
// OnEvict is a callback function that is called when an item is evicted from the cache.
OnEvict func(key K, value V, reason EvictionReason)
// SweepInterval defines the interval at which the cache will sweep for expired items.
SweepInterval time.Duration
}
235 changes: 235 additions & 0 deletions cache/lru.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
package cache

import (
"context"
"fmt"
"math"
"sync"
"sync/atomic"
"time"

hashicorp "github.com/hashicorp/golang-lru/v2"
)

const (
// DefaultMemoryCacheSweepInterval is the default interval for sweeping expired items from the cache.
DefaultMemoryCacheSweepInterval = time.Minute * 5
)

type lruCache[V any] struct {
data V
cacheDuration time.Duration
startTime time.Time
memorySize int64
evictionReason EvictionReason
}

type evictionEvent[K comparable, V any] struct {
key K
value *lruCache[V]
}

// NewLRUCache creates a new lruCache entry with the given value and TTL
func NewLRUCache[V any](data V, cacheDuration time.Duration) *lruCache[V] {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: NewLRUCache is exported but returns *lruCache[V], an unexported type. Callers outside this package can only use the value via type inference (:=) and can't name the type directly, which limits usability of this public constructor. Either export the lruCache type or make NewLRUCache unexported if it's only meant for internal use.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At cache/lru.go, line 33:

<comment>`NewLRUCache` is exported but returns `*lruCache[V]`, an unexported type. Callers outside this package can only use the value via type inference (`:=`) and can't name the type directly, which limits usability of this public constructor. Either export the `lruCache` type or make `NewLRUCache` unexported if it's only meant for internal use.</comment>

<file context>
@@ -0,0 +1,235 @@
+}
+
+// NewLRUCache creates a new lruCache entry with the given value and TTL
+func NewLRUCache[V any](data V, cacheDuration time.Duration) *lruCache[V] {
+	lc := &lruCache[V]{data: data, cacheDuration: cacheDuration, startTime: time.Now(), evictionReason: EvictCapacity}
+
</file context>

lc := &lruCache[V]{data: data, cacheDuration: cacheDuration, startTime: time.Now(), evictionReason: EvictCapacity}

return lc
}

// IsExpired checks if the cache entry's TTL has elapsed.
func (l *lruCache[V]) IsExpired() bool {
if l.cacheDuration <= 0 {
return false
}
return time.Now().After(l.startTime.Add(l.cacheDuration))
}

// LRUStore is a thread-safe LRU cache implementation that supports expiration and memory size limits.
type LRUStore[K comparable, V any] struct {
store *hashicorp.Cache[K, *lruCache[V]]
// maximumMemory Maximum Memory byte size of the cache
maximumMemory int64 // 0 = unlimited bytes
// sizeOf computes memory usage for each key/value entry.
sizeOf func(key K, value V) int64
// sweepInterval controls how often expired entries are removed.
sweepInterval time.Duration
// onEvict callback when an entry is evicted.
OnEvict func(key K, value V, reason EvictionReason)
// currentMemory Memory used by the cache
currentMemory int64
// mu mutex for synchronizing access to the cache
mu sync.Mutex
// pendingEvicts holds the list of eviction events to be processed after releasing the lock
pendingEvicts []evictionEvent[K, V]
}

// NewLRUStore creates a new LRUStore with the given options.
func NewLRUStore[K comparable, V any](ctx context.Context, opts Options[K, V]) (*LRUStore[K, V], error) {
if opts.MaxSize <= 0 {
// 0 = unlimited count: use the largest possible size so that only
// the byte budget (MaxBytes) drives eviction.
opts.MaxSize = math.MaxInt
}

if opts.MaxBytes < 0 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: A negative MaxBytes silently becomes an unlimited cache, despite only zero being documented as unlimited. Return a configuration error for negative limits instead of disabling the bound.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At cache/lru.go, line 74:

<comment>A negative `MaxBytes` silently becomes an unlimited cache, despite only zero being documented as unlimited. Return a configuration error for negative limits instead of disabling the bound.</comment>

<file context>
@@ -0,0 +1,235 @@
+		opts.MaxSize = math.MaxInt
+	}
+
+	if opts.MaxBytes < 0 {
+		opts.MaxBytes = 0
+	}
</file context>

opts.MaxBytes = 0
}

if opts.MaxBytes > 0 && opts.SizeOf == nil {
return nil, fmt.Errorf("SizeOf function must be provided when MaxBytes is greater than 0")
}

if opts.SweepInterval <= 0 {
opts.SweepInterval = DefaultMemoryCacheSweepInterval
}

lc := &LRUStore[K, V]{
maximumMemory: opts.MaxBytes,
sizeOf: opts.SizeOf,
sweepInterval: opts.SweepInterval,
OnEvict: opts.OnEvict,
currentMemory: 0,
}

lru, err := hashicorp.NewWithEvict(opts.MaxSize, func(key K, value *lruCache[V]) {
if value != nil {
atomic.AddInt64(&lc.currentMemory, -value.memorySize)
}
if lc.OnEvict != nil && value != nil {
lc.pendingEvicts = append(lc.pendingEvicts, evictionEvent[K, V]{
key: key,
value: value,
})
}
})

if err != nil {
return nil, err
}

lc.store = lru

go lc.run(ctx)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: NewLRUStore never validates ctx. If a caller passes a nil context, the background run goroutine panics on ctx.Done(). Consider defaulting to context.Background() when ctx == nil, or documenting that a non-nil context is required.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At cache/lru.go, line 112:

<comment>`NewLRUStore` never validates `ctx`. If a caller passes a `nil` context, the background `run` goroutine panics on `ctx.Done()`. Consider defaulting to `context.Background()` when `ctx == nil`, or documenting that a non-nil context is required.</comment>

<file context>
@@ -0,0 +1,235 @@
+
+	lc.store = lru
+
+	go lc.run(ctx)
+
+	return lc, nil
</file context>
Suggested change
go lc.run(ctx)
if ctx == nil {
ctx = context.Background()
}
go lc.run(ctx)


return lc, nil

}

// run starts a goroutine that periodically sweeps the cache for expired items.
func (l *LRUStore[K, V]) run(ctx context.Context) {
ticker := time.NewTicker(l.sweepInterval)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return
case <-ticker.C:
l.mu.Lock()
for _, key := range l.store.Keys() {
val, ok := l.store.Peek(key)
if ok && val != nil && val.IsExpired() {
val.evictionReason = EvictTTL
l.store.Remove(key)
}
}
l.executeEviction()
}
}
}

// Get retrieves a value from the cache by key.
func (l *LRUStore[K, V]) Get(key K) (value V, found bool) {
l.mu.Lock()
defer l.executeEviction()
lc, ok := l.store.Get(key)

if !ok {
return
}

if lc.IsExpired() {
lc.evictionReason = EvictTTL
l.store.Remove(key)
return
}

return lc.data, true
}

// Put adds a value to the cache with the specified key and expiration time.
func (l *LRUStore[K, V]) Put(key K, value V, cacheDuration time.Duration) {
l.mu.Lock()
defer l.executeEviction()
lc := NewLRUCache(value, cacheDuration)

if l.sizeOf != nil {
lc.memorySize = l.sizeOf(key, value)
}

if l.maximumMemory != 0 && lc.memorySize > l.maximumMemory {
return
}

if prevValue, ok := l.store.Peek(key); ok && prevValue != nil {
prevValue.evictionReason = EvictReplace
l.store.Remove(key)
}

if l.maximumMemory != 0 {
for atomic.LoadInt64(&l.currentMemory)+lc.memorySize > l.maximumMemory {
_, _, ok := l.store.RemoveOldest()
if !ok {
// If there is nothing left to evict, refuse the write.
return
}
}
}

// track bytes whenever sizeOf is provided, even if maximumMemory is 0.
if l.sizeOf != nil {
atomic.AddInt64(&l.currentMemory, lc.memorySize)
}

l.store.Add(key, lc)

}

// Delete removes a value from the cache by key.
func (l *LRUStore[K, V]) Delete(key K) {
l.mu.Lock()
defer l.executeEviction()
if val, ok := l.store.Peek(key); ok && val != nil {
val.evictionReason = EvictDelete
l.store.Remove(key)
}
}

// CurrentBytes returns the current memory usage of the cache in bytes.
func (l *LRUStore[K, V]) CurrentBytes() int64 {
return atomic.LoadInt64(&l.currentMemory)
}

// Purge removes all items from the cache.
func (l *LRUStore[K, V]) Purge() {
l.mu.Lock()
defer l.executeEviction()
for _, key := range l.store.Keys() {
if val, ok := l.store.Peek(key); ok && val != nil {
val.evictionReason = EvictPurge
}
}
l.store.Purge()
}

func (l *LRUStore[K, V]) executeEviction() {
evicts := l.pendingEvicts
l.pendingEvicts = nil
l.mu.Unlock()

for _, evict := range evicts {
if l.OnEvict != nil && evict.value != nil {
l.OnEvict(evict.key, evict.value.data, evict.value.evictionReason)
}
}
}
Loading
Loading