Php

How to Configure Memcached in Lumen Api Framework

Lumen is very popular,fast and light weight api micro-framework by Laravel using php. I have created many project on lumen framework.This tutorial help to configuration of memcached with lumen 5.1.* version.

There is no clear step by step docs available about configuration of memcached and uses in Lumen rest framework.I will share step by step lumen tutorial for memcached configuration with centOS 7,I am using below configuration of Lumen host server.

  • centOS 7
  • php 5.7+
  • Nginx
  • Lumen 5.1+

Please keep in mind do not try with windows server there is no memcached extension available for xampp/wamp server.You can get memcache.so but not memcached.so(‘d’) php extension.

If you will use with windows, you will get memcached is not available at …line 38.I wastage much time to find windows ext but unfortunately did not work for me.We will follow below steps to add Memcached with Lumen,

  • Enable memcache in .env file
  • Change configuration option in cache config file
  • Install Memcached server
  • Install Memcached ext for php
  • Start Memcached server service
  • Set and Get method to get data from Memcached cache Object

You can also check other recommended tutorials of Lumen/Laravel,

Lets start configuration of memcached with lumen,

Step 1: We need to update .env file of Lumen project and added Cache driver Memcached.

CACHE_DRIVER=memcached

Step 2: Copy cache.php file from lumen_project\vendor\vendor\laravel\lumen-framework\config\ and put into lumen_project\config\ folder,If config folder is not present in Lumen root create and put cache.php file.

Step 3: Now change value of default cache driver from array to memcached into lumen_project\config\cache.php file.

Related Post
 'default' => env('CACHE_DRIVER', 'memcached'),

Step 4: Now we will install memecahed server on Centos and php extention.

yum -y install memcached

yum install php-pecl-memcached

Step 5: Now we will restart ngnix,fpm to update php.ini configuration and memcached server using below Linux command.


service php-fpm restart

service memcached restart

service nginx restart

You can see memcached extension enabled in phpinfo file.If you are not getting memcached extension in phpinfo file then some thing wrong.

How to use Memcached in Lumen

Step 1: Set variable in memcached using set() function.

public function setMemcache() {
    Cache::put('name', 'parvez', 15);
}

I have set 15 min for this cache object time limit after that object will flush from Memcached.

Step 2: Set variable in Memcached using get() function.

/**
  * Method to getter method for memcached
  *
  * @author parvez.alam
  */   public static function getMemcache() {
     echo Cache::get('name');
   }

if not key available in Memcached then it will return false;

That’s all for Memcached configuration and uses with Lumen,I have shared this tutorial for all Lumen api framework and Memcached lover, you can use with other php api framework as well but need to change set() and get() method of object as per your php api framework docs.

View Comments

  • i follow above steps but still generating Error.
    Class 'Memcached' not found so what i do and where i change it.

    • i think memcached not installed correctly or service not started, if u are on nginx, please restart php-fpm

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

3 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

9 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

9 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

10 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

10 months ago

Categories