Module: QuickTravel::Cache

Defined in:
lib/quick_travel/cache.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cache(key, cache_options = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/quick_travel/cache.rb', line 9

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 cache_empty?(cached_value)
  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

.cache_empty?(cached_value) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
# File 'lib/quick_travel/cache.rb', line 21

def self.cache_empty?(cached_value)
  if cached_value.respond_to?(:body)
    return cached_value.body.nil? || cached_value.body.empty?
  end
  cached_value.nil?
end

.cache_storeObject



37
38
39
# File 'lib/quick_travel/cache.rb', line 37

def self.cache_store
  @@cache_store
end

.cache_store=(session) ⇒ Object



41
42
43
# File 'lib/quick_travel/cache.rb', line 41

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

.clearObject



33
34
35
# File 'lib/quick_travel/cache.rb', line 33

def self.clear
  cache_store.clear
end

.delete(key, namespace = true) ⇒ Object



28
29
30
31
# File 'lib/quick_travel/cache.rb', line 28

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

.namespaceObject



45
46
47
# File 'lib/quick_travel/cache.rb', line 45

def self.namespace
  @@namespace
end

.namespace=(namespace) ⇒ Object



49
50
51
# File 'lib/quick_travel/cache.rb', line 49

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

Instance Method Details

#cache(key, cache_options = {}, &block) ⇒ Object



3
4
5
6
7
# File 'lib/quick_travel/cache.rb', line 3

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