Class: MemCache

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

Defined Under Namespace

Classes: MemCacheError

Constant Summary collapse

VERSION =
'1.7.1'
DEFAULT_OPTIONS =

Default options for the cache object.

{
  :namespace   => nil,
  :readonly    => false,
  :multithread => true,
  :pool_initial_size => 10,
  :pool_min_size => 5,
  :pool_max_size => 100,
  :pool_max_idle => (1000 * 60 * 5),
  :pool_max_busy => (1000 * 30),
  :pool_maintenance_thread_sleep => (1000 * 30),
  :pool_socket_timeout => (1000 * 3),
  :pool_socket_connect_timeout => (1000 * 3),
  :pool_use_alive => false,
  :pool_use_failover => true,
  :pool_use_failback => true,
  :pool_use_nagle => false,
  :pool_name => 'default',
  :log_level => 2
}
MARSHALLING_CHARSET =

CHARSET for Marshalling

'UTF-8'
DEFAULT_PORT =

Default memcached port.

11211
DEFAULT_WEIGHT =

Default memcached server weight.

1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ MemCache

Configures the client



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/memcache.rb', line 67

def initialize(*args)
  @servers = []
  opts = {}

  case args.length
  when 0 then # NOP
  when 1 then
    arg = args.shift
    case arg
    when Hash   then opts = arg
    when Array  then @servers = arg
    when String then @servers = [arg]
    else raise ArgumentError, 'first argument must be Array, Hash or String'
    end
  when 2 then
    @servers, opts = args
    @servers = [@servers].flatten
  else
    raise ArgumentError, "wrong number of arguments (#{args.length} for 2)"
  end

  # Normalizing the server(s) so they all have a port number.

  @servers = @servers.map do |server|
    server =~ /(.+):(\d+)/ ? server : "#{server}:#{DEFAULT_PORT}"
  end

  Logger.getLogger('com.meetup.memcached.MemcachedClient').setLevel(opts[:log_level])
  Logger.getLogger('com.meetup.memcached.SockIOPool').setLevel(opts[:log_level])

  opts = DEFAULT_OPTIONS.merge opts

  @namespace = opts[:namespace] || opts["namespace"]
  @pool_name = opts[:pool_name] || opts["pool_name"]
  @readonly = opts[:readonly] || opts["readonly"]

  @client = MemCachedClient.new(@pool_name)

  @client.error_handler = opts[:error_handler] if opts[:error_handler]
  @client.primitiveAsString = true
  @client.sanitizeKeys = false

  weights = Array.new(@servers.size, DEFAULT_WEIGHT)

  @pool = SockIOPool.getInstance(@pool_name)
  unless @pool.initialized?
    @pool.servers = @servers.to_java(:string)
    @pool.weights = weights.to_java(:Integer)

    @pool.initConn = opts[:pool_initial_size]
    @pool.minConn = opts[:pool_min_size]
    @pool.maxConn = opts[:pool_max_size]

    @pool.maxIdle = opts[:pool_max_idle]
    @pool.maxBusyTime = opts[:pool_max_busy]
    @pool.maintSleep = opts[:pool_maintenance_thread_sleep]
    @pool.socketTO = opts[:pool_socket_timeout]
    @pool.socketConnectTO = opts[:pool_socket_connect_timeout]

    @pool.failover = opts[:pool_use_failover]
    @pool.failback = opts[:pool_use_failback]
    @pool.aliveCheck = opts[:pool_use_alive]
    @pool.nagle = opts[:pool_use_nagle]

    # public static final int NATIVE_HASH     = 0;        
    #     // native String.hashCode();
    # public static final int OLD_COMPAT_HASH = 1;        
    #     // original compatibility hashing algorithm (works with other clients)
    # public static final int NEW_COMPAT_HASH = 2;
    #     // new CRC32 based compatibility hashing algorithm (works with other clients)
    # public static final int CONSISTENT_HASH = 3;        
    #     // MD5 Based -- Stops thrashing when a server added or removed
    @pool.hashingAlg = opts[:pool_hashing_algorithm]

    # __method methods have been removed in jruby 1.5
    @pool.java_send :initialize rescue @pool.initialize__method
  end

end

Instance Attribute Details

#multithreadObject (readonly)

The multithread setting for this instance



59
60
61
# File 'lib/memcache.rb', line 59

def multithread
  @multithread
end

#namespaceObject (readonly)

The namespace for this instance



54
55
56
# File 'lib/memcache.rb', line 54

def namespace
  @namespace
end

#pool_nameObject (readonly)

The configured socket pool name for this client.



63
64
65
# File 'lib/memcache.rb', line 63

def pool_name
  @pool_name
end

#request_timeoutObject

Returns the value of attribute request_timeout.



49
50
51
# File 'lib/memcache.rb', line 49

def request_timeout
  @request_timeout
end

Instance Method Details

#add(key, value, expiry = 0, raw = false) ⇒ Object

Add a new value to the cache following the same conventions that are used in the set method.

Raises:



248
249
250
251
252
253
254
255
256
# File 'lib/memcache.rb', line 248

def add(key, value, expiry = 0, raw = false)
  raise MemCacheError, "Update of readonly cache" if @readonly
  value = marshal_value(value) unless raw
  if expiry == 0
    @client.add make_cache_key(key), value
  else
    @client.add make_cache_key(key), value, expiration(expiry)
  end
end

#alive?Boolean Also known as: active?

