Memcache installation : understanding memcache
Programming December 8th, 2010
In this article we will see a real short python memcache installation procedure on a ubuntu server. We will also look as understanding the functionality that memcache offers us by interacting with it.
Short Memcache Tutorial
Dynamic websites are those in which web pages are generated on the fly using some mainstream scripting
languages. Unlike static html websites that do not change, dynamic websites are characterized by their ever changing “dynamic” content. Almost without doubt a dynamic website must use some kind of data storage to update its content over time. One of the simple cases is a sql server. When a high traffic dynamic website requires to load a popular page a million times, it is wasteful of resources to run similar data acquisition queries on the sql server. To help such situations, a caching mechanism can act as a blocking layer that can store information regarding a page without having to re-fetch it from the data store of the mysql server. Memcache server is one such caching application that help your applications store / cache some information. Memcache is an in memory application which means that it stores the cached content in RAM. You will be able to interface with memcache using telnet but not quite practical for applications written in various languages to interact in that manner. Hence, most languages provide a wrapper that can act as in interface between your programming language and the memcache server.
Memcache has a simple interface which is basically a key value store. Let us consider a single web page as an item and the page url as a unique identifier to the page. Memcache can be easily imagined as a dictionary where the index is the url and the value is the content (html) of the page. Off course it need not work in the way that I just mentioned, but a way to think about it. Usually the keys are hashed to minimize conflicts.
Towards the end of this article we will see a way to interface python and memecache server.
Memcache installation ubuntu:
On an ubuntu box, memcache installation is easy.
sudo apt-get install libmemcache-dev libmemcache0
That is it ! Check if memcached is running by using
ps aux | grep memcached
To start memcached server, just go ahead and use the command (Note:- if you did not manually start memcache, find the memcache start command details using “ps aux | grep memcache”) :
memcached -d -m 64 -p 11211 -u memcache -l 127.0.0.1
Options:
-d runs the server as a daemon
-m specifies the memory that memcache is allowed to use
-p port to which memcache listens to
-u user
-l hostname that would like to bind to (127.0.0.1 prevent access from outside your server)
What is happening now:
After you have started memcache, you can interact with it in some nice different ways. Let us first interact with it through telnet. We gave the port number while startup so lets telnet to port 11211 as below
# telnet 127.0.0.1 11211
You should expect the following result :
Trying 127.0.0.1…
Connected to 127.0.0.1.
Escape character is ‘^]’.
|
This essentially has setup an access to memcache via port 11211 which the memcache server is listening to. Type in your commands and hit enter to get a response form memcache. Find a list of valid memcache commands to interact with it.
How to store value in memcache
If you had followed this far, you are currently at :
Trying 127.0.0.1…
Connected to 127.0.0.1.
Escape character is ‘^]’.
Type the set command which has the following syntax:
set <keyname> <flag> <time until expiry of data> <bytes of data you will store>
Trying 127.0.0.1…
Connected to 127.0.0.1.
Escape character is ‘^]’.
set testkey 0 120 4 (press enter and in the next line provide the 4 byte data)
data
STORED (Response from memcache. You just stored “data” to key “testkey”)
How to get value from memcache
Assuming that you had already stored a value as mentioned above, access the stored data using the get command which has the following syntax:
get <keyname>
The following is the result of the issued command:
Trying 127.0.0.1…
Connected to 127.0.0.1.
Escape character is ‘^]’.
VALUE testkey 0 4
data (You have got back the value that you had stored !!)
END
Make sure you do the set and the get within a span of 120 second as you have asked memcache to store the key value pair just for 120 seconds in the set command. Increase that to a really long time as desired. We have come far in understanding the functioning of memcache server and I hope it helped.

