Book Image

Getting Started with Memcached

By : Ahmed Soliman
Book Image

Getting Started with Memcached

By: Ahmed Soliman

Overview of this book

<p>Web application performance is no longer a non-functional requirement, but an implicit condition for an engaging user experience. As a result, responsive and highly scalable applications are becoming a necessity. Memcached is a high-performance distributed memory caching system built to speed up dynamic web applications by offloading pressure from your database. <br /><br />Getting Started with Memcached is a hands-on, comprehensive guide to the Memcached service and it’s API in different programming languages. It contains practical recipes to integrate Memcached within your Rails, Django, or even Scala Play! applications.<br /><br />This book will show you everything you need to know to start using Memcached in your existing or new web applications.<br />This book uses real-world recipes to help you learn how to store and retrieve data from your clustered virtual memory cache pool and how to integrate caching into your favourite web development framework.</p> <p><br />You will also learn how to build a Memcached consistent-hashing scalable cluster and how Memcached clients are properly configured to use different servers to scale out your memory cache pool in Ruby, Python, PHP, and Java. With this book, you will see how to cache templates and database queries in the most popular web development framework in use today.</p>
Table of Contents (9 chapters)

Using memcached with Ruby (Intermediate)


Ruby is also one of the most popular language used today by Web developers to build brilliant applications. The rise of the Rails framework was actually one of the main reasons this language received such popularity, however, Ruby is also a Swiss Army Knife language that is often used by system administrators for orchestration and automation.

There are of course, several clients to be memcached in Ruby but here we will be focusing on one of the most recent and stable clients that delivers high performance pure Ruby implementation of the memcached protocol, Dalli!

Dalli was written by the maintainer of memcache-client and is currently stable and being actively maintained.

The good thing about Dalli is that it can be integrated with Rails 3.x but, unfortunately, it does not integrate with the more popular Rails 2.x.

Getting ready

We need to install the Dalli gem using the following command:

gem install dalli

If you don't have the gem tool, then most likely you don't have Ruby properly installed. In Ubuntu you can always use the following to get Ruby installed:

sudo apt-get install ruby

How to do it...

  1. Let's start by doing a very basic set/get operation on the memcached server from Ruby:

    require 'dalli'
    dc = Dalli::Client.new('localhost:11211', :threadsafe => true, :compress => true)
    dc.set('somekey', 123)
    puts("the value from cache is: #{dc.get('somekey')}")
  2. This looks great; now let's store more complex structures in the memcached server:

    require 'dalli'
    dc = Dalli::Client.new('localhost:11211', :threadsafe => true, :compress => true)
    user = {:name => "Ahmed",
              :job => "Engineer"}
    dc.set('user1', user, ttl=20)
    puts("user from cache: #{dc.get('user1')}")
  3. Now, let's use a new feature which we did not use before (replace)

    require 'dalli'
    dc = Dalli::Client.new('localhost:11211', :threadsafe => true, :compress => true)
    user = {:name => "Ahmed",
    		:job => "Engineer"}
    dc.set('user1', user, ttl=20)
    puts("user from cache: #{dc.get('user1')}")
    
    user[:age] = 31
    dc.replace("user1", user, ttl=5)
    puts("user from cache: #{dc.get('user1')}")

How it works...

First, we are importing Dalli into our namespace by using the require statement. Then, we are creating a client that connects to the memcached server and we are also setting some options. The following are some of the interesting options:

  • :compress => true: This will ask Dalli to compress values larger than 1024 bytes.

  • :threadsafe => true: This ensures that only a single thread is actively using the connection socket at a time; this is actually enabled by default, we added this to the snippet for clarity only.

  • :namespace => "app": This adds a prefix to all keys set in this connection.

  • :expires_in => 100: This sets the default TTL(timeout) for all the keys where you are not specifying a TTL.

Then, we used the simple set method to set a basic integer value, after we retrieved that value and printed to the console using puts.

In the second snippet, we created a standard Ruby Hash and we used the built-in serializer to store this hash as a value for the "user1" key.

Note

This time we added a TTL parameter to specify that this key will expire after 20 seconds.

In the third snippet, we introduced a new feature of memcached, that is replace. This was used to replace the entire hash stored for the "user1" key with a modified version (we added age to it).

While we were replacing the value we also respecified the TTL value and changed the TTL of the value to be 5 seconds only. The replace feature fails if the key is not already stored in the memcached server and that's the main difference between it and the method that you are already familiar with set.

There's more...

The constructor of the Client class can also accept a list of servers to specify, in case you have a memcached cluster as you can see, it's a list of servers.

Dalli::Client.new(['localhost:11211:10', 'cache-2.example.com:11211:5', '192.168.0.1:22122:5'], :threadsafe => true, :failover => true, :expires_in => 300)

For every server, the format is server:port:weight, where weight allows you to distribute cache unevenly. Both weight and port are optional. If you pass in nil, Dalli will use the MEMCACHE_SERVERS environment variable or default to localhost:11211 if it is not present.