Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Memcached: In-memory Caching System

DZone's Guide to

Memcached: In-memory Caching System

· Performance Zone
Free Resource
Originally developed for LiveJournal, Memcached is a distributed memory caching system that is now used on many popular sites. Memcached provides performance boosts to database-driven sites by caching data into RAM to reduce reads to external data.

Architecture


Who uses Memcache?


  • YouTube
  • Reddit
  • Zynga
  • Facebook
  • Orange
  • Twitter
  • Wikipedia
  • Heroku offers a managed Memcached service built on Couchbase Server as part of their platform as a service.
  • Google App Engine, AppScale, Windows Azure and Amazon Web Services also offer a Memcached service through an API

Used for?


Pros

1) Reduces database load.
2) Perfect for websites with high database load.
3) Significantly reduces the number of retrieval requests to database.
4) Cuts down the I/O access (hard disk)

Cons

1) Not a persistent data store
2) Not a database
3) Not application specific
4) Cannot cache large object

Configuring Memcached (For Ubuntu)

Step 1) Install Libevent
Memcached uses the Libevent library for network IO.
cd libevent-1.4.11-stable$ autoconf

./configure --prefix=/usr/local

make

sudo make install
Step 2) Install Memcached
Download the latest version of Memcached from Danga.com, which developed Memcached originally for LiveJournal.
cd memcached-1.4.0

autoconf

./configure --prefix=/usr/local

make

sudo make install
Step 3) Run Memcached

Start Memcached as a daemon with 512 MB of memory on port 11211 (default).
Then you can telnet to the server, port and use any of the available commands.
memcached -d -m 512 127.0.0.1 -p 11211

telnet localhost 11211Trying ::1...
Connected to localhost.
Escape character is '^]'.
get joe
END

set joe 0 3600 10  (Note: TTL 3600 and 10 bytes)California
STORED

get joe
VALUE joe 0 10
California
END
Step 4) Spy Memcached (Memcached Java Client)

 public class Users implements Serializable{
  private int id;
private String name ;
  // Getter and Setter methods
}

public class MemcachedDemo {
    public static void main(String[] args) {
        Users users = new Users();
        users.setId(1);
        users.setName("Sunil123");
        try {
            MemcachedClient c=new MemcachedClient(new InetSocketAddress("127.0.0.1", 1121));
            c.set("someKey_1", 2592000, users);
            Object myObject=c.get("someKey_1");
            System.out.println("Object 1: " + myObject);
            System.out.println("Statistics: " + c.getStats());
            System.out.println("Statistics of individual Items: " + c.getStats("items"));
            c.delete("someKey_2");
            c.shutdown();
            c=null ;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}