Sitemap
CodeX

Everything connected with Tech & Code. Follow to join our 1M+ monthly readers

How to interact with Memcached using Telnet, Netcat, Python, or Django

5 min readNov 20, 2021

--

It’s easier than you think

Press enter or click to view image in full size
Photo by Joshua Sortino on Unsplash

Background

Caching is a technique to boost the performance of your program or website. The main idea is to store the result of an expensive operation, query, or computation to a location, so that similar call/request in the future will not need to redo it again, and it will just return the stored value.

Files or database is poor caching store due to slow I/O operation, and leveraging RAM instead is much faster. Memcached is very popular and used for scaling huge projects like Facebook, YouTube, Twitter, Wikipedia, etc. Once your website has significant traffic, Memcached is one of the effective ways to improve performance.

Memcached (Memory Cache Daemon) is a memory cache server. Telnet, Netcat, Python, and Django could then act as clients. Memcached store the data in RAM as key-value pairs, so it could be viewed as a huge but fast Python dictionary.

Press enter or click to view image in full size

Memcached Server

For installation, see https://github.com/memcached/memcached/wiki/Install.

For Mac users, it’s easy to install via Homebrew:

brew install memcached

and to run it:

brew services start memcached

To sanity check if it’s running (for Unix-like systems):

ps aux | grep memcached

Telnet Client

telnet (teletype network) is an old tech, created in the ’60s since computers that time were very expensive and huge to be used at home. So, this is a mechanism to connect to the remote computer. ssh (secure shell) is the successor of telnet, mainly to increase security. With telnet you could communicate with a server using the telnet protocol. You could even play chess in your terminal, try it:

telnet www.freechess.org

There are various ways to communicate with memcached, via telnet is one of them. Both the host and port are required, and 11211 is the default port for memcached:

telnet localhost 11211

which will display the telnet prompt:

We could then start typing the memcached commands like set, get, delete, or flush_all:

# Caches foo with the value of bar for 5 minutes (300 seconds).
# Default timeout is 0 (i.e. no expiration).
# Syntax: set KEY FLAGS TIMEOUT SIZE
# FLAGS usually is just 0.
# SIZE is the number of characters.
# the bar value is inputted in the next line.
set foo 0 300 3
bar
# Returns bar.
get foo
# Deletes foo.
delete foo
# Deletes all keys.
flush_all

Here is a sample run:

For more details and other commands, see https://github.com/memcached/memcached/wiki/Commands

Netcat Client

nc (netcat) is a Unix CLI tool mainly used for network inspection or debugging. And it could communicate with the memcached server via the TCP protocol. We could pipe the memcached commands into it using the echo statement.

So, to quick check the value of a cache key, it’s more convenient to use nc instead of telnet:

and to flush all keys, which is handy alternative to restarting the memcached or using telnet:

Here are the four memcached commands using nc :

Press enter or click to view image in full size

Likewise, it’s more efficient to create shell functions for them (put these in .bashrc or .zshrc:

Press enter or click to view image in full size

So, now we could simply run them as cache_set, cache_get, cache_delete, and cache_clear:

Python Client

There are many memcached clients implemented in Python. They have similar APIs though (set(), get(), delete(), flush_all()). One of the popular ones is the python-memcached. To install:

pip install python

It’s easy to use:

Django Client

Django could use various cache backends: files, database, memory, etc. The Cache API is a consistent interface, hence your code will not change when you decide to switch your cache backend. We just need to define the CACHES Django setting. The default cache backend is the Local Memory Cache (django.core.cache.backends.locmem.LocMemCache) which will leverage also the RAM. So, for memcached, we use the MemcachedCache class:

Press enter or click to view image in full size

We could then invoke the Cache API methods. The codes here are very similar to the Python codes above. However, note that Django uses clear() instead of flush_all():

To check that it’s indeed stored in memcached, we need to use the get :1:foo format, instead of simply get foo because Django uses a cache key function to generate this string pattern:

PREFIX:VERSION:KEY

where the default values are:

PREFIX = ''
VERSION = 1

Hence, we ended up with the :1:foo resultant key. We could verify this using the nc:

or using the shell function we created for it:

Key Takeaways

  • Memcached is powerful, easy to setup, and easy to learn.
  • The commands are relatively consistent across the various tools:
Press enter or click to view image in full size
  • For netcat (nc), it’s better to use the cache_set, cache_get, cache_delete, and cache_clear shell functions we implemented, especially the cache_get and cache_clear which are really convenient.
  • We only used the 'foo': 'bar' as the sample key-value pair in the article to achieve brevity. But the principles will still be the same, even when using more complex objects/structures.

Thank you for reading. If you found some value, kindly follow me, or give a clap/comment on the article, or buy me a coffee. This will mean a lot to me, and encourage me to create more high-quality contents.

--

--

Ranel Padon
Ranel Padon

Written by Ranel Padon

CodeX
CodeX

Published in CodeX