Sha256: 89879ab6d2c3ce38ed1e2709f7d4187ad26e99616c4c7ecca40f088db88f3a3d

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 KB

Contents

module QuickTravel
  module Cache
    def cache(key, cache_options = {}, &block)
      QuickTravel::Cache.cache(key, cache_options) do
        block.call
      end
    end

    def self.cache(key, cache_options = nil)
      return yield unless key.present?
      cache_options ||= {}
      key = "#{@@namespace}_#{key}" unless cache_options[:disable_namespacing]
      cached_value = cache_store.read(key)
      return cached_value unless cached_value.nil?
      return nil unless block_given?
      cache_options ||= {}
      cache_options[:expires_in] = 1.day unless cache_options.key?(:expires_in)
      yield.tap { |value| cache_store.write(key, value, cache_options) }
    end

    def self.delete(key, namespace = true)
      key = "#{@@namespace}_#{key}" if namespace
      cache_store.delete(key)
    end

    def self.clear
      cache_store.clear
    end

    def self.cache_store
      @@cache_store
    end

    def self.cache_store=(session)
      @@cache_store = session
    end

    def self.namespace
      @@namespace
    end

    def self.namespace=(namespace)
      @@namespace = namespace
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
quicktravel_client-3.9.0 lib/quick_travel/cache.rb
quicktravel_client-3.8.1 lib/quick_travel/cache.rb
quicktravel_client-3.8.0 lib/quick_travel/cache.rb