# http://stackoverflow.com/questions/4217968/a-problem-about-singleton-in-ruby # # To implement a backend it needs to respond to: # - self.setup! # - get # - set # - delete # require 'singleton' module Itrigga module Cache class Memcache include Singleton attr_accessor :cache, :enabled def initialize @cache = nil end def self.setup!(opts = {}) servers = opts[:servers] || "localhost:11211" opts.delete(:servers) opts[:default_ttl] ||= opts[:timeout] if opts[:timeout] # rename default_ttl to something easier to use opts.delete(:timeout) instance.enabled = true puts "Memcached init with: #{servers.inspect} - #{opts.inspect}" instance.cache = ::Memcached.new(servers, opts) end def get(key, opts = {}) cache.get key end def set(key, value, opts = {}) if opts[:timeout] cache.set key, value, opts[:timeout] else cache.set key, value end end def delete(key, opts = {}) cache.delete key end end end end