How to Install and Use Memcached in Laravel?

https://www.itsolutionstuff.com/post/how-to-install-and-use-memcached-in-laravelexample.html

How to Install and Use Memcached in Laravel?

By Hardik Savani May 20, 2021 Category : LaravelPlayUnmuteLoaded: 1.17%FullscreenVDO.AIHi Artisan,

In this tute, we will discuss how to use memcached in laravel. i would like to share with you laravel cache memcached. it's simple example of laravel cache driver memcached. We will look at example of laravel use memcached. Follow bellow tutorial step of install memcached php laravel.

Laravel provide several driver for cache your app with database query, view etc. here i will give you step by step instruction how to install and use memcached as cache driver in laravel application. you can easily use memcached with laravel 6, laravel 7, laravel 8 and laravel 9 version.

let's see step by step configuration memcached driver.

Install Memcached in Server:

in this step we need to install memcached in ubuntu server and php extension for it. so let's run both command:

sudo apt-get install memcached

next you need to install php extension for memcached. it's version specify so it will install with your php version like "sudo apt-get install php7.3-memcached", sudo apt-get install php7.4-memcached etc. but for default you can use as like bellow command:

sudo apt-get install php-memcached

Configure Memcached Driver:

now, we need to configure cache driver as memcached on env file as like bellow:

.env

CACHE_DRIVER=memcached

you can see bellow cache config file it will as bellow:

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 Memcached Driver:

now, we will create simple example route to cache our database records. let's see bellow code:

Route:

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

Check Memcached Cache:

in this step, we will check memcached driver cache properly or not. you need to run bellow command and get all keys of cached:

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

now you can use key and get that content as like bellow:

Read Also: Laravel Eloquent When Condition Example

telnet 127.0.0.1 11211  get laravel_cache:user

now it will works great.

i hope it can help you...

Last updated