Module: JsonSchematize::Cache::InstanceMethods

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

Instance Method Summary collapse

Instance Method Details

#__cache_key__Object



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

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

#__cache_namespace__Object



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

def __cache_namespace__
  self.class.cache_namespace
end

#__clear_entry__!Object



22
23
24
25
# File 'lib/json_schematize/cache/instance_methods.rb', line 22

def __clear_entry__!
  self.class.redis_client.zrem(__cache_namespace__, __cache_key__)
  self.class.redis_client.unlink(__cache_key__)
end

#__update_cache_item__(with_delete: true) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/json_schematize/cache/instance_methods.rb', line 13

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.redis_client
  ttl = self.class.cache_configuration[:ttl].to_i
  score = Time.now.to_i + ttl
  client.zadd(__cache_namespace__, score, __cache_key__)
  client.set(__cache_key__, Marshal.dump(self), ex: ttl)
end

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



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

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