Module: Cachecataz

Defined in:
lib/cachecataz.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Constant Summary collapse

Config =

Default config for Rails.cache, disabled, and [“:”, “/”] delims

Examples:

configure in an environment file (development.rb, production.rb) for Rails


config.after_initialize do
  Cachecataz.enable = true
  Cachecataz.provider = Rails.cache
  Cachecataz.random = false  # default "random" is true, and true is recommended
end
{:api => {:get => :read, :set => :write, :exist? => :exist?}, 
:enabled => false,
:ns_delim => ":",
:index_delim => "/", 
:random => true }

Class Method Summary collapse

Class Method Details

.[](*api_args) ⇒ Object

operator to run the actual calls on the cache provider configured



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/cachecataz.rb', line 56

def self.[](*api_args)
  return false if !Config[:enabled]
  
  api_method = api_args.slice!(0)
  
  case
  when Config[:api][api_method].respond_to?(:call)
    Config[:api][api_method].call(*api_args)
  when Config[:provider].respond_to?(Config[:api][api_method])
    Config[:provider].send(Config[:api][api_method], *api_args)
  else
    raise "Unknown method: #{api_method} for provider: #{Config[:provider]}"
  end
end

.api=(api) ⇒ Object

Config method that maps the api method calls from the provider to the caching server example is the mapping for the Rails.cache provider

Examples:

Cachecataz.api = => :read, :set => :write, :incr => :increment, :exist? => :exist?

Parameters:

  • api (Hash)

    a hash of symbols or procs/lambdas mapped to each of [:get, :set, :incr, :exist?]



50
51
52
53
# File 'lib/cachecataz.rb', line 50

def self.api=(api)
  validate_api(api)
  Config[:api] = api
end

.delim=(val) ⇒ Object

Set custom delimiter if desired



32
33
34
35
# File 'lib/cachecataz.rb', line 32

def self.delim=(val)
  Config[:ns_delim] = val.first rescue ":"
  Config[:index_delim] = val.last rescue "/"
end

.enable=(val) ⇒ Object

Config method to enable Cachecataz

Parameters:

  • val (Boolean)


20
21
22
# File 'lib/cachecataz.rb', line 20

def self.enable=(val)
  Config[:enabled] = val
end

.included(inc_class) ⇒ Object

Method that includes and extends the appropriate modules



79
80
81
82
83
# File 'lib/cachecataz.rb', line 79

def self.included(inc_class)
  inc_class.instance_variable_set(:@_point_keys, {})
  inc_class.send(:include, Cachecataz::InstanceMethods)
  inc_class.send(:extend, Cachecataz::ClassMethods)
end

.provider=(val) ⇒ Object

Config method to assign the provider, for Rails this is Rails.cache

Parameters:

  • val (Object)

    an object that responds to the api provided



40
41
42
# File 'lib/cachecataz.rb', line 40

def self.provider=(val)
  Config[:provider] = val
end

.random=(val) ⇒ Object

Config method to randomize the seed for namespaces in Cachecataz. (default to true, the recommended setting)

Parameters:

  • val (Boolean)


27
28
29
# File 'lib/cachecataz.rb', line 27

def self.random=(val)
  Config[:random] = val
end

.validate_api(api = {}) ⇒ Object

Method that validates the api and provider if they are defined in configuration



72
73
74
75
76
# File 'lib/cachecataz.rb', line 72

def self.validate_api(api={})
  unless api.include?(:get) && api.include?(:set) && api.include?(:exist?)
    raise "Unknown api methods, define [:get, :set, :exist?] to use cachecataz with a non-standard provider"
  end
end