[Yii2] Cache Speed Comparison

15 October 2021

Home » Blogs » Technology » [Yii2] Cache Speed Comparison

Most website developers tend to ignore caching without realizing that it can harm their applications. A one-second delay in access time can mean you're losing customers or users to competitors.

supports caching in a variety of methods. Supports fragment caching, data caching, page caching, and dynamic content. storage components can be exchanged without having to change the code that uses the .

In this article, we will make a comparison of speeds in several methods.

  • Without using cache
  • FileCache
  • DbCache
  • Redis

Steps to make a cache speed comparison on yii2

  1. Set up a table by using
CREATE TABLE 'test_cache' (
  'data' VARCHAR(20) COLLATE utf8mb4_general_ci DEFAULT NULL,

Create a simple table with id and data columns. Then create a record for the table.

In this trial, we added a data column with random characters with 20 characters. The number of lines we create is 10,000 lines.

  1. Create a model of the table, for example with the name “Testcache”.
  2. Create a Controller to display a response, for example under the name “TestcacheController”

“TestcacheController” without cache

public function actionIndex()
    Yii::$app->response->format = yiiwebResponse::FORMAT_JSON;
    return $data;

“TestcacheController” with cache

public function actionIndex(){
    $data = Yii::$app->cache->getOrSet('testcachedata', function () {
        return TestCache::find()->all();
    Yii::$app->response->format = yiiwebResponse::FORMAT_JSON;
    return $data;
  1. Configure cache in “config/main.php”.

“FileCache” Configuration

'cache' => [
    'class' => 'yiicachingFileCache',

“DbCache” Configuration

        'cache' = > [
            'class' => 'yiicachingDbCache',

For the “DbCache” configuration, previously create a table with the name “cache”

CREATE TABLE 'cache' (
  'id' CHAR(128) COLLATE utf8mb4_general_ci NOT NULL,
  'expire' INTEGER(11) DEFAULT NULL,
  BLOB 'data',
ROW_FORMAT=DYNAMIC CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_general_ci';

Redis Configuration

'cache' => [
    'class' => 'yiiredisCache',
'session' => [
    'class' => 'yiiredisSession',
'redis'     => [
    'class' => 'yiiredisConnection',
    'hostname' => '',
    'port' => 6379,
    'database' => 0,

Previously, install Redis with

php composer.phar require --prefer-dist yiisoft/yii2-redis:"~2.0.0"
  1. Response testing. To test the response speed, we can use Postman to look at the response speed and display the results of the response.
yii2 cache 01

Test results of cache usage on Yii2

yii2 cache 02

The results of the above test are the results we obtained using the same computer. The speed of response obtained is greatly influenced by various conditions, one of which is the hardware used and also the operating system.

From these results, at least we get an overview of the response speed of some methods of cache usage.

Good luck…

Related Articles

File Sharing between Hyper-V Host and Guest

You can share files between hosts and guests on Hyper-V in the following ways. Hyper-V is a Microsoft hardware virtualization that allows you to create and run software versions of computers, called virtual machines. Each virtual machine acts like a full-fledged...

How to use voice typing to type on a PC

Tired of typing, you can use Voice Typing to type articles on a PC or laptop. Voice typing is one of the tools that can convert voice into writing. To use this feature, the PC or laptop you are using must have a microphone. You can use the voice typing feature to...