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
end
{:api => {:get => :read, :set => :write, :incr => :increment, :exist? => :exist?}, 
:enabled => false,
:ns_delim => ":",
:index_delim => "/" }

Class Method Summary collapse

Class Method Details

.[](*api_args) ⇒ Object

operator to run the actual calls on the cache provider configured



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cachecataz.rb', line 47

def self.[](*api_args)
  return false if !Config[:enabled]
  
  api_method = api_args.slice!(0)
  
  if Config[:api][api_method].respond_to?(:call)
    Config[:api][api_method].call(*api_args)
  else
    Config[:provider].send(Config[:api][api_method], *api_args)    
  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?]



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

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

.delim=(val) ⇒ Object

Set custom delimiter if desired



23
24
25
26
# File 'lib/cachecataz.rb', line 23

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)


18
19
20
# File 'lib/cachecataz.rb', line 18

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

.included(inc_class) ⇒ Object

Method that includes and extends the appropriate modules



67
68
69
70
71
# File 'lib/cachecataz.rb', line 67

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



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

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

.validate_api(api = {}) ⇒ Object

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



60
61
62
63
64
# File 'lib/cachecataz.rb', line 60

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