Marshal FileStore values

This commit is contained in:
Joshua Peek 2008-08-13 20:57:26 -05:00
parent c7e09a8fb2
commit b8b30985d5
2 changed files with 29 additions and 2 deletions

@ -9,13 +9,13 @@ def initialize(cache_path)
def read(name, options = nil)
super
File.open(real_file_path(name), 'rb') { |f| f.read } rescue nil
File.open(real_file_path(name), 'rb') { |f| Marshal.load(f) } rescue nil
end
def write(name, value, options = nil)
super
ensure_cache_path(File.dirname(real_file_path(name)))
File.atomic_write(real_file_path(name), cache_path) { |f| f.write(value) }
File.atomic_write(real_file_path(name), cache_path) { |f| Marshal.dump(value, f) }
rescue => e
RAILS_DEFAULT_LOGGER.error "Couldn't create cache directory: #{name} (#{e.message})" if RAILS_DEFAULT_LOGGER
end

@ -70,3 +70,30 @@ def test_fetch_with_forced_cache_miss
end
end
end
class FileStoreTest < Test::Unit::TestCase
def setup
@cache = ActiveSupport::Cache.lookup_store(:file_store, Dir.pwd)
end
def test_should_read_and_write_strings
@cache.write('foo', 'bar')
assert_equal 'bar', @cache.read('foo')
ensure
File.delete("foo.cache")
end
def test_should_read_and_write_hash
@cache.write('foo', {:a => "b"})
assert_equal({:a => "b"}, @cache.read('foo'))
ensure
File.delete("foo.cache")
end
def test_should_read_and_write_nil
@cache.write('foo', nil)
assert_equal nil, @cache.read('foo')
ensure
File.delete("foo.cache")
end
end