Module: BennyCache::Model

Defined in:
lib/benny_cache/model.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
# File 'lib/benny_cache/model.rb', line 6

def self.included(base)
  base.instance_eval {
    include BennyCache::Base
  }

  base.extend BennyCache::Model::ClassMethods

  unless base.class_variable_defined? :@@BENNY_MODEL_INDEXES
    base.class_variable_set(:@@BENNY_MODEL_INDEXES, [])
  end

  unless base.class_variable_defined? :@@BENNY_DATA_INDEXES
    base.class_variable_set(:@@BENNY_DATA_INDEXES, [])
  end

  unless base.class_variable_defined? :@@BENNY_METHOD_INDEXES
    base.class_variable_set(:@@BENNY_METHOD_INDEXES, [])
  end

  if base.respond_to? :after_save
    base.after_save :benny_model_cache_delete
  end

  if base.respond_to? :after_destroy
    base.after_destroy :benny_model_cache_delete
  end

  def benny_model_cache_delete
    ns = self.class.get_benny_model_ns
    key = "#{ns}/#{self.id}"

    BennyCache::Config.store.delete(key)
    self.class.class_variable_get(:@@BENNY_MODEL_INDEXES).each do |idx|

      if idx.is_a?(Symbol)
        key =  "#{ns}/#{idx}/" + idx.to_s.gsub(/(\w+)/) { self.send($1) }
      elsif idx.is_a?(String)
        key =  "#{ns}/" +  idx.to_s.gsub(/:(\w+)/) { "#{self.send($1) }" }
      end

      BennyCache::Config.store.delete(key)
    end
  end

  def benny_data_cache(data_index, &block)
    full_index = self.class.benny_data_cache_full_index(self.id, data_index)
    BennyCache::Config.store.fetch(full_index, &block)
  end
end

Instance Method Details

#benny_data_cache(data_index, &block) ⇒ Object



50
51
52
53
# File 'lib/benny_cache/model.rb', line 50

def benny_data_cache(data_index, &block)
  full_index = self.class.benny_data_cache_full_index(self.id, data_index)
  BennyCache::Config.store.fetch(full_index, &block)
end

#benny_model_cache_deleteObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/benny_cache/model.rb', line 33

def benny_model_cache_delete
  ns = self.class.get_benny_model_ns
  key = "#{ns}/#{self.id}"

  BennyCache::Config.store.delete(key)
  self.class.class_variable_get(:@@BENNY_MODEL_INDEXES).each do |idx|

    if idx.is_a?(Symbol)
      key =  "#{ns}/#{idx}/" + idx.to_s.gsub(/(\w+)/) { self.send($1) }
    elsif idx.is_a?(String)
      key =  "#{ns}/" +  idx.to_s.gsub(/:(\w+)/) { "#{self.send($1) }" }
    end

    BennyCache::Config.store.delete(key)
  end
end