Why by every refreshing page, cache reload anew?

懵懂的女人 提交于 2019-12-02 12:37:51
Nana Partykar

Add, this in your config file. According to your need.

$linkAssets

Whether to use symbolic link to publish asset files. Defaults to false, meaning asset files are copied to $basePath. Using symbolic links has the benefit that the published assets will always be consistent with the source assets and there is no copy operation required. This is especially useful during development.

'components' => [
    'assetManager' => [
        'linkAssets' => true,
    ], 
]

Or

$forceCopy

Whether the directory being published should be copied even if it is found in the target directory. This option is used only when publishing a directory. You may want to set this to be true during the development stage to make sure the published directory is always up-to-date. Do not set this to true on production servers as it will significantly degrade the performance.

'components' => [
    'assetManager' => [
        'forceCopy' => true,
    ], 
]

For more info, Please click these useful links

  1. Link Assets - Yii2 Asset Manager
  2. Force Copy - Yii2 Asset Manager
  3. Assets-Clear-Cache - Yii2 (Stack Overflow)

Or,

As, I am using Yii2-App-Basic. So, My Assets are getting created in ROOT/web/assets folder. So, I manually hit this action to clear my cache. This is not a good way to clear cache. Even though, it's useful for time being.

This function, I created in SiteController.php.

And, I hit URL Like : MyWebsite.com/site/clear-cache.

<?
public function actionClearCache(){
  $cacheDirPath = $_SERVER['DOCUMENT_ROOT'].'/assets';
  if($this->destroy_dir($cacheDirPath, 0)){
    Yii::$app->session->setFlash('success', 'Cache cleared.');
  } 
  return $this->render('some-page');
}

private function destroy_dir($dir, $i = 1) {
  if (!is_dir($dir) || is_link($dir))
    return unlink($dir);
  foreach (scandir($dir) as $file) {
    if ($file == '.' || $file == '..') continue;
    if (!$this->destroy_dir($dir . DIRECTORY_SEPARATOR . $file)) {
      chmod($dir . DIRECTORY_SEPARATOR . $file, 0777);
      if (!$this->destroy_dir($dir . DIRECTORY_SEPARATOR . $file))
        return false;
    };
  }
  if($i == 1)return rmdir($dir);
  return true;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!