

Rails preserves entry TTL when incrementing or decrementing an integer value.
source link: https://blog.saeloun.com/2023/02/07/rails-preserve-ttl-while-incrementing
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Rails preserves entry TTL when incrementing or decrementing an integer value.
Feb 7, 2023 , by Murtaza Bagwala
1 minute read
Caching solutions like MemCache
and Redis always preserve expiration,
while altering a value to avoid any discrepancies caused by it.
Previously, while incrementing
or decrementing an integer value in MemoryStore,
Rails used to reset the expires_in
associated with the entry, for eg-
Before
Let’s say, we are storing certain number of likes on a product
as a like_counter
in MemoryStore for 10 seconds.
Here, if we increment
or decrement the counter,
it is used to reset the TTL.
irb(main):004:0> cache = ActiveSupport::Cache.lookup_store(:memory_store)
irb(main):004:0> cache.write("like_counter", 1, raw: true, expires_in: 10.seconds)
irb(main):004:0> sleep(2.seconds)
irb(main):004:0> cache.increment("like_counter")
irb(main):004:0> sleep(8.seconds)
irb(main):004:0> cache.read("like_counter")
irb(main):004:0> 2
As we can see after 10 seconds, like_counter
should have been deleted from the store, however,
it still shows the incremented value because cache.increment("like_counter")
set the timer
again to 10 seconds.
After
Now, Rails preserves expires_in while incrementing or decrementing an integer value in MemoryStore.
irb(main):004:0> cache = ActiveSupport::Cache.lookup_store(:memory_store)
irb(main):004:0> cache.write("like_counter", 1, raw: true, expires_in: 10.seconds)
irb(main):004:0> sleep(2.seconds)
irb(main):004:0> cache.increment("like_counter")
irb(main):004:0> sleep(8.seconds)
irb(main):004:0> cache.read("like_counter")
irb(main):004:0> nil
As it can be seen,
though we had incremented the counter,
the key got expired after 10 seconds
and returned nil
Check out the PR for more details.
Recommend
-
76
README.md vimade An eye catching plugin that fades your inactive buffers and preserves syntax highlighting!!!! Screenshots Fade inacti...
-
34
Incrementing Values Atomically with Cloud Firestore 2019-03-28ad...
-
18
PowerShell Regex Example - Incrementing Matches In the PowerShell Slack, I recently answered a question along these lines. Say you have a string that reads “first thing {} second thing {}” and you want to get to “first t...
-
11
How Bitcoin Better Preserves its Value Compared to Other CryptocurrenciesJune 9th 2021 new story4
-
10
-
17
Auto-incrementing Build Numbers in CapacitorAppflow provides an auto-incrementing environment variable (CI_BUILD_NUMBER) that can be used to automatically increment build numbers for your Capacitor app on iOS and Android...
-
8
Nations...
-
7
Maximize the smallest array element by incrementing all elements in a K-length subarray by 1 exactly M timesDifficulty Level : Medium...
-
3
Collaborative machine learning that preserves privacy Researchers increase the accuracy and efficiency of a machine-learning method that safeguards user data. Adam Zewe | MIT News Offic...
-
4
Library of Obsolete Sounds preserves recordings of old tech Recordings include old cameras, typewriters, computers, and more By
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK