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

fix cache bug #436

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

fix cache bug #436

wants to merge 3 commits into from

Conversation

BenZhang
Copy link

@BenZhang BenZhang commented May 13, 2018

Hi,
I ran into a very strange situation.

json.array! [1,2,3] do |i|
  json.cache! 'test' do
    json.name 'cache'
  end
  json.id i
end

The code above will print

[
  {"name":"cache","id":1},
  {"name":"cache","id":3},
  {"name":"cache","id":3}
]

If we move json.id on the top, it will work. The reason for that is when use cache! in the beginning of the block it will retrieve the same cached object from previous iteration and overwrite it. I think it is the same reason for this issue #330

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @pixeltrix (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@BenZhang
Copy link
Author

After further investigation, this will only happen when using memory_store for the cache. It is actually caused by the behaviour of Rails cache. Please see below example

Rails.cache.write('user', {id: 1})
a = Rails.cache.read('user')
#=> {:id=>1}
a[:name] = "Hello"
Rails.cache.read('user')
#=> {:id=>1, :name => "Hello"}

@ryanb
Copy link

ryanb commented Apr 1, 2019

I ran into this today. It would be great to get it fixed! Can someone address this PR?

@EvTi
Copy link

EvTi commented Jul 16, 2019

One more bug, i think

json.cache_collection! [1,2,3] do |category|
  next
end

result

{}
{}
{}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants