Module: FlagpoleSitta::CacheSitta

Extended by:
ActiveSupport::Concern
Defined in:
lib/flagpole_sitta/cache_sitta.rb

Overview

CacheSitta’s main purpose is to make it easier to effectively fragment cache in dynamic fashions in Rails.

When ever a cache is created it is associated with any model and/or record you tell it to be from the view helper method. When that model and/or record is updated all it’s associated caches are cleared.

Flagpole also expects you to put all your database calls into Procs/Lamdbas. This makes it so that your database calls wont have to happen unless your cache hasn’t been created. Thus speeding up response time and reducing database traffic.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#cache_sitta_after_saveObject



168
169
170
# File 'lib/flagpole_sitta/cache_sitta.rb', line 168

def cache_sitta_after_save
  self.post_cache_work
end

#cache_sitta_destoryObject



172
173
174
# File 'lib/flagpole_sitta/cache_sitta.rb', line 172

def cache_sitta_destory
  self.cache_work(false)
end

#cache_sitta_saveObject



164
165
166
# File 'lib/flagpole_sitta/cache_sitta.rb', line 164

def cache_sitta_save
  self.cache_work(true)
end

#cache_work(alive) ⇒ Object

Updates the cache after update of any cache sittaed item.



177
178
179
180
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
216
217
218
# File 'lib/flagpole_sitta/cache_sitta.rb', line 177

def cache_work(alive)
  begin
    original_clazz = self.class
    # Also have to go through all its super objects till the super objects aren't cache sittaed
    # this is because the new updated object for a sub class, could have also been in a cache for
    # said sub class, but also in a cache for its super.
    cur_clazz = original_clazz
    while(cur_clazz.respond_to? :destroy_array_cache)

      #AR - Clear all caches related to the old route_id
      cur_clazz.destroy_array_cache(:route_id => self.try(:send, ("#{cur_clazz.route_id}_was")).to_s)
      #AR - Clear all caches related to the new route_id just in case
      cur_clazz.destroy_array_cache(:route_id => self.try(:send, ("#{cur_clazz.route_id}")).to_s)
      #AR - If the new and old are the same All that will happen on the second call is that
      #it will write the flag out and then destroy it. A very tiny bit of work
      #for a great amount of extra protection.

      # AR - Remember to include models_in_index in your helper call in the corresponding index cache.
      cur_clazz.destroy_array_cache(:obj => self)

      cur_clazz = cur_clazz.superclass
    end

    #AR - For Safety this will not recurse upwards for the extra cache maintenance
    extra_cache_maintenance(alive)
  rescue Exception => e  
    #Keep ending up with one of the array objects having a key of nil. Despite the fact that it would have to at least start with /view
    #becuase of the way its set up in the helper. If that happens all bets are off and just clear everything.
    Rails.cache.clear
    Rails.logger.error("CACHE FAILURE @BEFORE STATE CHANGE CACHE IS BEING NUKED :: FLAGPOLE_SITTA")
    Rails.logger.error(e.message)
    e.backtrace.each do |b|
      Rails.logger.error("\t" + b.to_s)
    end
    puts "CACHE FAILURE @BEFORE STATE CHANGE CACHE IS BEING NUKED :: FLAGPOLE_SITTA"
    puts e.message
    e.backtrace.each do |b|
        puts "\t" + b.to_s
    end
  end

end

#extra_cache_maintenance(alive) ⇒ Object

AR - For Safety this will not recurse upwards for the extra cache maintenance



252
253
254
255
# File 'lib/flagpole_sitta/cache_sitta.rb', line 252

def extra_cache_maintenance alive
  method = (@_cache_extra_maintance || Proc.new{})
  method.call
end

#in_scope(scope) ⇒ Object



257
258
259
260
261
# File 'lib/flagpole_sitta/cache_sitta.rb', line 257

def in_scope scope

  self.class.where(scope).exists?(self.id)

end

#post_cache_workObject

Sense the current in_scope requires the object to be in the database, this has to be called in case the new version that has been saved fits into any cache’s scope. The above call to clear index caches is basically the object_was call, while this is just the call for the update object.



223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/flagpole_sitta/cache_sitta.rb', line 223

def post_cache_work
  begin
    original_clazz = self.class
    cur_clazz = original_clazz

    while(cur_clazz.respond_to? :destroy_array_cache)
      # AR - Remember to include models_in_index in your helper call in the corresponding index cache.
      cur_clazz.destroy_array_cache(:obj => self)

      cur_clazz = cur_clazz.superclass
    end
  rescue Exception => e  
    #Keep ending up with one of the array objects having a key of nil. Despite the fact that it would have to at least start with /view
    #becuase of the way its set up in the helper. If that happens all bets are off and just clear everything.
    Rails.cache.clear
    Rails.logger.error("CACHE FAILURE @AFTER_SAVE CACHE IS BEING NUKED :: FLAGPOLE_SITTA")
    Rails.logger.error(e.message)
    e.backtrace.each do |b|
      Rails.logger.error("\t" + b.to_s)
    end
    puts "CACHE FAILURE @AFTER_SAVE CACHE IS BEING NUKED :: FLAGPOLE_SITTA"
    puts e.message
    e.backtrace.each do |b|
        puts "\t" + b.to_s
    end
  end
end