#800 √ resolved
Ryan Bates

FileStore can only store strings

Reported by Ryan Bates | August 11th, 2008 @ 04:49 PM | in 2.x

It is currently only possible to store strings in FileStore. All other objects are just converted to a string (to_s) so it's not possible to read them back as an object. This behavior is inconsistent with MemCacheStore and MemoryStore.


cache = ActiveSupport::Cache.lookup_store(:file_store, 'tmp/cache')
cache.write('foo', {:a => 1})
cache.read('foo') # => "a1" (hash as string)

The attached patch solves this by using Marshal on read/write. Like MemCacheStore this is overridable with the :raw option. Raw is used in fragment caching so that behaves like it used to (since it only stores strings).

Comments and changes to this ticket

  • Joshua Peek

    Joshua Peek August 11th, 2008 @ 08:59 PM

    • → Tag changed from “activesupport bug patch” to “activesupport patch”
    • → State changed from “new” to “open”
    • → Assigned user changed from “” to “Joshua Peek”

    Skip the the :raw option and always marshal for consistent behavior. That raw option was only there to pass to memcache-client, we don't need it at the moment. Otherwise I'm down with the idea.

  • Ryan Bates

    Ryan Bates August 11th, 2008 @ 09:36 PM

    Here's a patch which doesn't have the :raw option.

    I originally thought it was necessary to support old fragment caches when one upgrades, but looks like "nil" will be returned on read if Marshal can't read the cache. Fragment caching is smart enough to rebuild the cache file if that happens (from looking at the code, but I haven't fully tested it).

  • Joshua Peek

    Joshua Peek August 13th, 2008 @ 01:53 AM

    Is there an easy to way to still read/write strings without marshaling? Just concerned about the speed impact.

  • Ryan Bates

    Ryan Bates August 13th, 2008 @ 05:49 PM

    It's easy enough to detect if the object is a string on write, but I don't know of a way to tell a normal string vs. a marshalled string on read. This is one reason we may want to keep the :raw option around? That way the developers can optimize their caching with :raw if they are just working with strings. And we can use that in the fragment cache to give a little speed boost since that is always strings.

  • Joshua Peek

    Joshua Peek August 14th, 2008 @ 02:59 AM

    • → State changed from “open” to “resolved”

    Marshal load can take a file object and parse it as it reads the file. It is much faster than reading the whole file into a buffer and than unmarshaling it. After that change, I ran some benchmarks and there is barely any difference between marshaling. Its on all the time.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Source available from github

Repository is at http://github.com/rails/rails

Check out the development master (Edge Rails):

git clone git://github.com/rails/rails.git

Creating or reviewing a patch

See the contributor guide.

Creating a feature request

Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too"..

Shared Ticket Bins

People watching this ticket