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

cache: Add .Set and .Add methods to cache clients #591

Merged
merged 5 commits into from
Oct 7, 2024

Conversation

56quarters
Copy link
Contributor

@56quarters 56quarters commented Oct 3, 2024

What this PR does:

This change adds a synchronous version of .Set to Memcached and Redis clients as well as the various Cache wrapper implementations. This allows callers to set a key and be sure it exists in the cache. This change also adds an .Add method which conditionally adds an item to the cache only if it does not already exist.

Which issue(s) this PR fixes:

This change is a prerequisite for grafana/mimir#9386

Checklist

  • Tests updated
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

@56quarters 56quarters force-pushed the 56quarters/more-cache-methods branch from 8b287e3 to 2f51483 Compare October 3, 2024 17:33
This change adds a synchronous version of `.Set` to Memcached and Redis
clients as well as the various `Cache` wrapper implementations. This
allows callers to set a key and be sure it exists in the cache. This
change also adds an `.Add` method which conditionally adds an item to
the cache only if it does not already exist.

This change is a prerequisite for grafana/mimir#9386

Signed-off-by: Nick Pillitteri <[email protected]>
@56quarters 56quarters force-pushed the 56quarters/more-cache-methods branch from 2f51483 to ebcbf7b Compare October 3, 2024 17:43
56quarters added a commit to grafana/mimir that referenced this pull request Oct 3, 2024
Proof-of-concept for discussion as part of:

#9434

grafana/dskit#591

Signed-off-by: Nick Pillitteri <[email protected]>
56quarters added a commit to grafana/mimir that referenced this pull request Oct 3, 2024
Proof-of-concept for discussion as part of:

#9434

grafana/dskit#591

Signed-off-by: Nick Pillitteri <[email protected]>
@56quarters 56quarters marked this pull request as ready for review October 4, 2024 15:05
Signed-off-by: Nick Pillitteri <[email protected]>
@56quarters 56quarters requested a review from a team October 4, 2024 15:22
Copy link
Contributor

@dimitarvdimitrov dimitarvdimitrov left a comment

Choose a reason for hiding this comment

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

nice one, I'm not sure about the 30d TTL, otherwise LGTM

cache/client.go Show resolved Hide resolved
Comment on lines +124 to +127
// When a caller uses the Add method, the presence of absence of an entry in the cache
// has significance. In order to maintain the semantics of that, we only add an entry to
// the LRU when it was able to be successfully added to the shared cache.
if err == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

i didn't think of this. Can you add a tiny test for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, will do 👍

cache/memcached_client.go Show resolved Hide resolved
cache/memcached_client.go Outdated Show resolved Hide resolved
@56quarters
Copy link
Contributor Author

nice one, I'm not sure about the 30d TTL, otherwise LGTM

When you give Memcached a TTL longer than 30d, it treats the number of seconds as a Unix timestamp. E.g. if you give it a 31d TTL your item will expire on February 1st 1970 -- immediately.

Signed-off-by: Nick Pillitteri <[email protected]>
@56quarters 56quarters merged commit de20fd2 into main Oct 7, 2024
5 checks passed
@56quarters 56quarters deleted the 56quarters/more-cache-methods branch October 7, 2024 16:37
56quarters added a commit to grafana/mimir that referenced this pull request Oct 7, 2024
Specifically, this pulls in grafana/dskit#591
which adds new `.Set()` and `.Add()` methods to cache interfaces and
clients.

Signed-off-by: Nick Pillitteri <[email protected]>
56quarters added a commit to grafana/mimir that referenced this pull request Oct 7, 2024
Specifically, this pulls in grafana/dskit#591
which adds new `.Set()` and `.Add()` methods to cache interfaces and
clients.

Signed-off-by: Nick Pillitteri <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants