Why by every refreshing page, cache reload anew?

前端 未结 1 2067
无人共我
无人共我 2021-01-28 03:17

I\'ve programmed my website by Yii2. When I refresh my website it works like Ctl + F5, and all the font awesome and all the cache of my site reload aga

相关标签:
1条回答
  • 2021-01-28 03:44

    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;
    }
    
    0 讨论(0)
提交回复
热议问题