How to install memcache in laravel with example

How to install memcache in laravel with example

As we all know that caching is a technique to boost up the performance of a web application. So here, in this article i will demonstrate about the installation and use of memchache in a laravel web application with an example. There are some easy steps to install memcache in laravel. 

There are some other cache techniques are also available like radis, we will cover those in some other article. Here we will focus only on the memcache installation and their uses in laravel.

Actually Laravel itself provides several drivers to cache your application with some database queries, views etc. So without wasting our tiem let’s have a look to the steps to install memcached and uses of this cacheing technique.

Install Memcached

Here i will install memcached on ubuntu machine. For installing memcache on your machine you will need to run the following command on your ubuntu machine.

sudo apt-get install memcached

After that you will need to install memcached php extension to use in php. So run the following command.

sudo apt-get install php-memcached

The above command will install latest php-memcached version according to php version installed in you machine. You can install this extension with specific version like

sudo apt-get install php7.3-memcached
or
sudo apt-get install php7.4-memcached

Configure Memcached Driver

After installing the above things, we will need to configure memchaed driver. So just open your laravel application’s .env file and add the following line.

.env 

CACHE_DRIVER=memcached

Now open config/cache.php file and add the code written below.

config/cache.php

<?php
  
use Illuminate\Support\Str;
  
return [
  
    ....
  
    'stores' => [
  
        ....
  
        'memcached' => [
            'driver' => 'memcached',
             'persistent_id' => env('MEMCACHED_PERSISTENT_ID'),
             'sasl' => [
                 env('MEMCACHED_USERNAME'),
                 env('MEMCACHED_PASSWORD'),
             ],
             'options' => [
                 /* Memcached::OPT_CONNECT_TIMEOUT => 2000, */
             ],
            'servers' => [
                [
                    'host' => env('MEMCACHED_HOST', '127.0.0.1'),
                    'port' => env('MEMCACHED_PORT', 11211),
                    'weight' => 100,
                ],
            ],
        ],
    ]
  
    ....
]

Use of Memcached

For using the memcached in our laravel application, first thing which we will do is create a route to cache the data or records fetched from database. So now open your route file and add the following route.

routes/web.php

Route::get('cache-data', function () {
      
    $user = \Cache::remember('user', 60, function() {
            return \App\Models\User::first();
        });
       
});

Test Memcached Cache

Now after doing all the things mentioned above, we can test or check that the memchached is actually creating cache of records or not. You can run the following command in your terminal to get the cached data.

php -r '$c = new Memcached(); $c->addServer("localhost", 11211); var_dump( $c->getAllKeys() );'

It will show all the keys stored in the cache. If you find the your stored keys here means memcached working properly in your machine and laravel application. So now i think you have learned to install memcache in laravel application.