Skip to content
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

Scan method on the LRU to iterate over the keys and values #34

Merged
merged 3 commits into from
Sep 6, 2024

Conversation

iuga
Copy link
Collaborator

@iuga iuga commented Sep 5, 2024

Overview

Scanning the key values in an LRU cache allows you to efficiently manage, analyze, and manipulate the cached data. By scanning, you can identify which keys are still relevant, which ones can be evicted, and how to optimize cache performance or resource utilization.

Important

As we are utilizing the latest Golang iterators, the minimum required Go version for this package is now v1.23.

Use Cases

Cache Preloading (Warm-up): In scenarios where anticipated high traffic or load spikes are expected, scanning the cache keys can identify critical data that should be retained or preloaded. This process, known as cache warm-up, ensures that essential data is readily available, reducing latency and improving system performance during peak times.
Usage Analytics and Cache Tuning: Regularly scanning the cache keys provides valuable insights into data access patterns and cache utilization. By analyzing which keys are frequently accessed or close to eviction, it is possible to fine-tune cache parameters, such as size or time-to-live (TTL)

Usage

Scan() returns an iterator that yields current non-expired items from the cache. It iterates over a snapshot of keys taken at the beginning of iteration, checking each key's existence and expiration before yielding its associated value.

for key, value := range lru.Scan() {
  fmt.Println("Items ~>", key, value)
}

Benchmarks

Average times to Scan and extract all the keys from the cache based on 5000 iterations:

goos: darwin
goarch: amd64
cpu: Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
Key Count min median average max
1000 13.05µs 51.32µs 62.30µs 455.15µs
100000 77.50µs 407.91µs 595.30µs 27145.62µs
1000000 526.71µs 2678.91µs 2902.60µs 18077.27µs
10000000 164.99µs 10713.59µs 11131.20µs 82050.29µs

@iuga iuga marked this pull request as ready for review September 5, 2024 20:14
@iuga iuga requested a review from a team as a code owner September 5, 2024 20:14
@iuga iuga added enhancement New feature or request help wanted Extra attention is needed labels Sep 5, 2024
Copy link

@coxley coxley left a comment

Choose a reason for hiding this comment

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

Nice! Looks reasonable to me.

There have definitely been a few times where I've wanted to scan the LRU.

a-brown-and-white-dog-is-sitting-on-a-couch-looking-at-the-camera

Comment on lines +598 to +599
require.Equal(t, []int{0, 1, 3, 5, 6}, keys)
require.Equal(t, []int{0, 16, 48, 80, 96}, values)
Copy link

Choose a reason for hiding this comment

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

Bit shift values on init, but normie numbers on assert? :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm full of complexities and contradictions

@coxley
Copy link

coxley commented Sep 5, 2024

Also whoa. TIL that Github Markdown supports admonitions???

@iuga iuga removed the help wanted Extra attention is needed label Sep 5, 2024
@coxley
Copy link

coxley commented Sep 6, 2024

@iuga : You can ignore G115 in golangci-lint. People are adding it to their default exclusion lists lately because it's a pretty big overstep in some cases: golangci/golangci-lint#4941

@iuga iuga enabled auto-merge (squash) September 6, 2024 14:21
@iuga iuga disabled auto-merge September 6, 2024 14:22
@iuga iuga merged commit 196160f into TriggerMail:master Sep 6, 2024
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants