Method: Cache#initialize

Defined in:
lib/cache.rb

#initialize(metal) ⇒ Cache

Create a new Cache instance by wrapping a client of your choice.

Supported memcached clients:

  • memcached (either a Memcached or a Memcached::Rails)

  • dalli (either a Dalli::Client or an ActiveSupport::Cache::DalliStore)

  • memcache-client (MemCache, the one commonly used by Rails)

Supported Redis clients:

metal - Either an instance of one of the above classes, or :nothing if you

want to use the Cache API but not actually cache anything.

Examples:

# Use Memcached
raw_client = Memcached.new('127.0.0.1:11211')
cache = Cache.new(raw_client)
cache.get(...)
cache.set(...)

# Don't cache anything
cache = Cache.new(:nothing)
cache.get(...)
cache.set(...)


50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cache.rb', line 50

def initialize(metal)
  @pid = ::Process.pid
  @config = Config.new
  if metal != :nothing
    @metal = Cache === metal ? metal.metal : metal
  end
  if @metal
    client_class = @metal.class.to_s
    driver_class = client_class.gsub('::', '')
  else
    client_class = 'Nothing'
    driver_class = 'Nothing'
  end
  filename = client_class.
    gsub(/([a-z])([A-Z]+)/) { [$1.downcase, $2.downcase].join('_') }.
    gsub('::', '_').downcase
  begin
    require "cache/#{filename}"
    extend Cache.const_get(driver_class)
  rescue LoadError, NameError => e
    puts "#{e.class}: #{e.message}"
    puts e.backtrace[0..5].join("\n")
    raise DriverNotFound, client_class
  end
end