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

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

Redis Configuration

'cache' => [
    'class' => 'yiiredisCache',
],
'session' => [
    'class' => 'yiiredisSession',
],
'redis'     => [
    'class' => 'yiiredisConnection',
    '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.
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…

Latest Articles