Sha256: d19da0be2c16bf2f67164d31b65ef8d36429d530884362fdc580253d5865ad13

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

# 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 Dalli
      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 "Dalli client init with: #{servers.inspect} - #{opts.inspect}"
          instance.cache = ::Dalli::Client.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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
itrigga-cache-1.1.0 lib/itrigga/cache/dalli.rb