Method: MemCache#add

Defined in:
lib/gems/activesupport-2.2.2/lib/active_support/vendor/memcache-client-1.5.1/memcache.rb

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

Add key to the cache with value value that expires in expiry seconds, but only if key does not already exist in the cache. If raw is true, value will not be Marshalled.

Readers should call this method in the event of a cache miss, not MemCache#set or MemCache#[]=.

Raises:



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/gems/activesupport-2.2.2/lib/active_support/vendor/memcache-client-1.5.1/memcache.rb', line 352

def add(key, value, expiry = 0, raw = false)
  raise MemCacheError, "Update of readonly cache" if @readonly
  server, cache_key = request_setup key
  socket = server.socket

  value = Marshal.dump value unless raw
  command = "add #{cache_key} 0 #{expiry} #{value.size}\r\n#{value}\r\n"

  begin
    @mutex.lock if @multithread
    socket.write command
    result = socket.gets
    raise_on_error_response! result
    result
  rescue SocketError, SystemCallError, IOError => err
    server.close
    raise MemCacheError, err.message
  ensure
    @mutex.unlock if @multithread
  end
end