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

Class Method Summary collapse

Class Attribute Details

.log_cache_activityObject

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

.cacheObject



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, cache_options, &block)
end

.cache_optionsObject



93
94
95
96
# File 'lib/forget-me-not/cacheable.rb', line 93

def self.cache_options
  @cache_options ||= {expires_in: 12 * 60 * 60}
  @cache_options.merge(Cacheable.cache_options_threaded)
end

.cache_options_threadedObject



98
99
100
# File 'lib/forget-me-not/cacheable.rb', line 98

def self.cache_options_threaded
  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.cache_options_threaded=(options)
  Thread.current['cacheable-cache-options'] = options
end

.cachersObject



106
107
108
# File 'lib/forget-me-not/cacheable.rb', line 106

def self.cachers
  @cachers ||= Set.new
end

.cachers_and_descendantsObject



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_loggerObject



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

.loggerObject



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_loggerObject



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.cache_options_threaded = {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.cache_options_threaded = nil
  end
end