class Spree::Preferences::StoreInstance
Public Class Methods
new()
click to toggle source
# File lib/spree/preferences/store.rb, line 10 def initialize @cache = Rails.cache end
Public Instance Methods
clear_cache()
click to toggle source
# File lib/spree/preferences/store.rb, line 62 def clear_cache @cache.clear end
delete(key)
click to toggle source
# File lib/spree/preferences/store.rb, line 57 def delete(key) @cache.delete(key) destroy(key) end
exist?(key)
click to toggle source
# File lib/spree/preferences/store.rb, line 20 def exist?(key) @cache.exist?(key) || should_persist? && Spree::Preference.where(key: key).exists? end
get(key) { || ... }
click to toggle source
# File lib/spree/preferences/store.rb, line 25 def get(key) # return the retrieved value, if it's in the cache # use unless nil? incase the value is actually boolean false # unless (val = @cache.read(key)).nil? return val end if 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? if preference = Spree::Preference.find_by(key: key) # it does exist val = preference.value else # use the fallback value val = yield end # Cache either the value from the db or the fallback value. # This avoids hitting the db with subsequent queries. @cache.write(key, val) return val else yield end end
Also aliased as: fetch
set(key, value)
click to toggle source
# File lib/spree/preferences/store.rb, line 14 def set(key, value) @cache.write(key, value) persist(key, value) end
Also aliased as: []=
Private Instance Methods
destroy(cache_key)
click to toggle source
# File lib/spree/preferences/store.rb, line 76 def destroy(cache_key) return unless should_persist? preference = Spree::Preference.find_by(key: cache_key) preference.destroy if preference end
persist(cache_key, value)
click to toggle source
# File lib/spree/preferences/store.rb, line 68 def persist(cache_key, value) return unless should_persist? preference = Spree::Preference.where(key: cache_key).first_or_initialize preference.value = value preference.save end
should_persist?()
click to toggle source
# File lib/spree/preferences/store.rb, line 83 def should_persist? Spree::Preference.table_exists? end