Module: BennyCache::Model::ClassMethods

Defined in:
lib/benny_cache/model.rb

Instance Method Summary collapse

Instance Method Details

#benny_data_cache_delete(model_id, data_index) ⇒ Object

Clear the a data cache from a given model



60
61
62
63
# File 'lib/benny_cache/model.rb', line 60

def benny_data_cache_delete(model_id, data_index)
  full_index = self.benny_data_cache_full_index(model_id, data_index)
  BennyCache::Config.store.delete(full_index)
end

#benny_data_cache_full_index(model_id, data_index) ⇒ Object

:nodoc:



80
81
82
83
84
# File 'lib/benny_cache/model.rb', line 80

def benny_data_cache_full_index(model_id, data_index) # :nodoc:
  raise "undefined cache data key '#{data_index}'" unless self.class_variable_get(:@@BENNY_DATA_INDEXES).include?(data_index.to_s)
  ns = self.get_benny_model_ns
  full_index = "#{ns}/#{model_id}/data/#{data_index.to_s}"
end

#benny_data_index(*options) ⇒ Object



157
158
159
# File 'lib/benny_cache/model.rb', line 157

def benny_data_index(*options)
  self.class_variable_get(:@@BENNY_DATA_INDEXES).push(*(options.map(&:to_s)))
end

#benny_method_args_sig(*options) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/benny_cache/model.rb', line 161

def benny_method_args_sig(*options)
  sig =  ''
  options.each { |arg|
      if arg.respond_to?(:id)
        sig += "#{arg.class}/##{arg.id}"
      elsif arg.is_a?(Array)
        arg.each do |val|
          sig += benny_method_args_sig(*val)
        end
      elsif arg.is_a?(Hash)
        arg.keys.sort.each do |key|
          sig += benny_method_args_sig(key)  + benny_method_args_sig(*(arg[key]))
        end
      else
        sig += Digest::SHA1.hexdigest(Marshal.dump(arg))
      end
  }
  sig
end

#benny_method_cache_delete(model_id, data_index) ⇒ Object

Clear the a method cache from a given model



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/benny_cache/model.rb', line 66

def benny_method_cache_delete(model_id, data_index)
  full_index = self.benny_method_cache_full_index(model_id, data_index)

  keys = BennyCache::Config.store.fetch(full_index)

  unless (keys.nil?  || keys.empty?)
    keys.each do |key|
      BennyCache::Config.store.delete(key)
    end
  end
  BennyCache::Config.store.delete(full_index)

end

#benny_method_cache_full_index(model_id, method_index) ⇒ Object

:nodoc:



86
87
88
89
90
# File 'lib/benny_cache/model.rb', line 86

def benny_method_cache_full_index(model_id, method_index) # :nodoc:
  raise "undefined cache method key '#{method_index}'" unless self.class_variable_get(:@@BENNY_METHOD_INDEXES).include?(method_index.to_s)
  ns = self.get_benny_model_ns
  full_index = "#{ns}/#{model_id}/method/#{method_index.to_s}"
end

#benny_method_index(*options) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/benny_cache/model.rb', line 181

def benny_method_index(*options)
  self.class_variable_get(:@@BENNY_METHOD_INDEXES).push(*(options.map(&:to_s)))

  options.each do |method_name|

    define_method "#{method_name}_with_benny_cache" do |*method_opts|
      @_benny_method_local_cache ||= {}
      #puts "benny cache method: #{method_name}_with_benny_cache  #{method_opts.inspect}"

      model_id = self.id
      base_method_index = self.class.benny_method_cache_full_index(model_id, method_name)

      sig = self.class.benny_method_args_sig(*method_opts)

      args_method_index = "#{base_method_index}/args/#{sig}"

      self.class.benny_method_store_method_args_index(base_method_index, args_method_index)

      return @_benny_method_local_cache[args_method_index] if @_benny_method_local_cache[args_method_index]

      return_data = BennyCache::Config.store.fetch(args_method_index) {
        self.send("#{method_name}_without_benny_cache", *method_opts)
      }

      @_benny_method_local_cache[args_method_index] = return_data
      return_data

    end
    alias_method "#{method_name}_without_benny_cache", method_name
    alias_method method_name, "#{method_name}_with_benny_cache"
    alias_method "#{method_name}!", "#{method_name}_without_benny_cache"

  end

