Sha256: 487b7ef60925b3c0e7d6e6c9912f3b71b99d8e7f7003fa3e4dc7bbc668db3958

Contents?: true

Size: 1.9 KB

Versions: 26

Compression:

Stored size: 1.9 KB

Contents

# Use singleton class Spree::Preferences::Store.instance to access
#

require 'singleton'

module Spree::Preferences
  class StoreInstance
    def initialize
      @cache = Rails.cache
    end

    def set(key, value)
      @cache.write(key, value)
      persist(key, value)
    end
    alias_method :[]=, :set

    def exist?(key)
      @cache.exist?(key) ||
        should_persist? && Spree::Preference.where(key: key).exists?
    end

    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
    alias_method :fetch, :get

    def delete(key)
      @cache.delete(key)
      destroy(key)
    end

    def clear_cache
      @cache.clear
    end

    private

    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

    def destroy(cache_key)
      return unless should_persist?

      preference = Spree::Preference.find_by_key(cache_key)
      preference.destroy if preference
    end

    def should_persist?
      Spree::Preference.table_exists?
    end
  end

  class Store < StoreInstance
    include Singleton
  end
end

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
solidus_core-2.2.2 app/models/spree/preferences/store.rb
solidus_core-2.1.1 app/models/spree/preferences/store.rb
solidus_core-2.0.3 app/models/spree/preferences/store.rb
solidus_core-1.4.2 app/models/spree/preferences/store.rb
solidus_core-1.3.2 app/models/spree/preferences/store.rb
solidus_core-2.0.2 app/models/spree/preferences/store.rb
solidus_core-1.4.1 app/models/spree/preferences/store.rb
solidus_core-2.2.1 app/models/spree/preferences/store.rb
solidus_core-2.2.0 app/models/spree/preferences/store.rb
solidus_core-2.2.0.rc1 app/models/spree/preferences/store.rb
solidus_core-2.2.0.beta1 app/models/spree/preferences/store.rb
solidus_core-2.1.0 app/models/spree/preferences/store.rb
solidus_core-2.1.0.rc1 app/models/spree/preferences/store.rb
solidus_core-2.0.1 app/models/spree/preferences/store.rb
solidus_core-2.1.0.beta1 app/models/spree/preferences/store.rb
solidus_core-2.0.0 app/models/spree/preferences/store.rb
solidus_core-1.4.0 app/models/spree/preferences/store.rb
solidus_core-2.0.0.rc1 app/models/spree/preferences/store.rb
solidus_core-1.4.0.rc1 app/models/spree/preferences/store.rb
solidus_core-2.0.0.beta1 app/models/spree/preferences/store.rb