Module: Sequel::Plugins::ActsAsCacheable::ClassMethods

Defined in:
lib/sequel_acts_as_cacheable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#acts_as_cacheable_cacheObject

Returns the value of attribute acts_as_cacheable_cache.



12
13
14
# File 'lib/sequel_acts_as_cacheable.rb', line 12

def acts_as_cacheable_cache
  @acts_as_cacheable_cache
end

#acts_as_cacheable_time_to_liveObject

Returns the value of attribute acts_as_cacheable_time_to_live.



13
14
15
# File 'lib/sequel_acts_as_cacheable.rb', line 13

def acts_as_cacheable_time_to_live
  @acts_as_cacheable_time_to_live
end

Instance Method Details

#[](*args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/sequel_acts_as_cacheable.rb', line 28

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
      logger.error "CACHE.get failed in primary key lookup with args #{args.inspect} and model #{self.class.name} so using mysql lookup instead, exception was #{e.inspect}"
      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) ⇒ Object

Copy the necessary class instance variables to the subclass.



16
17
18
19
20
# File 'lib/sequel_acts_as_cacheable.rb', line 16

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
end

#model_cache_key(model_id) ⇒ Object



22
23
24
25
26
# File 'lib/sequel_acts_as_cacheable.rb', line 22

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