Module: HydraAttribute::Model::Cacheable::ClassMethods
- Defined in:
- lib/hydra_attribute/model/cacheable.rb
Instance Method Summary collapse
-
#add_to_cache(model) ⇒ NilClass
Add model to all cache objects This method should not be used outside the model.
-
#all ⇒ Array<HydraAttribute::Cacheable>
Finds all records and store them into the cache.
-
#define_cached_singleton_method(method_name, options = {}) ⇒ NilClass
Defines singleton method.
-
#delete_from_cache(model) ⇒ NilClass
Delete model from all cache objects This method should not be used outside the model.
-
#find(id) ⇒ HydraAttribute::Cacheable
Find record by ID and store it into the cache.
-
#get_from_nested_cache_or_load_all_models(nested_cache_key, identifier) ⇒ Object
Gets data from cache or load all models and repeats the operation.
-
#update_cache(model) ⇒ NilClass
Update model in all registered caches This method should not be used outside the model.
Instance Method Details
#add_to_cache(model) ⇒ NilClass
Add model to all cache objects This method should not be used outside the model
91 92 93 |
# File 'lib/hydra_attribute/model/cacheable.rb', line 91 def add_to_cache(model) notify_cache_callbacks('add_to', model) end |
#all ⇒ Array<HydraAttribute::Cacheable>
Finds all records and store them into the cache
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/hydra_attribute/model/cacheable.rb', line 14 def all return identity_map[:all] if identity_map.has_key?(:all) ids = nested_identity_map(:model).keys ids.present? ? where_not(id: ids) : where identity_map[:all] = [] nested_identity_map(:model).each do |_, model| add_to_cache(model) end identity_map[:all] end |
#define_cached_singleton_method(method_name, options = {}) ⇒ NilClass
Defines singleton method
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 72 73 |
# File 'lib/hydra_attribute/model/cacheable.rb', line 45 def define_cached_singleton_method(method_name, = {}) register_nested_cache(method_name) cache_key = [:cache_key] ? ".#{[:cache_key]}" : '' cache_value = [:cache_value] != :self ? ".#{[:cache_value]}" : '' cache_key_was = [:cache_key] ? "#{cache_key}_was" : '' cache_value_was = [:cache_value] != :self ? "#{cache_value}_was" : '' type_cast_key = [:cache_key_cast] ? ".#{[:cache_key_cast]}" : '' instance_eval <<-OES, __FILE__, __LINE__ + 1 def #{method_name}(param) get_from_nested_cache_or_load_all_models(:#{method_name}, param#{type_cast_key}) || [] end private def add_to_#{method_name}_cache(model) add_value_to_nested_cache(:#{method_name}, key: model#{cache_key}, value: model#{cache_value}) end def update_#{method_name}_cache(model) delete_value_from_nested_cache(:#{method_name}, key: model#{cache_key_was}, value: model#{cache_value_was}) add_to_#{method_name}_cache(model) end def delete_from_#{method_name}_cache(model) delete_value_from_nested_cache(:#{method_name}, key: model#{cache_key}, value: model#{cache_value}) end OES end |
#delete_from_cache(model) ⇒ NilClass
Delete model from all cache objects This method should not be used outside the model
109 110 111 |
# File 'lib/hydra_attribute/model/cacheable.rb', line 109 def delete_from_cache(model) notify_cache_callbacks('delete_from', model) end |
#find(id) ⇒ HydraAttribute::Cacheable
Find record by ID and store it into the cache
30 31 32 33 34 |
# File 'lib/hydra_attribute/model/cacheable.rb', line 30 def find(id) model = get_from_nested_cache_or_load_all_models(:model, id.to_i) raise RecordNotFound, "Couldn't find #{name} with id=#{id}" unless model model end |
#get_from_nested_cache_or_load_all_models(nested_cache_key, identifier) ⇒ Object
Gets data from cache or load all models and repeats the operation
80 81 82 83 84 |
# File 'lib/hydra_attribute/model/cacheable.rb', line 80 def get_from_nested_cache_or_load_all_models(nested_cache_key, identifier) return nested_identity_map(nested_cache_key)[identifier] if nested_identity_map(nested_cache_key).has_key?(identifier) all # preload all models nested_identity_map(nested_cache_key)[identifier] end |
#update_cache(model) ⇒ NilClass
Update model in all registered caches This method should not be used outside the model
100 101 102 |
# File 'lib/hydra_attribute/model/cacheable.rb', line 100 def update_cache(model) notify_cache_callbacks('update', model) end |