Module: LogicalModel::Cache::ClassMethods

Defined in:
lib/logical_model/cache.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#expires_inObject

Returns the value of attribute expires_in.



68
69
70
# File 'lib/logical_model/cache.rb', line 68

def expires_in
  @expires_in
end

Instance Method Details

#async_find(id, params = {}) ⇒ Object



84
85
86
# File 'lib/logical_model/cache.rb', line 84

def async_find(id, params={})
  super(id, params)
end

#async_find_response(id, params = {}, body) ⇒ Object



103
104
105
# File 'lib/logical_model/cache.rb', line 103

def async_find_response(id, params={}, body)
  super(id, params, body)
end

#async_find_response_with_cache(id, params = {}, body) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/logical_model/cache.rb', line 107

def async_find_response_with_cache(id, params={}, body)
  # remove params not used in cache_key
  %w(app_key token).each {|k| params.delete(k) }
  cache_value = async_find_response_without_cache(id, params, body)
  # Generate key based on params
  cache_key = self.cache_key(id, params)
  self.logger.debug "LogicalModel Log CACHE: Writing cache key=#{cache_key}"
  self.cache_store.write(cache_key, cache_value, :expires_in => self.expires_in || 10.minutes)
  cache_value
end

#async_find_with_cache(id, params = {}, &block) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/logical_model/cache.rb', line 88

def async_find_with_cache(id, params = {}, &block)
  # Generate key based on params
  cache_key = self.cache_key(id, params)
  # If there is a cached value return it
  self.logger.debug "LogicalModel Log CACHE: Reading cache key=#{cache_key}"
  cached_result = self.cache_store.read(cache_key)
  if cached_result
    yield cached_result
  else
    self.logger.debug 'LogicalModel Log CACHE: Cache not present. Calling find_async without cache'
    async_find_without_cache(id, params, &block)
  end
end

#cache_key(id, params = {}) ⇒ Object

Will return key for cache

Parameters:

  • id (String)

    (nil)

  • params (Hash) (defaults to: {})


77
78
79
80
81
82
# File 'lib/logical_model/cache.rb', line 77

def cache_key(id, params = {})
  model_name = self.to_s.pluralize.underscore
  params_hash = Digest::MD5.hexdigest(params.to_s)
  
  cache_key = "#{model_name}/#{id}-#{params_hash}"
end

#cache_storeObject



70
71
72
# File 'lib/logical_model/cache.rb', line 70

def cache_store
  @cache_store ||= Rails.cache
end

#delete(id, params = {}) ⇒ Object



119
120
121
# File 'lib/logical_model/cache.rb', line 119

def delete(id, params={})
  super(id, params)
end

#delete_multiple(ids, params = {}) ⇒ Object



132
133
134
# File 'lib/logical_model/cache.rb', line 132

def delete_multiple(ids, params={})
  super(ids, params)
end

#delete_multiple_with_cache(ids, params = {}) ⇒ Object



136
137
138
139
140
141
142
# File 'lib/logical_model/cache.rb', line 136

def delete_multiple_with_cache(ids, params = {})
  model_name = self.to_s.pluralize.underscore
  self.logger.debug "LogicalModel Log CACHE: Delete cache for #{model_name}\/(#{ids.join('|')})-.*"
  self.cache_store.delete_matched(/#{model_name}\/(#{ids.join('|')})-.*/)
  #TODO: also delete cache for parent (belongs_to)
  delete_multiple_without_cache(ids, params)
end

#delete_with_cache(id, params = {}) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/logical_model/cache.rb', line 123

def delete_with_cache(id, params = {})
  model_name = self.to_s.pluralize.underscore
  self.logger.debug "LogicalModel Log CACHE: Delete cache for #{model_name}\/#{id}-.*"
  self.cache_store.delete_matched(/#{model_name}\/#{id}-.*/)
  #TODO: also delete cache for parent (belongs_to)
  delete_without_cache(id, params)
end