Module: JsonSchematize::Cache::InstanceMethods

Defined in:
lib/json_schematize/cache/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#__cache_key__Object



30
31
32
# File 'lib/json_schematize/cache/instance_methods.rb', line 30

def __cache_key__
  "#{__cache_namespace__}:#{self.class.cache_configuration[:key].call(self, @__incoming_cache_key__)}"
end

#__cache_namespace__Object



34
35
36
# File 'lib/json_schematize/cache/instance_methods.rb', line 34

def __cache_namespace__
  self.class.cache_namespace
end

#__clear_entry__!Object



25
26
27
28
# File 'lib/json_schematize/cache/instance_methods.rb', line 25

def __clear_entry__!
  self.class.cache_client.delete(__cache_key__)
  self.class.__delete_record__!(__cache_key__)
end

#__update_cache_item__(with_delete: true) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/json_schematize/cache/instance_methods.rb', line 15

def __update_cache_item__(with_delete: true)
  __clear_entry__! if with_delete # needs to get done first in the event the cache_key changes
  client = self.class.cache_client
  ttl = self.class.cache_configuration[:ttl].to_i
  score = Time.now.to_i + ttl
  self.class.__update_record_keeper__!(expire: score, cache_key: __cache_key__)

  client.write(__cache_key__, Marshal.dump(self), expires_in: ttl)
end

#initialize(stringified_params = nil, cache_key: nil, skip_cache_update: false, raise_on_error: true, **params) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/json_schematize/cache/instance_methods.rb', line 6

def initialize(stringified_params = nil, cache_key: nil, skip_cache_update: false, raise_on_error: true, **params)
  super(stringified_params, raise_on_error: raise_on_error, **params)

  @__incoming_cache_key__ = cache_key
  if @values_assigned
    __update_cache_item__(with_delete: false) unless skip_cache_update
  end
end