Class: Memcached::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/memcached/client.rb

Constant Summary collapse

FLAGS =
0b0
FLAG_ENCODED =
0b1
DEFAULTS =
{
  :hash => :fnv1_32,
  :distribution => :consistent_ketama,
  :ketama_weighted => true,
  :verify_key => true,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = "localhost:11211", options = {}) ⇒ Client

Returns a new instance of Client


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/memcached/client.rb', line 23

def initialize(config = "localhost:11211", options = {})
  options = DEFAULTS.merge(options)

  @codec = Memcached::MarshalCodec
  @default_ttl = options.delete(:ttl) || 0
  @prefix = options.delete(:prefix_key)
  @credentials = options.delete(:credentials)
  if !@credentials && ENV["MEMCACHE_USERNAME"] && ENV["MEMCACHE_PASSWORD"]
    @credentials = [ENV["MEMCACHE_USERNAME"], ENV["MEMCACHE_PASSWORD"]]
  end
  # SASL requires binary protocol
  options[:binary_protocol] = true if @credentials

  @connection = nil
  @behaviors = normalize_behaviors(options)
  @config = create_config_str(config)
  Memcached::Connection.check_config!(@config)
end

Instance Attribute Details

#behaviorsObject (readonly)

Returns the value of attribute behaviors


21
22
23
# File 'lib/memcached/client.rb', line 21

def behaviors
  @behaviors
end

#configObject (readonly)

Returns the value of attribute config


21
22
23
# File 'lib/memcached/client.rb', line 21

def config
  @config
end

Instance Method Details

#add(key, value, ttl: @default_ttl, raw: false, flags: FLAGS) ⇒ Object


80
81
82
83
84
85
# File 'lib/memcached/client.rb', line 80

def add(key, value, ttl: @default_ttl, raw: false, flags: FLAGS)
  return false unless key

  value, flags = @codec.encode(key, value, flags) unless raw
  connection.add(key, value, ttl, flags)
end

#append(key, value, ttl: @default_ttl, flags: FLAGS) ⇒ Object


110
111
112
# File 'lib/memcached/client.rb', line 110

def append(key, value, ttl: @default_ttl, flags: FLAGS)
  connection.append(key, value, ttl, flags)
end

#cloneObject


127
128
129
130
131
# File 'lib/memcached/client.rb', line 127

def clone
  client = super
  client.instance_variable_set(:@connection, @connection.clone) if @connection
  client
end

#connectionObject


140
141
142
143
144
145
146
# File 'lib/memcached/client.rb', line 140

def connection
  @connection ||= Memcached::Connection.new(@config).tap do |conn|
    conn.set_prefix(@prefix)
    conn.set_behaviors(@behaviors)
    conn.set_credentials(*@credentials) if @credentials
  end
end

#decrement(key, offset = 1) ⇒ Object


91
92
93
# File 'lib/memcached/client.rb', line 91

def decrement(key, offset = 1)
  connection.decrement(key, offset)
end

#delete(key) ⇒ Object


76
77
78
# File 'lib/memcached/client.rb', line 76

def delete(key)
  connection.delete(key)
end

#exist(key) ⇒ Object


95
96
97
# File 'lib/memcached/client.rb', line 95

def exist(key)
  connection.exist(key)
end

#flushObject


42
43
44
# File 'lib/memcached/client.rb', line 42

def flush
  connection.flush
end

#get(key, raw: nil) ⇒ Object


53
54
55
56
57
58
59
60
61
# File 'lib/memcached/client.rb', line 53

def get(key, raw: nil)
  value, flags = connection.get(key)
  return nil unless value
  if raw != true
    value = @codec.decode(key, value, flags)
  end

  value
end

#get_multi(keys, raw: nil) ⇒ Object


63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/memcached/client.rb', line 63

def get_multi(keys, raw: nil)
  keys = keys.compact
  hash = connection.get_multi(keys)
  hash.each do |key, (value, flags)|
    if raw != true
      hash[key] = @codec.decode(key, value, flags)
    else
      hash[key] = value
    end
  end
  hash
end

#increment(key, offset = 1) ⇒ Object


87
88
89
# File 'lib/memcached/client.rb', line 87

def increment(key, offset = 1)
  connection.increment(key, offset)
end

#namespaceObject


114
115
116
# File 'lib/memcached/client.rb', line 114

def namespace
  @prefix
end

#namespace=(value) ⇒ Object


118
119
120
121
# File 'lib/memcached/client.rb', line 118

def namespace=(value)
  connection.set_prefix(value)
  @prefix = value
end

#prepend(key, value, ttl: @default_ttl, flags: FLAGS) ⇒ Object


106
107
108
# File 'lib/memcached/client.rb', line 106

def prepend(key, value, ttl: @default_ttl, flags: FLAGS)
  connection.prepend(key, value, ttl, flags)
end

#replace(key, value, ttl: @default_ttl, raw: false, flags: FLAGS) ⇒ Object


99
100
101
102
103
104
# File 'lib/memcached/client.rb', line 99

def replace(key, value, ttl: @default_ttl, raw: false, flags: FLAGS)
  return false unless key

  value, flags = @codec.encode(key, value, flags) unless raw
  connection.replace(key, value, ttl, flags)
end

#resetObject


133
134
135
136
137
138
# File 'lib/memcached/client.rb', line 133

def reset
  if @connection
    @connection.close
    @connection = nil
  end
end

#set(key, value, ttl: @default_ttl, raw: false, flags: FLAGS) ⇒ Object


46
47
48
49
50
51
# File 'lib/memcached/client.rb', line 46

def set(key, value, ttl: @default_ttl, raw: false, flags: FLAGS)
  return false unless key

  value, flags = @codec.encode(key, value, flags) unless raw
  connection.set(key, value, ttl, flags)
end

#touch(key, ttl = @default_ttl) ⇒ Object


123
124
125
# File 'lib/memcached/client.rb', line 123

def touch(key, ttl = @default_ttl)
  connection.touch(key, ttl)
end