Module: QuickTravel::Cache
- Defined in:
- lib/quick_travel/cache.rb
Class Method Summary collapse
- .cache(key, cache_options = nil) ⇒ Object
- .cache_empty?(cached_value) ⇒ Boolean
- .cache_store ⇒ Object
- .cache_store=(session) ⇒ Object
- .clear ⇒ Object
- .delete(key, namespace = true) ⇒ Object
- .namespace ⇒ Object
- .namespace=(namespace) ⇒ Object
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, = nil) return yield unless key.present? ||= {} key = "#{@@namespace}_#{key}" unless [:disable_namespacing] cached_value = cache_store.read(key) return cached_value unless cache_empty?(cached_value) return nil unless block_given? ||= {} [:expires_in] = 1.day unless .key?(:expires_in) yield.tap { |value| cache_store.write(key, value, ) } end |
.cache_empty?(cached_value) ⇒ 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_store ⇒ Object
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 |
.clear ⇒ Object
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 |
.namespace ⇒ Object
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, = {}, &block) QuickTravel::Cache.cache(key, ) do block.call end end |