1. When the application’s runtime environment is the development environment, meaning the value of YII_ENV is dev, disable Schema caching. Edit environments/dev/common/config/main-local.php
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=g-p-y-a-a-c',
'username' => 'g-p-y-a-a-c',
'password' => 'H9xQ0wnWTDEAbRk9',
'tablePrefix' => 'ac_',
'charset' => 'utf8mb4',
'enableSchemaCache' => false,
'schemaCacheDuration' => 3600,
'schemaCache' => 'redisCache',
],
2. When the application’s runtime environment is the production environment, meaning the value of YII_ENV is prod, enable Schema caching. Edit environments/dev/common/config/main-local.php
'db' => [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=localhost;dbname=g-p-y-a-a-c',
'username' => 'g-p-y-a-a-c',
'password' => 'H9xQ0wnWTDEAbRk9',
'tablePrefix' => 'ac_',
'charset' => 'utf8mb4',
'enableSchemaCache' => true,
'schemaCacheDuration' => 3600,
'schemaCache' => 'redisCache',
],
3. After enabling Schema caching, check Redis. The key name: sa:cache:84f9ac2fc2a79a80b91b6ddd83286798, already contains Schema cache data, as shown in Figure 1

4. To account for table structure changes after executing a new database migration, simulate a database migration by manually updating the table structure and adding the field: is_deleted, as shown in Figure 2

5. Request the API again (related to the user table) and check Redis. The key name: sa:cache:84f9ac2fc2a79a80b91b6ddd83286798, shows that the newly added field: is_deleted, has not yet been updated in the cache
6. Because the table structure changed, the Schema cache data and the Schema data are now inconsistent. Therefore, the database table structure cache for the given connection component needs to be cleared. Reference URL: https://www.yiiframework.com/doc/guide/2.0/zh-cn/caching-data . Execute the console command, but checking Redis, key name: sa:cache:84f9ac2fc2a79a80b91b6ddd83286798, shows that the newly added field: is_deleted, is still not updated in the cache, as shown in Figure 3

PS E:\wwwroot\gitlab-php-yii2-app-advanced-cmc> ./yii cache/flush-schema db
Flush cache schema for "db" connection? (yes|no) [no]:yes
Schema cache for component "db", was flushed.
7. Request the API again (related to the user table) and check Redis. The key name: sa:cache:84f9ac2fc2a79a80b91b6ddd83286798, shows that the newly added field: is_deleted, has been updated in the cache, as expected. This leads to the conclusion that after clearing the database table structure cache for a given connection component, the corresponding cache is not updated immediately. Instead, the update is triggered when the relevant table structure is used (only the involved tables are updated; uninvolved tables are not). As shown in Figure 4

8. Analyzing the principle behind triggering the update further, it likely lies in the key name: sa:cache:28c0caa7a3c3c0bfa2e9d21956903c9d. After clearing the database table structure cache for the given connection component, its value changed. A comparison of the values before and after is shown in Figure 5

a:2:{i:0;s:21:"0.25243100 1577424191";i:1;N;}
a:2:{i:0;s:21:"0.84367400 1577424375";i:1;N;}
9. When deploying with Docker, during container upgrades, after executing the database migration command, execute the command to clear the database table structure cache for the given connection component. Edit build/c_files/config/init/console_init.sh, as shown in Figure 6

if [[ $PCS_API_CFG_MIGRATE == "true" ]]
then
php /mcloud/www/pcs-api/yii migrate --migrationPath=@yii/log/migrations/ --interactive=0
php /mcloud/www/pcs-api/yii migrate --interactive=0
php /mcloud/www/pcs-api/yii cache/flush-schema db --interactive=0
elif [[ $PCS_API_CFG_MIGRATE == "false" ]]
then
echo "running without db migrate"
else
echo "please set environment variable PCS_API_CFG_MIGRATE true or false"
fi
10. When upgrading the container via Rancher, with the application’s runtime environment set to production, meaning the value of YII_ENV is prod, check the upgrade logs: Schema cache for component “db”, was flushed., as shown in Figure 7

