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

Make return value of Cache::Store#write consistent #49873

Merged
merged 1 commit into from
Nov 16, 2023

Conversation

Mangara
Copy link
Contributor

@Mangara Mangara commented Oct 31, 2023

Motivation / Background

I'm trying to use memcached to de-duplicate some work. When I start the work, I want to write a key with a 5s TTL and if the key already exists, someone else is already doing the work and started at most 5s ago, so I don't have to start it again. To avoid a race condition, I'd like to use the unless_exist: true option for this. If write(..., unless_exist: true) returns false, the key was already there and we don't need to do the work.

The only problem with this approach is that the write method also returns false if there is some error with memcached. But in that case I do want to do the work, to ensure it gets done at least once.

Detail

The return value of Cache::Store#write was not specified before, and varied between backends. This PR makes it consistent:

  • true indicates a successful write
  • nil indicates an error talking to the cache backend
  • false indicates a write that failed for another reason

This lets us distinguish the case where the key already exists from the case where there was a memcached (or Redis) error.

Additional information

I only changed the implementation of the Redis and memcached stores. From CI, it looks like all others already followed this pattern?

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Changes that are unrelated should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@byroot
Copy link
Member

byroot commented Nov 1, 2023

Does it make sense to add this to more / all cache stores?

We generally try to avoid backend specific capabilities yes. Ideally you should be able to easily swap your cache backend. (it's never perfect of course)

@Mangara Mangara force-pushed the mangara-memcache-fallback branch 2 times, most recently from 7c4187f to 402aee6 Compare November 2, 2023 12:48
@Mangara
Copy link
Contributor Author

Mangara commented Nov 2, 2023

I implemented it for all cache stores that use the FailureSafetyBehavior test suite, which is Redis and memcached.
Ready for another look.

@Mangara Mangara changed the title Add fallback option to MemCacheStore#write Add fallback option to CacheStore#write Nov 9, 2023
@casperisfine
Copy link
Contributor

Sorry for dragging my feets on this, I got quite busy lately, and also I can't really put my finger on it, but I'm not super fan of this change. Can't really explain why though 😞 .

Perhaps because that fallback option is specific to write, and I fear it will creep up.

What if we returned nil on failure and false when the command worked, but didn't lead to a write?

@Mangara
Copy link
Contributor Author

Mangara commented Nov 16, 2023

What if we returned nil on failure and false when the command worked, but didn't lead to a write?

That would work for me. I'll see what that looks like.

@Mangara Mangara changed the title Add fallback option to CacheStore#write Return nil on cache write errors Nov 16, 2023
@Mangara Mangara force-pushed the mangara-memcache-fallback branch 2 times, most recently from 2753161 to d38b2ca Compare November 16, 2023 13:20
@Mangara Mangara changed the title Return nil on cache write errors Make return value of Cache::Store#write consistent Nov 16, 2023
Copy link
Member

@byroot byroot left a comment

Choose a reason for hiding this comment

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

Some very minor thing, other than that LGTM.

The return value was not specified before. Now it returns `true` on a
successful write, `nil` if there was an error talking to the cache
backend, and `false` if the write failed for another reason (e.g. the
key already exists and `unless_exist: true` was passed).
@byroot byroot merged commit 949b4d1 into rails:main Nov 16, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants