How to Use Memcached With Yii 2

by Ahmed Khan  August 16, 2016

In the previous article on caching technologies, I discussed how to integrate Redis with Yii2 as a cache and session handler. In this blog, I will demonstrate how to use Memcached with Yii2.

Memcached is a free and open source distributed memory caching system. It is mainly used to speed up dynamic database-driven websites by caching data to reduce the execution and access times. Cloudways comes pre-installed and activated Memcached on users’ servers and I will use it in this tutorial.

Using Memcached in Yii2

Yii2 provides its own Memcache class, which works with both Memcache and Memcached. Let’s get started with this class.

Related: How To Host Yii 2 Framework On Cloudways Using Composer

Adding Memcache Class In Yii2

Go to the config folder inside the main folder of Yii2 and open web.php. Under components add this line:

By setting useMemcached as true, Yii2 will know that I am using Yii2 Memcached.

Using Memcached in Yii2

As i have already added Yii2 Memcache class, it is time to add and save some data in Memcached. Go to the controllers folder in Yii2, create a new file in it and name it MemController.php. Paste the following code in it.

After saving the file, launch the browser and point it to this controller’s index by adding ?r=mem/index. The result would be this line:

memcached result

Now the data is saved in Memcached.

Refresh the page one more time so that it could be ascertained that the data is coming from Memcached and not from any other source(s).

Let’s now verify the data and check whether Memcached is being accessed.

Verifying Data In Memcached

There are two ways you can verify that the data is being saved in Memcached

1. Using Console:

Open your console and type the following command to make a connection to Memcached server:

telnet localhost 11211

Once you are connected, you will get the following screen:

memcached cli

Now we are connected to Memcached server. Type the following to check whether your key exists.

get Mem

If you see the following screen, then your value have been successfully added in MemCached.

memcached get

Next, check the hits on Memcached. Type the following command:

stats

Now look for variable get_hits. You will find the number of hits that Memcached received and the data returned.

memcached hits

2. On Cloudways Panel

On Cloudways, you can easily look up the hit rate of Memcached by going to the server and then choosing Monitoring from the sidebar.

cloudways settings

In the monitoring window, select the Server tab, and from the dropdown, select Memcached Hit Rate. A graph of the hits received by the MemCached will open up.

memcached hit graph

If you are unable to see the hit rate, try refreshing it by clicking the Refresh button.

Final Thoughts

In this tutorial I discussed how to use Memcached with Yii2 as the default cache for your site. If you have a question or wish to contribute to the discussion, please leave a comment below.

Start Creating Web Apps on Managed Cloud Servers Now!

Easy Web App Deployment for Agencies, Developers and E-Commerce Industry

About Ahmed Khan

Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at ahmed.khan@cloudways.com

Stay Connected:

You Might Also Like...

  • Correct me if I’m wrong but right now Redis seems quite a good alternative here instead. Otherwise hopefully there will be some stable release of Memcached extension for PHP 7+ on PECL available:

    https://pecl.php.net/package/memcached

    otherwise this won’t work on latest PHP versions…