Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

FileStore can only store strings

#800

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

Reported by Ryan Bates · August 11th, 2008 @ 04:49 PM

State: resolved
Milestone: 2.x
Assigned to: josh josh
Importance: none

Activity

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

    August 11th, 2008 @ 08:59 PM

  2. Ryan Bates
    Ryan Bates

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

    August 11th, 2008 @ 09:36 PM

  3. josh
    josh

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

    August 13th, 2008 @ 01:53 AM

  4. Ryan Bates
    Ryan Bates

    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.

    August 13th, 2008 @ 05:49 PM

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

    August 14th, 2008 @ 02:59 AM