This article explains why memcached is used, how it is installed and how it is used from Python.

The following figure shows an application that gets its data from a (slow) database. Here the problem arises when the database is queried and, depending on the number of queries, the query may at some point come back empty. The other issue is that if you have to fetch the same data several times; in both cases memcached becomes necessary.

without memcached

The following figure shows the addition of memcached, used together with the database, to "cache" the database's data.

with memcached

This article is based on an article about memcached from realpython.

Installing memcached

For Debian and Debian derivatives, apt is used to install memcached:

apt-get install memcached

For Redhat, Fedora, Centos:

yum install memcached

Install the library for Python; in this case pip is used:

pip install pymemcache  

Run memcached:

memcached

The code

The example code for using pymemcache is shown below:


#!/usr/bin/env python3

#Se importa el cliente de memcached
from pymemcache.client import base


#Se conecta al servidor memcached
client = base.Client(('localhost', 11211))

#Se almacena en memcached una variable y un valor
client.set('variable', 'valor')

#Se almacena otro valor
client.set("entero",1)

#Se almacena otro
client.set("booleano",True)
variable = str(client.get('variable'))
entero = int(client.get('entero'))
booleano = bool(client.get('booleano'))

#se ve el tipo de las variables
print(type(client.get('variable')))
print(type(client.get('entero')))
print(type(client.get('booleano')))


#Se muestra el resultado tomando los datos de memcached
print(f"variable: {type(variable)}, {variable}")
print(f"entero: {type(entero)}, {entero}")
print(f"booleano: {type(booleano)},{booleano}")

Result

The output of the script (mem1.py) is shown below:

<class 'bytes'>
<class 'bytes'>
<class 'bytes'>
variable: <class 'str'>, b'valor'
entero: <class 'int'>, 1
booleano: <class 'bool'>,True

As the output shows, the variables stored in memcached are all of type bytes when their values are retrieved, so they have to be converted before they can be manipulated in the code.

Make a donation! If you liked the article you can make a donation with Bitcoin (BTC) using the digital wallet of your choice at the following address: 17MtNybhdkA9GV3UNS6BTwPcuhjXoPrSzV

Or by scanning the QR code from your wallet:

17MtNybhdkA9GV3UNS6BTwPcuhjXoPrSzV