module Knuckles::Keygen

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.

@example Change global keygen stragey

Knuckles.keygen = MyCustomKeygen.new

Public Instance Methods

expand_key(object) click to toggle source

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.

@param [Object] object An object to caclculate the key from

@return [String] Computed cache key

@example

Knuckles::Keygen.expand_key(model) #=> "MyModel/1/1234567890"
# 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