Class: ActiveModelCachers::CacheService

Inherits:
Object
  • Object
show all
Defined in:
lib/active_model_cachers/cache_service.rb

Constant Summary collapse

@@column_value_cache =
ActiveModelCachers::ColumnValueCache.new

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ CacheService


● instance methods




64
65
66
# File 'lib/active_model_cachers/cache_service.rb', line 64

def initialize(id)
  @id = id
end

Class Attribute Details

.cache_keyObject

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_mappingObject

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
# 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_touch(class_name) do
      clean.call(send(foreign_key))
    end

    after_commit(class_name) do # TODO: on
      next if fire_on and not transaction_include_any_action?(fire_on)
      changed = column ? previous_changes.key?(column) : previous_changes.present?
      clean.call(send(foreign_key)) if changed || destroyed?
    end

    pre_before_delete(class_name) do |id, model|
      clean_ids << @@column_value_cache.add(self, class_name, id, foreign_key, model)
    end

    before_delete(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



78
79
80
81
82
# File 'lib/active_model_cachers/cache_service.rb', line 78

def clean_cache(binding: nil, reflect: nil)
  @cached_data = nil
  Rails.cache.delete(cache_key)
  return nil
end

#get(binding: nil, reflect: nil) ⇒ Object



68
69
70
71
# File 'lib/active_model_cachers/cache_service.rb', line 68

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



73
74
75
76
# File 'lib/active_model_cachers/cache_service.rb', line 73

def peek(binding: nil, reflect: nil)
  @cached_data ||= get_from_cache
  return cache_to_raw_data(@cached_data)
end