























1 Share

Installing memcached is very easy and it is very helpful for the websites that handles large amount of data and large amount of users with huge traffic. Since, It is a memory based cache it is very fast and effective. Many CMS like wordpress and Drupal have Plugins/Modules to work with memcached. In my last article I have already told you “How to install memcached on Ubuntu”. In this article, I will tell you, How to use it with PHP 7.

Since PHP 7 is already all time optimized and high performance version than its previous one. Using memcached with PHP 7 will boost the performance many folds.

See, How PHP 7 performance is truly amazing when we compare it with previous version i.e., PHP 5.6.

#1 Prerequisite

To install php-memcached plugin, it is important to have some prerequisites

PHP 7 (Since we are using php 7 here)

Memcached: Memcached should be already installed on the server to work with php-memcached.

#2 Installation

As a best practice first update the apt-get package using :

$ sudo apt-get update

Install PHP plugin for memcached

$ sudo apt-get install php-memcached

#3 Verification

Create a phpinfo file to check whether php memcached plugin is working or not?

Open the browser and type ‘localhost/<info file name>.php’ to verify that php-memcached plugin is working .

The above screen shot shows that php-memcached plugin is ready to use.

#4 Use with PHP 7

Create a php file e.g., test.php and use the code below

Source code <?php $cacheObj = new Memcached ( ) ; $cacheObj -> addServer ( "127.0.0.1" , 11211 ) ; $result = $cacheObj -> get ( "cachekey" ) ; if ( $result ) { echo $result ; } else { echo "No matching key found yet." ; $cacheObj -> set ( "cachekey" , "cached data is available" ) ; } ?>

Open the browser and type ‘localhost/test.php’. It gives the message of not having any matching key found

Reload the cache. Now it has the data in cache and it shows “cached data is available”;

If you want to learn PHP, buy a great deal on it Globally and in India

#5 Flush the cache

Since memcached uses server RAM to cache the data, removing cache is too easy

$ echo ‘flush_all’ | netcat localhost <memcached port no>

Please write me up if you face any issue with the above solution. Cheers!!!

Share this: Tweet





Like this: Like Loading...