Class: ActiveModelCachers::CacheService

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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ CacheService


● instance methods



24
25
26
# File 'lib/active_model_cachers/cache_service.rb', line 24

def initialize(id)
  @id = id
end

Class Attribute Details

.cache_keyObject

Returns the value of attribute cache_key


8
9
10
# File 'lib/active_model_cachers/cache_service.rb', line 8

def cache_key
  @cache_key
end

.queryObject

Returns the value of attribute query


9
10
11
# File 'lib/active_model_cachers/cache_service.rb', line 9

def query
  @query
end

Class Method Details

.clean_at(id) ⇒ Object


16
17
18
# File 'lib/active_model_cachers/cache_service.rb', line 16

def clean_at(id)
  instance(id).clean_cache
end

.instance(id) ⇒ Object


11
12
13
14
# File 'lib/active_model_cachers/cache_service.rb', line 11

def instance(id)
  hash = (RequestStore.store[self] ||= {})
  return hash[id] ||= new(id)
end

Instance Method Details

#clean_cache(binding: nil) ⇒ Object


38
39
40
41
42
# File 'lib/active_model_cachers/cache_service.rb', line 38

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

#get(binding: nil) ⇒ Object


28
29
30
31
# File 'lib/active_model_cachers/cache_service.rb', line 28

def get(binding: nil)
  @cached_data ||= fetch_from_cache(binding: binding)
  return cache_to_raw_data(@cached_data)
end

#peek(binding: nil) ⇒ Object


33
34
35
36
# File 'lib/active_model_cachers/cache_service.rb', line 33

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