This project is archived and is in readonly mode.

#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

  • josh

    josh August 11th, 2008 @ 08:59 PM

    • State changed from “new” to “open”
    • Assigned user set to “josh”
    • Tag changed from activesupport, bug, patch to activesupport, patch

    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).

  • josh

    josh 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.

  • josh

    josh 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.

Create your profile

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

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Referenced by

Pages