Module: Cachecataz::InstanceMethods

Defined in:
lib/cachecataz.rb

Instance Method Summary collapse

Instance Method Details

#cache_key(point_key, indexes = [], scope_hash = {}) ⇒ Object

Note:

method removes any index that is already in the namespace definition as it can’t be 2x as unique on the same key

Instance method to return a cache_key for the class

Examples:

user.cache_key(:ck_name, [:id]) # => “0:ck_name/:id”

Parameters:

  • point_key (Symbol)

    name of the cache_space defined on the Class

  • indexes (Array, []) (defaults to: [])

    additional data elements that makeup the key for the instance

  • scope_hash (Hash, self.attributes) (defaults to: {})

    provides the data for the namespace key



174
175
176
177
178
179
180
# File 'lib/cachecataz.rb', line 174

def cache_key(point_key, indexes=[], scope_hash={})
  cache_key_point = cachecataz_key(point_key, scope_hash)
  indexes = [indexes] if !indexes.respond_to?(:each)
  indexes.uniq!
  indexes.reject!{ |i| self.class.point_key(point_key).include?(i) }
  return indexes.inject(cache_key_point){|s, n| s << Cachecataz::Config[:index_delim] << self.cachecataz_index_convert(n) }
end

#cachecataz_index_convert(val) ⇒ String

Note:

if index responds to :call then it will check the arity to determine if it is 1, if so it passes self as the argument

Determines the intended index conversion for index passed to cache_key

Parameters:

  • val (Object)

    the value passed to cache_key index

Returns:

  • (String)

    string to append to namespace key



188
189
190
191
192
193
194
195
196
197
# File 'lib/cachecataz.rb', line 188

def cachecataz_index_convert(val)
  case
  when val.kind_of?(Symbol) && self.respond_to?(val)
    self.send(val).to_s
  when val.respond_to?(:call)
    val.arity == 1 ? val.call(self).to_s : val.call.to_s  
  else
    val.to_s
  end
end

#cachecataz_key(point_key, scope_hash = {}) ⇒ string

Instance method for accessing the cachecataz namespace identifier

Parameters:

  • point_key (Symbol)

    name of the cache_space defined on the Class

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

    provides the data for the namespace key

Returns:

  • (string)

    namespace key for the cachecataz namespace



158
159
160
161
162
163
# File 'lib/cachecataz.rb', line 158

def cachecataz_key(point_key, scope_hash={})
  return "cachecataz disabled" if !Cachecataz::Config[:enabled]

  scope_hash = self.attributes if scope_hash == {} && self.respond_to?(:attributes)
  return self.class.cache_key(point_key, scope_hash)
end

#cachecataz_namespace_reset(point_key, scope_hash = {}) ⇒ Object

Instance method to reset a namespace, shouldn’t really be needed, but avail



215
216
217
218
# File 'lib/cachecataz.rb', line 215

def cachecataz_namespace_reset(point_key, scope_hash={})
  scope_hash = self.attributes if scope_hash == {} && self.respond_to?(:attributes)
  self.class.cachecataz_namespace_reset(point_key, scope_hash)
end

#expire_all_namespaces(scope_hash = {}) ⇒ Object

Instance method to expire all namespaces for an object



209
210
211
212
# File 'lib/cachecataz.rb', line 209

def expire_all_namespaces(scope_hash={})
  scope_hash = self.attributes if scope_hash == {} && self.respond_to?(:attributes)
  self.class.expire_all_namespaces(scope_hash)
end

#expire_namespace(point_key, scope_hash = {}) ⇒ Object

Instance method to expire a cachecataz namespace

Parameters:

  • point_key (Symbol)

    name of the cache_space defined on the Class

  • scope_hash (Hash, self.attributes) (defaults to: {})

    provides the data for the namespace key



203
204
205
206
# File 'lib/cachecataz.rb', line 203

def expire_namespace(point_key, scope_hash={})
  scope_hash = self.attributes if scope_hash == {} && self.respond_to?(:attributes)
  self.class.expire_namespace(point_key, scope_hash)
end