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,
- How to create Queue and Run Jobs using worker in Lumen/Laravel
- Laravel Micro Rest Framework – Simple Example of RESTful API in Lumen
- How to Configure Memcached in Lumen Api Framework
- Simple Example of Laravel 5 Login System Using Sentry
- Authorization and Authentication of Users in laravel 5 Using Sentry
- Simple Laravel Layouts using Blade Template and Bootstrap Theme
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.
'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.
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