module Sequel::Plugins::ActsAsCacheable::ClassMethods

Attributes

acts_as_cacheable_cache[RW]
acts_as_cacheable_logger[RW]
acts_as_cacheable_time_to_live[RW]

Public Instance Methods

[](*args) click to toggle source
Calls superclass method
# File lib/sequel_acts_as_cacheable.rb, line 31
def [](*args)
  is_primary_key_lookup =
      if 1 == args.size
        if Fixnum == args.first.class
          true
        else
          if String == args.first.class
            begin
              Integer(args.first)
              true
            rescue
              false
            end
          else
            false
          end
        end
      else
        false
      end

  if is_primary_key_lookup
    key = model_cache_key args.first
    begin
      cache_value = @acts_as_cacheable_cache.get key
    rescue Exception => e
      if acts_as_cacheable_logger
        acts_as_cacheable_logger.error "CACHE.get failed in primary key lookup with args #{args.inspect} and model #{name} so using mysql lookup instead, exception was #{e.inspect}"
      end
      cache_value = super *args
    end

    if !cache_value
      cache_value = super *args
      cache_value.cache! if cache_value
    end

    if cache_value.kind_of? self
      cache_value
    else
      nil
    end
  else
    super *args
  end
end
inherited(subclass) click to toggle source

Copy the necessary class instance variables to the subclass.

Calls superclass method
# File lib/sequel_acts_as_cacheable.rb, line 18
def inherited(subclass)
  super
  subclass.acts_as_cacheable_cache = acts_as_cacheable_cache
  subclass.acts_as_cacheable_time_to_live = acts_as_cacheable_time_to_live
  subclass.acts_as_cacheable_logger = acts_as_cacheable_logger
end
model_cache_key(model_id) click to toggle source
# File lib/sequel_acts_as_cacheable.rb, line 25
def model_cache_key model_id
  base_model_klass = self
  base_model_klass = base_model_klass.superclass while Sequel::Model != base_model_klass.superclass
  "#{base_model_klass.name}~#{model_id}"
end