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 retrieval of cached entry scope to be public. Fixes #45311 #45313
base: main
Are you sure you want to change the base?
Conversation
There was also a read multi that I will move public as well to allow both reads to be publicly accessible. |
I am sure this is not the way to go by making internal methods public. Have you tried |
@fatkodima that doesn't work if the yield block raises an error. There is no opportunity to return the value. I simply modified my provided example and you can see that when the block yields the error there is no opportunity to retrieve and/or use the previous value. |
@poloka which cache store are you using? My questions are based on how the memcached store works, where if an entry is expired in the cache it's also expired in memcached (so it won't even get returned). Which is why I am confused by the idea that I'm guessing you're using the file store or memory store, because it seems like that situation is possible and sensible for you. Using memcached store: > key = 'foo'
"foo"
> args = { expires_in: 10 }
{
:expires_in => 10
}
> Rails.cache.fetch(key, args) { 'bing' }
# Dalli::Server#connect localhost:11211
"bing"
> sleep(10)
10
> entry = Rails.cache.send(:read_entry, key, args)
nil # whereas I think you expect this to return an entry |
@ghiculescu my example is using the memcache. You are correct that if you call |
But in my example just above I don't call
Basically I have done this but you can't retain the value by doing this. |
@ghiculescu, I don't believe the use case in your console is the same and I did not experience this behavior within the rails application. I initially thought I was a MemCacheStore but digging into what logic I drop into it looks as if I'm an I took your example and I did the same code within the key = 'foo'
args = { expires_in: 10 }
Rails.cache.fetch(key, args) { puts 'retrieving value'; 'bing' }
Rails.cache.fetch(key, args) { puts 'retrieving value'; 'bing' }
entry = Rails.cache.send(:read_entry, key, args)
puts entry
Rails.application.config.cache_store It yielded the following: 2.7.2 :001 > key = 'foo'
=> "foo"
2.7.2 :002 > args = { expires_in: 10 }
=> {:expires_in=>10}
2.7.2 :003 > Rails.cache.fetch(key, args) { puts 'retrieving value'; 'bing' }
retrieving value
=> "bing"
2.7.2 :004 > Rails.cache.fetch(key, args) { puts 'retrieving value'; 'bing' }
retrieving value
=> "bing"
2.7.2 :005 > entry = Rails.cache.send(:read_entry, key, args)
=> nil
2.7.2 :006 > puts entry
=> nil
2.7.2 :007 > Rails.application.config.cache_store
=> :null_store As you can see, the following |
Yeah, that's to be expected in the Null store, as it doesn't do any actual caching. So it would always need to retrieve a value. But you probably shouldn't use the Null store in production :) I would suggest |
Thanks @ghiculescu, yeah I wouldn't use |
And if I run the example with :001 >
:002 > key = 'foo'
=> "foo"
:003 > args = { expires_in: 10 }
=> {:expires_in=>10}
:004 > Rails.cache.fetch(key, args) { puts 'retrieving value'; 'bing' }
retrieving value
=> "bing"
:005 > Rails.cache.fetch(key, args) { puts 'retrieving value'; 'bing' }
=> "bing"
:006 > entry = Rails.cache.send(:read_entry, key, args)
=> #<ActiveSupport::Cache::Entry:0x00007f8afc844818 @value="bing", @version=nil, @created_at=1654880053.508685, @expires_in=10.0>
:007 > puts entry
#<ActiveSupport::Cache::Entry:0x00007f8afc844818>
=> nil
:008 > Rails.application.config.cache_store
=> :memory_store
:009 > |
The memory store isn't the same as the memcached store FYI. I wouldn't recommend it for production either; https://api.rubyonrails.org/classes/ActiveSupport/Cache/MemoryStore.html talks about why. But yeah what you're seeing makes sense for the memory store as it doesn't have a separate backend, similar to null store. |
I'm going to close this for now as the conversation's split between here and #45311 and that can get confusing. We can re-open if it seems like there is a bug here or something that requires a PR. |
@ghiculescu I do not agree that this PR should be closed. Please reopen this PR as the functionality being requested is still valid. |
The conversations that have been going on the PR should have been on the issue in the first place but I continued the conversation wherever it was occurring. |
Thanks @ghiculescu for reopening! |
Summary
Making the retrieval of
ActiveSupport::Cache::Entry
objects public to assist in consumers retrieving a value prior to its deletion if they so wish to perform a retry operation at some future period of time.Fixes #45311
Other Information
n/a