Working with memcached

memcached is a general-purpose distributed memory caching system used to speed up dynamic database driven websites by caching data in RAM to reduce the number of times an external data source to be read.

Here is how memcached can be useful.The system uses a client server architecture. Server maintains associative array in key value pair. the clients populate this array and query it. Keys are up to 250 bytes long and values can be at most 1 MB size. by default server runs on 11211 port. A client should be installed in order to access memcached data. client can connect to multiple server by adding new server to it's pool. gettting data from memcached there are several methods to use memcached. yo can go to official site for more information on memcached.

Using memcached from a script. [I've used PHP in example.]

1. Memcached class should exist if memcached client is installed properly. Create an object of memcached.

                  $obj = new Memcached();
    if object created means memcached service is running. If not then you can start the service from command prompt.using command  svc -u path/to/memcached/ [eg:  /usr/local/memcache/bin/memcached ]

2. Add the server to the pool.
                $obj->addServers($servers); [$servers = array( '172.18.1.69', 11211) where 1st param. is IP of the server and second is port.]

3 Now you can use different methods to add/fetch memcached data. [example get($key),set($key,$value)]. for more information visit php.net site and search for memcached based function.


Checking memcache log.

1. Go to memcashed directory  /service/memcached

2.write the following command to get log
tail -f log/main/current | tai64nlocal


Using telnet to connect to memcache server.

1. telnet server_IP port [telnet 127.0.01 11214]

Once connected, you can check if memcache key is set, by typing  get memcache_key_name to get the memcached data for the key memcache_key_name and press enter. You can also set and delete key using set and delete.

Well that's all folks. We'll see membase in the next session.