2019/12/27 下午3:24:28PCS_API_CFG_ENV=prod
2019/12/27 下午3:24:28Yii Application Initialization Tool v1.0
2019/12/27 下午3:24:28
2019/12/27 下午3:24:28
2019/12/27 下午3:24:28 Start initialization ...
2019/12/27 下午3:24:28
2019/12/27 下午3:24:28 generate console/config/main-local.php
2019/12/27 下午3:24:28 generate console/config/params-local.php
2019/12/27 下午3:24:28 generate common/config/main-local.php
2019/12/27 下午3:24:28 generate common/config/params-local.php
2019/12/27 下午3:24:28 generate yii
2019/12/27 下午3:24:28 generate api/web/index.php
2019/12/27 下午3:24:28 generate api/web/robots.txt
2019/12/27 下午3:24:28 generate api/config/main-local.php
2019/12/27 下午3:24:28 generate api/config/params-local.php
2019/12/27 下午3:24:28 generate frontend/web/index.php
2019/12/27 下午3:24:28 generate frontend/web/robots.txt
2019/12/27 下午3:24:28 generate frontend/config/main-local.php
2019/12/27 下午3:24:28 generate frontend/config/params-local.php
2019/12/27 下午3:24:28 generate rpc/web/index.php
2019/12/27 下午3:24:28 generate rpc/web/robots.txt
2019/12/27 下午3:24:28 generate rpc/config/main-local.php
2019/12/27 下午3:24:28 generate rpc/config/params-local.php
2019/12/27 下午3:24:28 generate backend/web/index.php
2019/12/27 下午3:24:28 generate backend/web/robots.txt
2019/12/27 下午3:24:28 generate backend/config/main-local.php
2019/12/27 下午3:24:28 generate backend/config/params-local.php
2019/12/27 下午3:24:28 generate cookie validation key in backend/config/main-local.php
2019/12/27 下午3:24:28 generate cookie validation key in frontend/config/main-local.php
2019/12/27 下午3:24:28 generate cookie validation key in api/config/main-local.php
2019/12/27 下午3:24:28 chmod 0777 backend/runtime
2019/12/27 下午3:24:28 chmod 0777 backend/web/assets
2019/12/27 下午3:24:28 chmod 0777 frontend/runtime
2019/12/27 下午3:24:28 chmod 0777 frontend/web/assets
2019/12/27 下午3:24:28 chmod 0777 api/runtime
2019/12/27 下午3:24:28 chmod 0777 api/web/assets
2019/12/27 下午3:24:28 chmod 0755 yii
2019/12/27 下午3:24:28
2019/12/27 下午3:24:28 ... initialization completed.
2019/12/27 下午3:24:28
2019/12/27 下午3:24:28PCS_API_CFG_MIGRATE=true
2019/12/27 下午3:24:28Yii Migration Tool (based on Yii v2.0.15.1)
2019/12/27 下午3:24:28
2019/12/27 下午3:24:28No new migrations found. Your system is up-to-date.
2019/12/27 下午3:24:28Yii Migration Tool (based on Yii v2.0.15.1)
2019/12/27 下午3:24:28
2019/12/27 下午3:24:28No new migrations found. Your system is up-to-date.
2019/12/27 下午3:24:29Schema cache for component "db", was flushed.
11. For the same API (query), before enabling Schema caching, its performance test results showed concurrency (peak/limit): 120/150, as shown in Figure 8

12. For the same API (query), before enabling Schema caching, its performance test results showed that at a concurrency of 105, requests were consistently successful. When it increased to 120, they started to fail. Therefore, the actual concurrency peak can be considered to be 105, as shown in Figure 9

13. For the same API (query), after enabling Schema caching, its performance test results showed that at a concurrency of 90, requests were consistently successful. When it increased to 105, they started to fail. Therefore, the actual concurrency peak can be considered to be 90. The performance optimization effect is not very obvious, and even slightly regressed, but I decided to leave it as is for now and not roll back the version, as shown in Figure 10

