[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.

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

In this article, we will make a comparison of cache 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 MySQL
CREATE TABLE 'test_cache' (
  'id' INTEGER(11) NOT NULL AUTO_INCREMENT,
  'data' VARCHAR(20) COLLATE utf8mb4_general_ci DEFAULT NULL,
  PRIMARY KEY USING BTREE ('id')
) ENGINE=InnoDB
AUTO_INCREMENT=1 ROW_FORMAT=DYNAMIC CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_general_ci';

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

TOP TUTORIALS:  [yii2] How to Change a GridView Filter from GET to POST

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()
{
    $data=TestCache::find()->all();
    \Yii::$app->response->format = \yii\web\Response::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 = \yii\web\Response::FORMAT_JSON;
    return $data;
}
  1. Configure yii2 cache in “config/main.php”.

“FileCache” Configuration

'cache' => [
    'class' => 'yii\caching\FileCache',
],

“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',
  PRIMARY KEY USING BTREE ('id')
) ENGINE=InnoDB
ROW_FORMAT=DYNAMIC CHARACTER SET 'utf8mb4' COLLATE 'utf8mb4_general_ci';

Redis Configuration

'cache' => [
    'class' => 'yii\redis\Cache',
],
'session' => [
    'class' => 'yii\redis\Session',
],
'redis'     => [
    'class' => 'yii\redis\Connection',
    'hostname' => '127.0.0.1',
    '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.
TOP TUTORIALS:  [Yii2] How to create and use Functions on Yii2 Basic and Advanced Templates

Test results of cache usage on Yii2

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…

Leave a Comment

Your email address will not be published. Required fields are marked *