diff --git a/lib/sequel/plugins/static_cache_cache.rb b/lib/sequel/plugins/static_cache_cache.rb index 3183c7304..96b130ea1 100644 --- a/lib/sequel/plugins/static_cache_cache.rb +++ b/lib/sequel/plugins/static_cache_cache.rb @@ -26,8 +26,18 @@ def self.configure(model, file) module ClassMethods # Dump the in-memory cached rows to the cache file. def dump_static_cache_cache - static_cache_cache = {} - @static_cache_cache.sort do |a, b| + File.open(@static_cache_cache_file, 'wb'){|f| f.write(Marshal.dump(sort_static_cache_hash(@static_cache_cache)))} + nil + end + + Plugins.inherited_instance_variables(self, :@static_cache_cache_file=>nil, :@static_cache_cache=>nil) + + private + + # Sort the given static cache hash in a deterministic way, so that + # the same static cache values will result in the same marshal file. + def sort_static_cache_hash(cache) + cache = cache.sort do |a, b| a, = a b, = b if a.is_a?(Array) @@ -47,17 +57,10 @@ def dump_static_cache_cache else a <=> b end - end.each do |k, v| - static_cache_cache[k] = v end - File.open(@static_cache_cache_file, 'wb'){|f| f.write(Marshal.dump(static_cache_cache))} - nil + Hash[cache] end - Plugins.inherited_instance_variables(self, :@static_cache_cache_file=>nil, :@static_cache_cache=>nil) - - private - # Load the rows for the model from the cache if available. # If not available, load the rows from the database, and # then update the cache with the raw rows. diff --git a/spec/extensions/static_cache_cache_spec.rb b/spec/extensions/static_cache_cache_spec.rb index 7bbf83b1a..4d6945b63 100644 --- a/spec/extensions/static_cache_cache_spec.rb +++ b/spec/extensions/static_cache_cache_spec.rb @@ -88,5 +88,9 @@ def self.name; 'Bar' end c = Class.new(Sequel::Model) c.plugin :static_cache_cache, @file c.instance_variable_get(:@static_cache_cache).keys.must_equal ['Bar', 'Foo', ['Bar', :a], ['Bar', :c]] + + c.send(:sort_static_cache_hash, {"Foo"=>[], ["Bar", "baz"]=>[]}).keys.must_equal ["Foo", ["Bar", "baz"]] + c.send(:sort_static_cache_hash, {["Bar", "baz"]=>[], "Foo"=>[]}).keys.must_equal ["Foo", ["Bar", "baz"]] + c.send(:sort_static_cache_hash, {["Foo", "baz"]=>[], ["Bar", "bar"]=>[]}).keys.must_equal [["Bar", "bar"], ["Foo", "baz"]] end end