A Ruby interface to the libmemcached C client
Перейти к файлу
Jean byroot Boussier f19f132965
Update the README about exceptions (#196)
Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
2021-04-14 14:49:13 -04:00
.github/workflows CI: Test gem installation 2021-04-06 18:48:06 -07:00
ext/memcached Verify compaction references (#193) 2021-04-07 14:22:37 -04:00
lib Allow to configure the codec (#195) 2021-04-08 13:41:08 -04:00
test Update the README about exceptions (#196) 2021-04-14 14:49:13 -04:00
test-containers move Dockerfile to a folder 2016-03-21 16:16:41 -04:00
vendor/libmemcached-1.0.18 remove osx patch, and just apply it 2016-03-17 10:47:33 -04:00
.gitignore Update gitignore with libmemcached compiled files 2016-03-15 23:23:02 -04:00
BENCHMARKS latest benchmarks 2016-02-10 09:39:32 -05:00
CHANGELOG Add Changelog 2016-02-11 22:04:38 -05:00
Gemfile Use minitest instead of test-unit 2016-03-04 17:41:24 -05:00
Gemfile.lock Regenerate Gemfile.lock as done in CI 2021-03-31 16:27:53 -07:00
LICENSE license 2008-01-20 09:16:17 +00:00
README.md Update the README about exceptions (#196) 2021-04-14 14:49:13 -04:00
Rakefile ext: rename the native extension to `memcached` 2016-03-16 15:05:58 +01:00
TODO.org Update TODO.org 2016-07-05 14:00:43 -04:00
memcached.gemspec Fix gem install portability by avoiding a shell feature dependency 2021-04-06 18:48:04 -07:00
memcached.pem Fix #143 2013-11-07 17:53:29 -08:00

README.md

memcached

An interface to the libmemcached C client. Build Status

License

Copyright 2009-2013 Cloudburst, LLC. Licensed under the AFL 3. See the included LICENSE file. Portions copyright 2007-2009 TangentOrg, Brian Aker, licensed under the BSD license, and used with permission.

Features

  • clean API
  • robust access to all memcached features
  • SASL support for the binary protocol
  • multiple hashing modes, including consistent hashing
  • ludicrous speed, including optional pipelined IO with no_reply

The memcached library wraps the pure-C libmemcached client via SWIG.

Installation

You need Ruby 1.8.7 or Ruby 1.9.2. Other versions may work, but are not guaranteed. You also need the libsasl2-dev and gettext libraries, which should be provided through your system's package manager.

Install the gem: sudo gem install memcached --no-rdoc --no-ri

Usage

Start a local networked memcached server: $ memcached -p 11211 &

Now, in Ruby, require the library and instantiate a Memcached object at a global level:

require 'memcached'
$cache = Memcached::Client.new("localhost:11211")

Now you can set things and get things:

value = 'hello'
$cache.set 'test', value
$cache.get 'test' #=> "hello"

You can set with an expiration timeout:

value = 'hello'
$cache.set 'test', value, 1
sleep(2)
$cache.get 'test' #=> nil

You can get multiple values at once:

value = 'hello'
$cache.set 'test', value
$cache.set 'test2', value
$cache.get ['test', 'test2', 'missing']
  #=> {"test" => "hello", "test2" => "hello"}

You can set a counter and increment it. Note that you must initialize it with an integer, encoded as an unmarshalled ASCII string:

start = 1
$cache.set 'counter', start.to_s, 0, false
$cache.increment 'counter' #=> 2
$cache.increment 'counter' #=> 3
$cache.get('counter', false).to_i #=> 3

You can get some server stats:

$cache.stats #=> {..., :bytes_written=>[62], :version=>["1.2.4"] ...}

Rails 3 and 4

Use memcached_store gem to integrate ActiveSupport cache store and memcached gem

Pipelining

Pipelining updates is extremely effective in memcached, leading to more than 25x write throughput than the default settings. Use the following options to enable it:

:no_block => true,
:buffer_requests => true,
:noreply => true,
:binary_protocol => false

Currently #append, #prepend, #set, and #delete are pipelined. Note that when you perform a read, all pending writes are flushed to the servers.

Threading

memcached is threadsafe, but each thread requires its own Memcached instance. Create a global Memcached, and then call Memcached#clone each time you spawn a thread.

thread = Thread.new do
  cache = $cache.clone
  # Perform operations on cache, not $cache
  cache.set 'example', 1
  cache.get 'example'
end

# Join the thread so that exceptions don't get lost
thread.join

Legacy applications

There is a compatibility wrapper for legacy applications called Memcached::Rails.

Benchmarks

memcached, correctly configured, is at least twice as fast as memcache-client and dalli. See link:BENCHMARKS for details.

Reporting problems

The support forum is here.

Patches and contributions are very welcome. Please note that contributors are required to assign copyright for their additions to Cloudburst, LLC.

Further resources