MemCached Basic

Kiranghimire
4 min readMay 9, 2020

--

Memcached is a distributed memory object caching system that accelerates that data fetching from database.

The main reason for using this open source software is to cut down the load that can be faced during accessing the data from the database by temporarily storing in memcached server. This enhance the performance of the web applications.It stores the data in key-value pair in memory and due to inconstancy memcached rather seems NoSQl database.

Working of it:

Simply first if the users/client-server want to retrieve data from web applications it seeks/checks to the memcached server.

The requested data will be forwaded to user/client-server if that is alreadyin memcached server otherwise the data will be retrieved from database and ensuing it in memcached.

Let’s install it

For Debian or Ubuntu: sudo apt-get install memcached

For Redhat/Fedora: yum install memcached

For Windows: I recommend this website for installation process.

https://commaster.net/posts/installing-memcached-windows/

For Mac: brew install memcached

Why not try it?

This process is for Debian/Ubuntu users.

To access it first you have to connect to the localhost using telnet protocal.

“Telnet is a simple, text-based network protocol that is used for accessing remote computers over TCP/IP networks like the Internet.”

Command:

ps -ef | grep -i memc

“ps” finds the information about the running processes.

In above, you can see that the memcached is running on port and localhost (127.0.0.1). “-m 64” indicates 64megabytes of memory is allocated in memcached.

Now connect it through telnet.

Command:

telnet localhost 11211

If connection fails type below Command.

/etc/init.d/memcached restart

This simply restart the memcached.

Commands on memcached:

1.stats

This gives bevy information.

Some like:

cmd_get 0 > indicates how many time we hit get Command

cmd_set 0 > indicates how many time we hit set Command

total_items 0 > depicts the total items

We will again see this after checking some commands.

2.Let set the key and value:

ex:

set KEY <flag> <expiration-time> <bytes>

Here we have set the key foo and 0 3600 3 are the parameters.

What does mean them?

‘0’=setting flag

3600>>expiration time in second which may not exceed more than 2592000s(30 days) if then memcached uses UNIX timestamp. If it is set 0 the item will never expire.

‘3’=bytes

After setting key ‘wow’ value is given.

We can retrieve stored data from memcached using ‘get foo’ command.

3.Command:

replace <key-name> <flag> <expiration-time> <bytes>

To suppress the existing key in memcached server replace command should use.

4.delete <key-name> <flag> <expiration-time> <bytes>

This delete the key-value.

5.flush_all

To delete all key-value data pairs(existing cache items) ‘flush_all’ command should use.

Now check the stats .We have hit get command 6 times ,get misses 2 times ,set command 7 times, flush_all command 1 time etc.

Hope this below figure will help you to understand even better.

credit: https://lzone.de/cheat-sheet/memcached

Now, let’s dive into another part.Even without connecting through telnet we can access memcached server.

For this package ‘libmemcached-tools’ should be installed.

sudo apt-get install libmemcached-tools

libmemcached is a C and C++ client library provided to the memcached server which providesfull access to server side methods.

Commandlines: These are just the alternative way of doing.

memstat — -it shows statistics of the server

memcdump — -it shows what we have stored

memccat — -it shows the value of a key

memcrm — -to remove key-value

memccp — -copy files in memcached servers

memcflush — -flush all key-value data pairs

Check the stats.

There are memcached libraries in python ,node.js, php etc so that memcached can even used in python ,node.js, php.

I have listed the sites for tutorials you can see them for further knowledge and ideas.

For python:-

https://pymemcache.readthedocs.io/en/latest/getting_started.html

For node.js:-

https://redislabs.com/lp/node-js-memcached/

For php:-

https://dzone.com/articles/how-use-memcache-php

--

--

No responses yet