-
Notifications
You must be signed in to change notification settings - Fork 21.4k
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
Conversation
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) |
7c4187f
to
402aee6
Compare
I implemented it for all cache stores that use the |
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 |
That would work for me. I'll see what that looks like. |
05a78a6
to
05fd4be
Compare
2753161
to
d38b2ca
Compare
Cache::Store#write
consistent
d38b2ca
to
ef4bad0
Compare
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.
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).
ef4bad0
to
4cfdcfe
Compare
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. Ifwrite(..., unless_exist: true)
returnsfalse
, 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 returnsfalse
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 writenil
indicates an error talking to the cache backendfalse
indicates a write that failed for another reasonThis 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:
[Fix #issue-number]