Install and Configure Memcached

Memcached is a lightweight alternative to Redis for storing short lived cache which would otherwise we written to the local storage folder as files. 

Installing Memcached on Linux is fast and easy. Follow these steps (5 minute job):

1.) As a user with root privileges, enter the following command:

sudo apt-get update;
sudo apt-get install memcached libmemcached-tools php-memcached;

2.) Once the installation is completed, the Memcached service will start automatically. To check the status of the service, enter the following command:

sudo systemctl status memcached

3.) Change the memcached configuration setting for CACHESIZE and -l:

Open /etc/memcached.conf in a text editor.

Locate the -m parameter and change its value to at least 2048 (2GB)

# memory
-m 2048

Locate the -l parameter and confirm its value is set to 127.0.0.1 or localhost

4.) Save your changes to memcached.conf and exit the text editor then restart memcached.

#restart memcached
sudo systemctl restart memcached

#confirm it is running
echo "stats settings" | nc localhost 11211

# check number of cached items
echo "stats items" | nc localhost 11211

5.) Note that on some systems memcached may not automatically start on bootup. In that case use this command to fix:

sudo systemctl enable memcached

6.) Add the php memchached extension

 sudo apt-get install php7.3-memcached;

Configure Laravel to Use Memcached

Laravel is wired to use memcached out-of-the-box. To enable memcached you simply have to add this line to the .env file:

CACHE_DRIVER=memcached

If you need to edit the ports used by memcaches, you can find those setting in config/cache.php.