Introduction
Memcached is a general-purpose distributed memory-caching system. It is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read. Also it is a high performance free and opensource in-memory key-value store used as a caching system. It’s mainly used for speeding up database-driven sites and web applications by caching data in RAM. In so doing, it significantly reduces the frequency that an eternal source of data read.
Memcached was first developed by Brad Fitzpatrick for his website LiveJournal, on May 22, 2003. It was originally written in Perl, then later was rewritten in C by Anatoly Vorobey, then employed by LiveJournal. Memcached now used by many other systems
Memcached is simple and easy to deploy and its API is widely available for a wide range of popular programming languages such as Python.
Install Memcached
Memcached packages are already included in the Ubuntu repository, and as such, we are going to install Memcached using the APT package manager.
But first, update system packages :
$ sudo apt update
Thereafter, install Memcached by invoking the command:
$ sudo apt install memcached libmemcached-tools -y
The libmemcached-tools package is a C & C++ library that provides multiple command-line utilities that you can use for interacting and managing the Memcached server.
Memcached service will automatically start and you can verify this by running the command:
$ sudo systemctl status memcached
Memcached listens on port 11211 and you can verify this using the netstat command as shown:
$ sudo netstat -pnltu
Configure Memcached
To configure Memcached, you need to configure the /etc/memcached.conf  file. For the most part, the default settings will work just fine for a majority of users.
Without any configuration, Memcached listens on the localhost only. If you are connecting to the Memcached server from the server itself, no configuration needed.
To allow remote connections to the server, some additional configuration required. We need to modify the firewall to allow access to UDP port 11211 which Memcached listens to by default.
assume that the Memcached server IP address is 192.168.12.128 and the client’s IP address is 192.168.12.129. To allows the client machine access to the Memcached server, run the command.
$ sudo ufw allow from 192.168.12.129 to any port 11211
Reload the firewall for the changes to persist.
$ sudo ufw reload
head over to the memcached.conf
configuration file.
$ sudo vim /etc/memcached.conf
Be sure to locate the line that starts with -l 127.0.0.1Â
Replace it with the server’s IP, which in this case is 192.168.12.128 as shown:
Now, restart Memcached for the changes to come into effect.
$ sudo systemctl restart memcached
Enable Memcached for PHP and Python Applications
If you intend to use Memcached as a caching database for PHP applications such as Drupal or WordPress, the php-memcached extension required.
To install it, run the command:
$ sudo apt install php-memcached -y
For Python applications, install the following Python libraries using pip. If pip not installed, you can install it using the command:
$ sudo apt install python3-pip
Then install the libraries as shown.
$ pip3 install pymemcache
$ pip3 install python-memcached