Class: Spree::Preferences::StoreInstance
- Inherits:
-
Object
- Object
- Spree::Preferences::StoreInstance
- Defined in:
- app/models/spree/preferences/store.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#persistence ⇒ Object
Returns the value of attribute persistence.
Instance Method Summary collapse
- #clear_cache ⇒ Object
- #delete(key) ⇒ Object
- #exist?(key) ⇒ Boolean
- #get(key, fallback = nil) ⇒ Object
-
#initialize ⇒ StoreInstance
constructor
A new instance of StoreInstance.
- #set(key, value, type) ⇒ Object
Constructor Details
#initialize ⇒ StoreInstance
Returns a new instance of StoreInstance.
14 15 16 17 |
# File 'app/models/spree/preferences/store.rb', line 14 def initialize @cache = Rails.cache @persistence = true end |
Instance Attribute Details
#persistence ⇒ Object
Returns the value of attribute persistence.
12 13 14 |
# File 'app/models/spree/preferences/store.rb', line 12 def persistence @persistence end |
Instance Method Details
#clear_cache ⇒ Object
66 67 68 |
# File 'app/models/spree/preferences/store.rb', line 66 def clear_cache @cache.clear end |
#delete(key) ⇒ Object
61 62 63 64 |
# File 'app/models/spree/preferences/store.rb', line 61 def delete(key) @cache.delete(key) destroy(key) end |
#exist?(key) ⇒ Boolean
24 25 26 27 |
# File 'app/models/spree/preferences/store.rb', line 24 def exist?(key) @cache.exist?(key) || should_persist? && Spree::Preference.where(:key => key).exists? end |
#get(key, fallback = nil) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/models/spree/preferences/store.rb', line 29 def get(key,fallback=nil) # 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 Spree::Preference.table_exists? && preference = Spree::Preference.find_by_key(key) # 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 end end unless fallback.nil? # cache fallback so we won't hit the db above on # subsequent queries for the same key # @cache.write(key, fallback) end return fallback end |
#set(key, value, type) ⇒ Object
19 20 21 22 |
# File 'app/models/spree/preferences/store.rb', line 19 def set(key, value, type) @cache.write(key, value) persist(key, value, type) end |