Sha256: 70711c0cafdef56f5d360bbfd48d56f5928f51b6c4e874f322d569376a713128

Contents?: true

Size: 1.23 KB

Versions: 3

Compression:

Stored size: 1.23 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 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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
itrigga-cache-1.1.0 lib/itrigga/cache/memcache.rb
itrigga-cache-0.3.0 lib/itrigga/cache/memcache.rb
itrigga-cache-0.2.1 lib/itrigga/cache/memcache.rb