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



162
163
164
165
166
167
168
# File 'lib/cachecataz.rb', line 162

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



176
177
178
179
180
181
182
183
184
185
# File 'lib/cachecataz.rb', line 176

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



146
147
148
149
150
151
# File 'lib/cachecataz.rb', line 146

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



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

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



197
198
199
200
# File 'lib/cachecataz.rb', line 197

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



191
192
193
194
# File 'lib/cachecataz.rb', line 191

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