end

#benny_method_store_method_args_index(base_method_index, args_method_index) ⇒ Object

For each benny cached method, we store an array cached results. Each result is unique to the parameters used during the method call. Meaning: foo.bar(:baz) and foo.bar(:bin) are stored as two separate results, with their own key in the benny cache. Additionally, these various keys associated with foo.bar are stored as an separate array in their own cache entry. When it’s time to clear the all the foo.bar cached results, we will have an array of keys to reference.



100
101
102
103
104
105
106
# File 'lib/benny_cache/model.rb', line 100

def benny_method_store_method_args_index(base_method_index, args_method_index)
  method_sig_ary = BennyCache::Config.store.fetch(base_method_index) { [] }
  unless method_sig_ary.include?(args_method_index)
    method_sig_ary.push(args_method_index)
    BennyCache::Config.store.write(base_method_index, method_sig_ary)
  end
end

#benny_method_store_method_args_indexes_delete(model_id, method_name) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/benny_cache/model.rb', line 108

def benny_method_store_method_args_indexes_delete(model_id, method_name)
  base_method_index = self.benny_method_cache_full_index(model_id, method_name)

  method_sig_ary = BennyCache::Config.store.fetch(base_method_index) { [] }
  method_sig_ary.each do |args_method_index|
    BennyCache::Config.store.clear(args_method_index)
  end
  BennyCache::Config.store.clear(base_method_index)
end

#benny_model_cache(options) ⇒ Object

Retrieves a model from the cache. If the model is no in the cache, BennyCache will load it from the database and store in the cache.

agent = Agent.benny_model_cache(1)

If the agent with id of 1 is not in the cache, it will make an ActiveRecord call to popuplate the cache, and return the model, like so:

Agent.find(1)

If you have declared separate data indexes, you can pass a hash and and BennyCache will use ActiveRelation#where to populate the hash

Agent.benny_model_cache(:user_id => 999)

To populate cache, BennyCache will call

Agent.where(:user_id => 999)


236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/benny_cache/model.rb', line 236

def benny_model_cache(options)
  ns = self.get_benny_model_ns

  if options.is_a?(Hash)
    key_format = []
    key = []
    options.keys.sort.each do  |k|
      key_format << "#{k.to_s}/:#{k.to_s}"
      key << "#{k.to_s}/#{options[k].to_s}"
    end

    key = key.join('/')

    key_format = key_format.join('/')

    raise "undefined cache key format #{ns}/#{key_format}" unless self.class_variable_get(:@@BENNY_MODEL_INDEXES).include?(key_format)

    BennyCache::Config.store.fetch("#{ns}/#{key}") {
      self.where(options).first
    }
  else # should be a number/id
    BennyCache::Config.store.fetch("#{ns}/#{options}") {
      self.find(options)
    }
  end
end

#benny_model_index(*options) ⇒ Object

Declares one or more caching indexes for instances of this class. You do not have to declare an :id index, but if you will be referencing or loading models by other indexes, declare them here.

Explicit declarations are needed so BennyCache knows which cache keys to clear on a relevant change.

Valid options are symbols of other methods, or for multiple-field indexes, an array of :symbols

class Agent
  benny_model_index :user_id
  # internally works like Agent.where(:user_id => user_id ).first when referenced
end

or

class Location
  benny_model_index [:x, :y]
  # internally works like Locaion.where(:x => x, :y => y ).first when referenced
end

You can include many indexes in the declaration:

class Foo
  benny_model_index :bar, :baz, [:zip, :zap]
end


146
147
148
149
150
151
152
153
154
155
# File 'lib/benny_cache/model.rb', line 146

def benny_model_index(*options)
  index_keys = options.map do |idx|
    if idx.is_a?(Array)
        idx.map{ |jdx| "#{jdx.to_s}/:#{jdx.to_s}"}.join("/")
    else
       "#{idx.to_s}/:#{idx.to_s}"
    end
  end
  self.class_variable_get(:@@BENNY_MODEL_INDEXES).push(*index_keys)
end