Determines whether any of the connections to the servers is alive. We are alive if it is the case.

Returns:

  • (Boolean)


170
171
172
# File 'lib/memcache.rb', line 170

def alive?
  servers.to_a.any? { |s| s.alive? }
end

#decr(key, amount = 1) ⇒ Object

Decrements the value associated with the key by a certain amount.

Raises:



293
294
295
296
297
298
# File 'lib/memcache.rb', line 293

def decr(key, amount = 1)
  raise MemCacheError, "Update of readonly cache" if @readonly
  value = @client.decr(make_cache_key(key),amount)
  return nil if value == "NOT_FOUND\r\n"
  return value.to_i
end

#delete(key, expires = 0) ⇒ Object

Removes the value associated with the key from the cache. This will ignore values that are not already present in the cache, which makes this safe to use without first checking for the existance of the key in the cache first.

Raises:



263
264
265
266
# File 'lib/memcache.rb', line 263

def delete(key, expires = 0)
  raise MemCacheError, "Update of readonly cache" if @readonly
  @client.delete(make_cache_key(key))
end

#flush_allObject

Clears the cache.



302
303
304
# File 'lib/memcache.rb', line 302

def flush_all
  @client.flush_all
end

#get(key, raw = false) ⇒ Object Also known as: []

Retrieves a value associated with the key from the cache. Retrieves the raw value if the raw parameter is set.



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/memcache.rb', line 179

def get(key, raw = false)
  value = @client.get(make_cache_key(key))
  return nil if value.nil?
  unless raw
    begin
      marshal_bytes = java.lang.String.new(value).getBytes(MARSHALLING_CHARSET)
      decoded = Base64.decode64(String.from_java_bytes(marshal_bytes))
      value = Marshal.load(decoded)
    rescue
      value = case value
        when /^\d+\.\d+$/ then value.to_f
        when /^\d+$/ then value.to_i
        else value
      end
    end
  end
  value
end

#get_multi(keys, raw = false) ⇒ Object

Retrieves the values associated with the keys parameter.



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/memcache.rb', line 202

def get_multi(keys, raw = false)
  keys = keys.map {|k| make_cache_key(k)}
  keys = keys.to_java :String
  values = {}
  values_j = @client.getMulti(keys)
  values_j.to_a.each {|kv|
    k,v = kv
    next if v.nil?
    unless raw
      begin
        marshal_bytes = java.lang.String.new(v).getBytes(MARSHALLING_CHARSET)
        decoded = Base64.decode64(String.from_java_bytes(marshal_bytes))
        v = Marshal.load(decoded)
      rescue
        v = case v
          when /^\d+\.\d+$/ then v.to_f
          when /^\d+$/ then v.to_i
          else v
        end
      end
    end
    values[k] = v
  }
  values
end

#incr(key, amount = 1) ⇒ Object

Increments the value associated with the key by a certain amount.

Raises:



284
285
286
287
288
289
# File 'lib/memcache.rb', line 284

def incr(key, amount = 1)
  raise MemCacheError, "Update of readonly cache" if @readonly
  value = @client.incr(make_cache_key(key), amount)
  return nil if value == "NOT_FOUND\r\n"
  return value.to_i
end

#replace(key, value, expiry = 0, raw = false) ⇒ Object

Replaces the value associated with a key in the cache if it already is stored. It will not add the value to the cache if it isn’t already present.

Raises:



272
273
274
275
276
277
278
279
280
# File 'lib/memcache.rb', line 272

def replace(key, value, expiry = 0, raw = false)
  raise MemCacheError, "Update of readonly cache" if @readonly
  value = marshal_value(value) unless raw
  if expiry == 0
    @client.replace make_cache_key(key), value
  else
    @client.replace make_cache_key(key), value
  end
end

#resetObject



147
148
149
150
# File 'lib/memcache.rb', line 147

def reset
  @pool.shut_down
  @pool.java_send :initialize rescue @pool.initialize__method
end

#serversObject

Returns the servers that the client has been configured to use. Injects an alive? method into the string so it works with the updated Rails MemCacheStore session store class.



156
157
158
159
160
161
162
163
164
165
# File 'lib/memcache.rb', line 156

def servers
  @pool.servers.to_a.collect do |s|
    s.instance_eval(<<-EOIE)
    def alive?
      #{!!stats[s]}
    end
    EOIE
    s
  end rescue []
end

#set(key, value, expiry = 0, raw = false) ⇒ Object Also known as: []=

Associates a value with a key in the cache. MemCached will expire the value if an expiration is provided. The raw parameter allows us to store a value without marshalling it first.

Raises:



232
233
234
235
236
237
238
239
240
241
# File 'lib/memcache.rb', line 232

def set(key, value, expiry = 0, raw = false)
  raise MemCacheError, "Update of readonly cache" if @readonly
  value = marshal_value(value) unless raw
  key = make_cache_key(key)
  if expiry == 0
    @client.set key, value
  else
    @client.set key, value, expiration(expiry)
  end
end

#statsObject

Reports statistics on the cache.



308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/memcache.rb', line 308

def stats
  stats_hash = {}
  @client.stats.each do |server, stats|
    stats_hash[server] = Hash.new
    stats.each do |key, value|
      unless key == 'version'
        value = value.to_f
        value = value.to_i if value == value.ceil
      end
      stats_hash[server][key] = value
    end
  end
  stats_hash
end