Using Memcached with PHP and Drupal 8

Code With Travel
2 min readJan 18, 2020

--

Memcached is an in-memory key-value store for small chunks of arbitrary data (strings, objects) from results of database calls, API calls, or page rendering.

Memcached is simple yet powerful. Its simple design promotes quick deployment, ease of development, and solves many problems facing large data caches. Its API is available for most popular languages.

How to install Memcache on server

sudo apt-get update
sudo apt install memcached
sudo apt install php-memcached

Also, add the following line in php.ini to enable the Memcache extension: extension=php_memcache.dll

How to install Memcache on windows

The following are the download links for the Memcached windows binaries:

Extract files and put them into drive. Then run cmd as administrator.

D:/memcached/memcached.exe  -d install
D:/memcached/memcached.exe -d start
D:/memcached/memcached.exe -d runservice -m 512

Put extension=php_memcache.dll in php.ini and restart xampp/wamp.

Now, check into phpinfo() file.

Now make one PHP file and put the following code.

$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$tmp = new stdClass;
$tmp->str_attr = 'kshitij';
$tmp->int_attr = 741;
$memcache->set('key', $tmp_object, false, 100) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 100 seconds)<br/>\n";
$get_result = $memcache->get('key');
echo "Data from the cache:<br/>\n";
print_r($get_result);

Now Integrate Memcache in Drupal 8

Download https://www.drupal.org/project/memcache.Put following code into settings.php

$settings['memcache']['servers'] = ['127.0.0.1:11211' => 'default'];
$settings['memcache']['bins'] = ['default' => 'default'];
$settings['memcache']['key_prefix'] = '';

--

--

No responses yet