module ForgetMeNot::Cacheable

Allows the cross-system caching of lengthy function calls

Public Class Methods

cache() click to toggle source
# File lib/forget-me-not/cacheable.rb, line 88
def self.cache
  @cache ||= default_cache
end
cache=(value) click to toggle source
# File lib/forget-me-not/cacheable.rb, line 92
def self.cache=(value)
  @cache = value
end
cache_fetch(key, &block) click to toggle source
# File lib/forget-me-not/cacheable.rb, line 84
def self.cache_fetch(key, &block)
  cache.fetch(key, cache_options, &block)
end
cache_options() click to toggle source
# File lib/forget-me-not/cacheable.rb, line 96
def self.cache_options
  @cache_options ||= {expires_in: 12 * 60 * 60}
  @cache_options.merge(Cacheable.cache_options_threaded)
end
cache_options_threaded() click to toggle source
# File lib/forget-me-not/cacheable.rb, line 101
def self.cache_options_threaded
  Thread.current['cacheable-cache-options'] || {}
end
cache_options_threaded=(options) click to toggle source
# File lib/forget-me-not/cacheable.rb, line 105
def self.cache_options_threaded=(options)
  Thread.current['cacheable-cache-options'] = options
end
cachers() click to toggle source
# File lib/forget-me-not/cacheable.rb, line 109
def self.cachers
  @cachers ||= Set.new
end
cachers_and_descendants() click to toggle source
# File lib/forget-me-not/cacheable.rb, line 113
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
included(base) click to toggle source
# File lib/forget-me-not/cacheable.rb, line 7
def included(base)
  base.extend(ClassMethods)
  Cacheable.cachers << base
end
warm(*args) click to toggle source
# File lib/forget-me-not/cacheable.rb, line 122
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

Private Class Methods

active_support_cache() click to toggle source
# File lib/forget-me-not/cacheable.rb, line 155
def self.active_support_cache
  defined?(ActiveSupport) &&
      defined?(ActiveSupport::Cache) &&
      defined?(ActiveSupport::Cache::MemoryStore) &&
      ActiveSupport::Cache::MemoryStore.new
end
default_cache() click to toggle source
# File lib/forget-me-not/cacheable.rb, line 140
    def self.default_cache
      rails_cache ||
          active_support_cache ||
          raise(
              <<-ERR_TEXT
          When using Cacheable in a project that does not have a Rails or ActiveSupport Cache,
          you must explicitly set the cache to an object shaped like ActiveSupport::Cache::Store
          ERR_TEXT
          )
    end
rails_cache() click to toggle source
# File lib/forget-me-not/cacheable.rb, line 151
def self.rails_cache
  defined?(Rails) && Rails.cache
end