This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

How to Use Memcached With Yii 2

Updated on July 15, 2021

4 Min Read

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:

       'cache'         => [

           'class'        => 'yii\caching\MemCache',

           'useMemcached' => true,

       ],

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.

namespace app\controllers;

use Yii;

use yii\web\Controller;

class MemController extends Controller

{

   public function actionIndex()

   {

       $cache = Yii::$app->cache;

       $key   = 'Mem';

       $data  = $cache->get($key);

       if ($data === false) {

           $key  = 'Mem';

           $data = 'My First Memcached Data';

           $cache->set($key, $data);

       }

       echo $data;

   }

}

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.

Get Ready for Core Web Vitals Update

Ebook to Speed Up Your Website Before You Start Losing Traffic.

Thank You

Your list is on it’s Way to Your Inbox.

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.

Share your opinion in the comment section. COMMENT NOW

Share This Article

Customer Review at

“Cloudways hosting has one of the best customer service and hosting speed”

Sanjit C [Website Developer]

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 [email protected]

×

Get Our Newsletter
Be the first to get the latest updates and tutorials.

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!