-
Notifications
You must be signed in to change notification settings - Fork 9
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
Support increment/decrement #29
Comments
I'm not a maintainer of this gem. but increment/decrement seems like standard interface for rails cache storage. It would be great to have it, just in case. |
PR would be nice :) |
Here is the solution I've been using. Not efficient as I'd like, a read and a write, which could be done with an upsert with conflict adding old and new value.
|
That's prone to races, I'm afraid. All DBs support atomic increments. |
AR comes with #increment_counter and #decrement_counter functions, the only problem with those is that they only offer a way to increment and decrement by the value of one. #update_counters seems like a generic implementation for updating counters, that could be used here. |
Yeah it's not great. But anything more needed a bit more plumbing. Ideally we need add an upsert-like atomic increment which uses a raw (non-Marshaled) value and inserts the delta or adds the the delta to existing value on conflict. ActiveRecord has Unless there are other ideas. |
The existing AR counter methods assume a record exists, which we cannot. |
We could use |
We can't use on_duplicate if you want to support < AR v7 though.
We don't need/want to use marshal dump. There is a convention in AS Cache to support a raw value which is not dumped so that atomic operations can be performed on it at the cache server. Otherwise you need to return existing record to ruby land and unmarshal to perform a simple increment. Seems wasteful and slow.
SQL looks like the best option short of backporting the on_duplicate option. Though I'm not sure of the consistency of the upsert SQL statements across database types.
…On 28 Aug 2023 at 10:26 PM +1000, Dimitrij Denissenko ***@***.***>, wrote:
We could use on_duplicate but I assume incrementing a Marshal.dump value with pure SQL could be difficult. Transactions maybe?
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.Message ID: ***@***.***>
|
Here's a quick hack for what I'm thinking
|
The expiry is not honoured so that would need fixing. |
It's not yet released, but support for Rails 6 has been removed. |
Oh right. Well I need this for Rails 6.0 and 6.1 projects which are not going to be upgraded for a little while. I'll go my own way I guess. |
Ooops, my bad, I assumed Rails 6.1 is now reached EOL too, but it's only 6.0. Will bring it back in a separate PR - https://endoflife.date/rails |
Ref: #37 |
Any reason not to support these cache methods? I gather you don't use them so you left them out.
Wondering if you want a PR for it?
The text was updated successfully, but these errors were encountered: