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.

Memcache commands

Programming December 8th, 2010

The following are commands that you can issue to memcache while connected to it using telent.

Command Description Example
get Reads a value get keyname
set Set a key unconditionally set keyname 0 120 4
add Add a new key add keyname 0 120 4
replace Overwrite existing key replace key 0 120 4
append Append data to existing key append key 0 120 30
prepend Prepend data to existing key prepend key 0 120 30
incr Increments numerical key value by given number incr keyname 2
decr Decrements numerical key value by given number decr keyname 5
delete Deletes an existing key delete keyname
flush_all Invalidate specific items immediately flush_all
Invalidate all items in n seconds flush_all 900
stats Prints general statistics stats
Prints memory statistics stats slabs
Prints memory statistics stats malloc
Print higher level allocation statistics stats items
stats detail
stats sizes
Resets statistics stats reset
version Prints server version. version
verbosity Increases log level verbosity
quit Terminate telnet session quit

Database design tutorial : UML, SQL & Normalization

Programming January 7th, 2010

Numerous websites online offer tutorials on database designing. Some specifically detail php+Mysql usage. While there are numerous aspects of a database driven website, the two most important aspects are design and implementation. While most websites teach you how to write effective sql queries, not many teach you the art of design.

Tom Jewett form Department of Computer Engineering and Computer California State University, Long Beach has a gem of a website that talks about database design.

It it one of the best references available if you are looking to learn the fundamentals of database design or just refresh your sql skills.

Database design tutorial

Site: http://www.tomjewett.com/

DB Design, UML,SQL, 3rd Edition.