Module: Knuckles::Keygen

Extended by:
Keygen
Included in:
Keygen
Defined in:
lib/knuckles/keygen.rb

Overview

The Keygen module provides simple cache key generation. The global keygen strategy can be changed at the top level by configuring ‘Knuckles.keygen`.

Any object that responds to ‘expand_key` with a single argument can be used instead.

Examples:

Change global keygen stragey


Knuckles.keygen = MyCustomKeygen.new

Instance Method Summary collapse

Instance Method Details

#expand_key(object) ⇒ String

Calculates a cache key for the given object. It first attempts to use the object’s ‘cache_key` method (present on `ActiveRecord` models). It falls back to combining the object’s ‘id` and `updated_at` values.

Examples:


Knuckles::Keygen.expand_key(model) #=> "MyModel/1/1234567890"

Parameters:

  • object (Object)

    An object to caclculate the key from

Returns:

  • (String)

    Computed cache key



29
30
31
32
33
34
35
# File 'lib/knuckles/keygen.rb', line 29

def expand_key(object)
  if object.respond_to?(:cache_key)
    object.cache_key
  else
    "#{object.class.name}/#{object.id}/#{object.updated_at.to_i}"
  end
end