dimanche 5 juillet 2015

Array Redid-Store cache created in Rails 3 could not read from Rails 4

Recently, I upgrade my app from Rails 3(3.2.17) to Rails 4(4.0.13). But I ran into a problem when reading from redis-store cache.

I have cache in Rails 3 created like this.

class Blog
  def self.hidden_ids
    hidden_ids = Rails.cache.fetch(['hidden_ids blog'],expires_in: 1.week) do
      hidden_ids = hidden.pluck(:id)
      hidden_ids << 0 unless hidden_ids.include?(0)
    end
  hidden_ids
  end
end

And [1,2,3,4,5,6] Array cache was stored in Redis like

"setex" "hidden_ids blog" "604800"

"\x04\bo: ActiveSupport::Cache::Entry\t:\x10@compressedF:\x10@expires_inf\x0c6.048e5:\x10@created_atf\x161436151348.106037:\x0b@value\"6\x04\b[\x11i\x01\x88i\x02-\x01i\x02\x7f\x01i\x02\xa6\x03i\x02\xb8\x05i\x02\xb6\x06i\x02\xe1\x06i\x02\xe8\x06i\x02\xea\x06i\x02\xfa\x06i\x02\xfb\x06i\x00"

When I called Blog.hidden_ids in Rails 4,I Got:

"\x04\b[\x11i\x01\x88i\x02-\x01i\x02\x7F\x01i\x02\xA6\x03i\x02\xB8\x05i\x02\xB6\x06i\x02\xE1\x06i\x02\xE8\x06i\x02\xEA\x06i\x02\xFA\x06i\x02\xFB\x06i\x00"

not an Array

when I dig into the source code,I found the reason.

In Rails 3.2.17,ActiveSupport::Cache::Entry,line 562

def initialize(value,options={})
   ...
   ...
   @value = Marshal.dump(value)
end

But in Rails 4.0.0, ActiveSupport::Cache::Entry, line 581

def initialize(value,options={})
  ...
  else
    @value = value
  end
  ...
end    

If I call this in Rails 4

Marshal.restore(Blog.hidden_ids), I will got [1,2,3,....11,12]

So My Question is Is that any way to fix so I can still use the cache created by rails 3 or I should just drop the cache created in Rails 3?

Aucun commentaire:

Enregistrer un commentaire