diff --git a/core/app/models/spree/preferences/store.rb b/core/app/models/spree/preferences/store.rb index 80dcdeb72d9..cee07d59239 100644 --- a/core/app/models/spree/preferences/store.rb +++ b/core/app/models/spree/preferences/store.rb @@ -23,11 +23,35 @@ def set(key, value, type) end def exist?(key) - @cache.exist? key + @cache.exist?(key) || Spree::Preference.where(:key => key).exists? end def get(key) - @cache.read(key) + # look first in our cache + val = @cache.read(key) + + # return the retrieved value, if it's in the cache + return val if val.present? + + return nil unless should_persist? + + # if it's not in the cache, maybe it's in the database, but + # has been cleared from the cache + + # does it exist in the database? + preference = Spree::Preference.find_by_key(key) + + if preference.present? + + # it does exist, so let's put it back into the cache + @cache.write(preference.key, preference.value) + + # and return the value + return preference.value + else + # it never existed and our initial cache miss was correct + return nil + end end def delete(key) diff --git a/core/spec/models/preferences/store_spec.rb b/core/spec/models/preferences/store_spec.rb index f63cf748c7c..2d5a091934e 100644 --- a/core/spec/models/preferences/store_spec.rb +++ b/core/spec/models/preferences/store_spec.rb @@ -15,4 +15,10 @@ @store.set :test, false, :boolean @store.get(:test).should be_false end -end \ No newline at end of file + + it "returns the correct preference value when the cache is empty" do + @store.set :test, "1", :string + Rails.cache.clear + @store.get(:test).should == "1" + end +end