Topic: [Problem] [Rails.cache] [:file_store]

Hello all

I am using developement environment with file_store.

I have the following:

Product Model:

def self.all_cached
Rails.cache.fetch('product.all') {all}
end

This works fine, it writes on cache files.

But, if I call Product.all_cached, it returns a string. Why this does not return objects?

Re: [Problem] [Rails.cache] [:file_store]

It's an inconsistency between the various cache stores: the FileStore doesn't marshall objects when reading/writing the cache (unlike, for example, the MemCacheStore which does).

You can do the marshalling yourself but you won't be able to use the fetch method, instead something like this:

def self.all_cached
  if (value = Rails.cache.fetch('product.all'))
    Marshal.load(value)
  else
    value = all
    Rails.cache.write('product.all', Marshal.dump(value))
    value
  end
end

Although I don't know how well that'd play with other stores, and really this should be in the FileStore itself, so you should raise a bug ticket on the Rails tracker.

Rob Anderton
TheWebFellas