Method: Memcache#initialize

Defined in:
lib/memcache.rb

#initialize(opts) ⇒ Memcache

Returns a new instance of Memcache.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/memcache.rb', line 35

def initialize(opts)
  @default_expiry   = opts[:default_expiry] || DEFAULT_EXPIRY
  @backup           = opts[:backup] # for multi-level caches
  @hash_with_prefix = opts[:hash_with_prefix].nil? ? true : opts[:hash_with_prefix]

  if opts[:native]
    native_opts = opts.clone
    native_opts[:servers] = (opts[:servers] || [ opts[:server] ]).collect do |server|
      server.is_a?(Hash) ? "#{server[:host]}:#{server[:port]}:#{server[:weight]}" : server
    end
    native_opts[:hash] ||= :crc unless native_opts[:ketama] or native_opts[:ketama_wieghted]
    native_opts[:hash_with_prefix] = @hash_with_prefix

    server_class = opts[:segment_large_values] ? SegmentedNativeServer : NativeServer
    @servers = [server_class.new(native_opts)]
  else
    raise "only CRC hashing is supported unless :native => true" if opts[:hash] and opts[:hash] != :crc

    server_class = opts[:segment_large_values] ? SegmentedServer : Server
    @servers = (opts[:servers] || [ opts[:server] ]).collect do |server|
      case server
      when Hash
        server = server_class.new(opts.merge(server))
      when String
        host, port = server.split(':')
        server = server_class.new(opts.merge(:host => host, :port => port))
      when Class
        server = server.new
      when :local
        server = Memcache::LocalServer.new
      end
      server
    end
  end

  @server = @servers.first if @servers.size == 1 and @backup.nil?
  self.namespace = opts[:namespace] if opts[:namespace]
end