Module: ForgetMeNot::Cacheable
- Defined in:
- lib/forget-me-not/cacheable.rb
Overview
Allows the cross-system caching of lengthy function calls
Defined Under Namespace
Modules: ClassMethods
Class Attribute Summary collapse
-
.log_cache_activity ⇒ Object
Returns the value of attribute log_cache_activity.
Class Method Summary collapse
- .cache ⇒ Object
- .cache=(value) ⇒ Object
- .cache_fetch(key, &block) ⇒ Object
- .cache_options ⇒ Object
- .cache_options_threaded ⇒ Object
- .cache_options_threaded=(options) ⇒ Object
- .cachers ⇒ Object
- .cachers_and_descendants ⇒ Object
- .default_logger ⇒ Object
- .included(base) ⇒ Object
- .logger ⇒ Object
- .logger=(logger) ⇒ Object
- .rails_logger ⇒ Object
- .warm(*args) ⇒ Object
Class Attribute Details
.log_cache_activity ⇒ Object
Returns the value of attribute log_cache_activity.
136 137 138 |
# File 'lib/forget-me-not/cacheable.rb', line 136 def log_cache_activity @log_cache_activity end |
Class Method Details
.cache ⇒ Object
85 86 87 |
# File 'lib/forget-me-not/cacheable.rb', line 85 def self.cache @cache ||= default_cache end |
.cache=(value) ⇒ Object
89 90 91 |
# File 'lib/forget-me-not/cacheable.rb', line 89 def self.cache=(value) @cache = value end |
.cache_fetch(key, &block) ⇒ Object
81 82 83 |
# File 'lib/forget-me-not/cacheable.rb', line 81 def self.cache_fetch(key, &block) cache.fetch(key, , &block) end |
.cache_options ⇒ Object
93 94 95 96 |
# File 'lib/forget-me-not/cacheable.rb', line 93 def self. @cache_options ||= {expires_in: 12 * 60 * 60} @cache_options.merge(Cacheable.) end |
.cache_options_threaded ⇒ Object
98 99 100 |
# File 'lib/forget-me-not/cacheable.rb', line 98 def self. Thread.current['cacheable-cache-options'] || {} end |
.cache_options_threaded=(options) ⇒ Object
102 103 104 |
# File 'lib/forget-me-not/cacheable.rb', line 102 def self.() Thread.current['cacheable-cache-options'] = end |
.cachers ⇒ Object
106 107 108 |
# File 'lib/forget-me-not/cacheable.rb', line 106 def self.cachers @cachers ||= Set.new end |
.cachers_and_descendants ⇒ Object
110 111 112 113 114 115 116 117 |
# File 'lib/forget-me-not/cacheable.rb', line 110 def self.cachers_and_descendants all_cachers = Set.new Cacheable.cachers.each do |c| all_cachers << c all_cachers += c.descendants if c.is_a? Class end all_cachers end |
.default_logger ⇒ Object
151 152 153 154 155 |
# File 'lib/forget-me-not/cacheable.rb', line 151 def default_logger logger = Logger.new(STDOUT) logger.level = Logger::INFO logger end |
.included(base) ⇒ Object
7 8 9 10 |
# File 'lib/forget-me-not/cacheable.rb', line 7 def included(base) base.extend(ClassMethods) Cacheable.cachers << base end |
.logger ⇒ Object
138 139 140 141 |
# File 'lib/forget-me-not/cacheable.rb', line 138 def logger return @logger if defined?(@logger) @logger = rails_logger || default_logger end |
.logger=(logger) ⇒ Object
143 144 145 |
# File 'lib/forget-me-not/cacheable.rb', line 143 def logger=(logger) @logger = logger end |
.rails_logger ⇒ Object
147 148 149 |
# File 'lib/forget-me-not/cacheable.rb', line 147 def rails_logger defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger end |
.warm(*args) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/forget-me-not/cacheable.rb', line 119 def self.warm(*args) begin Cacheable. = {force: true} Cacheable.cachers_and_descendants.each do |cacher| begin cacher.cache_warm(*args) rescue StandardError => e logger.error "Exception encountered when warming #{cacher.name}: #{e.inspect}. \n\t#{e.backtrace.join("\n\t")}" end end ensure Cacheable. = nil end end |