-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feat] - Add SizedLRU Cache #3344
Conversation
16c1db1
to
ecc3967
Compare
Is this meant to replace the existing memory cache? If not, what are the use cases of this vs that? |
I don't believe so. The plan is to use this for detection caching. |
ecc3967
to
59a7884
Compare
It might be worth tweaking the naming + comments in that case. "Memory" and "LRU" aren't exactly mutually exclusive and don't have clear purposes. |
e7fed02
to
f7e5b96
Compare
f7e5b96
to
51e6206
Compare
pkg/cache/sizedlru/sizedlru.go
Outdated
const defaultSize = 1 << 29 // 512MB | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a smaller default (like 8MB) would be sensible here.
pkg/cache/sizedlru/sizedlru.go
Outdated
// WithCapacity is a functional option to set the maximum capacity of the cache. | ||
// If the capacity is not set, the default value (512MB) is used. | ||
func WithCapacity[T any](capacity int) Option[T] { | ||
return func(lc *Cache[T]) { lc.capacity = capacity } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you document the units expected here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, great idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whoops, I got the cache implementations mixed up. This capacity refers to the number of items, not total size.
collectorOnce.Do(func() { | ||
collector = &MetricsCollector{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider having this called automatically when the package is imported using init()?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea, that makes sense. Good call.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also cleans up some of the test code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another option is to just declar them as package vars like we do with most others
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Love the metric instrumentation!
I'll update the name for the memory cache in a separate PR. |
* add impl for lru sized cache * update error message * address comments * rename * update comments
Description:
This PR introduces a size-limited, generic LRU cache with optional metrics tracking, which wraps the golang-lru caching library. Key features include customizable metrics collection (e.g., hits, misses, evictions) and configurable behavior through functional options like setting the maximum cache size.
Checklist:
make test-community
)?make lint
this requires golangci-lint)?