Module: Georeferencer::ObjectCache::ClassMethods
- Defined in:
- lib/georeferencer/mixins/object_cache.rb
Instance Attribute Summary collapse
-
#perform_object_caching ⇒ Object
Returns the value of attribute perform_object_caching.
Instance Method Summary collapse
-
#cache_key_base ⇒ Object
Base of the cache key for this class.
-
#expire_cache_for(*args) ⇒ Object
A method to expire the relevant caches for a collection of objects or ids.
-
#find(*ids) ⇒ Object
Redeclare the find() method, with caching.
Instance Attribute Details
#perform_object_caching ⇒ Object
Returns the value of attribute perform_object_caching.
28 29 30 |
# File 'lib/georeferencer/mixins/object_cache.rb', line 28 def perform_object_caching @perform_object_caching end |
Instance Method Details
#cache_key_base ⇒ Object
Base of the cache key for this class.
31 32 33 |
# File 'lib/georeferencer/mixins/object_cache.rb', line 31 def cache_key_base "georeferencer/#{Georeferencer::VERSION}/#{self.to_s.underscore}" end |
#expire_cache_for(*args) ⇒ Object
A method to expire the relevant caches for a collection of objects or ids
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/georeferencer/mixins/object_cache.rb', line 58 def expire_cache_for(*args) args = args.collect {|a| a.respond_to?(:id) ? a.id : a}.flatten # the caches we need to clear are: # - the object cache # - any collection caches which included this object args.each do |id| object_cache_key = "#{cache_key_base}/#{id}" object_collections = Georeferencer.configuration.cache.read("#{object_cache_key}/collection_hashes") if object_collections.present? # this object has a list of collection hashes, each of which we need to remove object_collections.each do |hash| Georeferencer.configuration.cache.delete("#{cache_key_base}/collection_query/#{hash}") end # by implication, the cached object should also be present; clear that too, along with any of its child keys begin Georeferencer.configuration.cache.delete_matched("#{object_cache_key}*") rescue NotImplementedError # rescue to removing the whole cache if delete_matched isn't supported Georeferencer.configuration.cache.clear end else # the object isn't in any collections; unfortunately we can't be sure whether this is because the object is old # and uncached, or new and therefore needs to be in collections which currently exist for this class. # Because of that, we'll aggressively remove caches for collections begin Georeferencer.configuration.cache.delete_matched("#{cache_key_base}/collection_query*") rescue NotImplementedError Georeferencer.configuration.cache.clear # rescue to cache.clear for caching methods where delete_matched isn't supported end end # Always remove the object's cache. There's no risk of doing this for nonexistent things. Georeferencer.configuration.cache.delete(object_cache_key) end end |
#find(*ids) ⇒ Object
Redeclare the find() method, with caching. Only pass uncached keys to the super method.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/georeferencer/mixins/object_cache.rb', line 36 def find(*ids) if Georeferencer.configuration.perform_caching && perform_object_caching != false ids.uniq! uncached_ids = ids.reject {|i| Georeferencer.configuration.cache.read("#{cache_key_base}/#{i}").present?} [super(*uncached_ids)].flatten.reject(&:blank?).collect do |object| Georeferencer.configuration.logger.debug("Caching #{cache_key_base}/#{object.id}") Georeferencer.configuration.cache.write(object.cache_key, object) end all_objects = ids.collect do |id| Georeferencer.configuration.cache.read("#{cache_key_base}/#{id}") end all_objects.each {|o| o.run_callbacks(:find)} all_objects.length == 1 ? all_objects.first : all_objects else super end end |