Class: ActiveModelCachers::CacheService
- Inherits:
-
Object
- Object
- ActiveModelCachers::CacheService
- Defined in:
- lib/active_model_cachers/cache_service.rb
Constant Summary collapse
- @@column_value_cache =
ActiveModelCachers::ColumnValueCache.new
Class Attribute Summary collapse
-
.cache_key ⇒ Object
Returns the value of attribute cache_key.
-
.query_mapping ⇒ Object
Returns the value of attribute query_mapping.
Class Method Summary collapse
- .clean_at(id) ⇒ Object
- .define_callback_for_cleaning_cache(class_name, column, foreign_key, with_id, on: nil) ⇒ Object
- .instance(id) ⇒ Object
Instance Method Summary collapse
- #clean_cache(binding: nil, reflect: nil) ⇒ Object
- #get(binding: nil, reflect: nil) ⇒ Object
-
#initialize(id) ⇒ CacheService
constructor
—————————————————————- ● instance methods —————————————————————-.
- #peek(binding: nil, reflect: nil) ⇒ Object
Constructor Details
#initialize(id) ⇒ CacheService
● instance methods
74 75 76 |
# File 'lib/active_model_cachers/cache_service.rb', line 74 def initialize(id) @id = id end |
Class Attribute Details
.cache_key ⇒ Object
Returns the value of attribute cache_key.
9 10 11 |
# File 'lib/active_model_cachers/cache_service.rb', line 9 def cache_key @cache_key end |
.query_mapping ⇒ Object
Returns the value of attribute query_mapping.
10 11 12 |
# File 'lib/active_model_cachers/cache_service.rb', line 10 def query_mapping @query_mapping end |
Class Method Details
.clean_at(id) ⇒ Object
17 18 19 |
# File 'lib/active_model_cachers/cache_service.rb', line 17 def clean_at(id) instance(id).clean_cache end |
.define_callback_for_cleaning_cache(class_name, column, foreign_key, with_id, on: nil) ⇒ Object
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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/active_model_cachers/cache_service.rb', line 22 def define_callback_for_cleaning_cache(class_name, column, foreign_key, with_id, on: nil) return if @callbacks_defined @callbacks_defined = true clean = ->(id){ clean_at(with_id ? id : nil) } clean_ids = [] fire_on = Array(on) if on ActiveRecord::Extension.global_callbacks.instance_exec do on_nullify(class_name) do |nullified_column, get_ids| get_ids.call.each{|s| clean.call(s) } if nullified_column == column end after_touch1(class_name) do clean.call(@@column_value_cache.add(self.class, class_name, id, foreign_key, self).call) end after_touch2(class_name) do @@column_value_cache.clean_cache end after_commit1(class_name) do next if fire_on and not transaction_include_any_action?(fire_on) changed = column ? previous_changes.key?(column) : previous_changes.present? if changed || destroyed? clean.call(@@column_value_cache.add(self.class, class_name, id, foreign_key, self).call) end end after_commit2(class_name) do @@column_value_cache.clean_cache end before_delete1(class_name) do |id, model| clean_ids << @@column_value_cache.add(self, class_name, id, foreign_key, model) end before_delete2(class_name) do |_, model| clean_ids.each{|s| clean.call(s.call) } clean_ids = [] end after_delete(class_name) do @@column_value_cache.clean_cache end end end |
.instance(id) ⇒ Object
12 13 14 15 |
# File 'lib/active_model_cachers/cache_service.rb', line 12 def instance(id) hash = (RequestStore.store[self] ||= {}) return hash[id] ||= new(id) end |
Instance Method Details
#clean_cache(binding: nil, reflect: nil) ⇒ Object
88 89 90 91 92 |
# File 'lib/active_model_cachers/cache_service.rb', line 88 def clean_cache(binding: nil, reflect: nil) @cached_data = nil Rails.cache.delete(cache_key) return nil end |
#get(binding: nil, reflect: nil) ⇒ Object
78 79 80 81 |
# File 'lib/active_model_cachers/cache_service.rb', line 78 def get(binding: nil, reflect: nil) @cached_data ||= fetch_from_cache(binding: binding, reflect: reflect) return cache_to_raw_data(@cached_data) end |
#peek(binding: nil, reflect: nil) ⇒ Object
83 84 85 86 |
# File 'lib/active_model_cachers/cache_service.rb', line 83 def peek(binding: nil, reflect: nil) @cached_data ||= get_from_cache return cache_to_raw_data(@cached_data) end |