为什么每次刷新页面都会重新加载缓存? [英] Why by every refreshing page, cache reload anew?

查看:36
本文介绍了为什么每次刷新页面都会重新加载缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 Yii2 编写了我的网站.当我刷新我的网站时,它的工作原理类似于 Ctl + F5,并且所有 font awesome 和我网站的所有缓存都会再次重新加载.看起来像是我第一次打开页面.

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 again. It look likes I open the page first time.

我的网站链接

推荐答案

在您的 config 文件中添加.根据您的需要.

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

$linkAssets

是否使用符号链接发布资产文件.默认为false,意味着资产文件被复制到 $basePath.使用符号链接的好处是发布的资产将始终是与源资产一致,无复制操作必需的.这在开发过程中特别有用.

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,
    ], 
]

$forceCopy

正在发布的目录是否应该复制,即使是在目标目录中找到.此选项仅在以下情况下使用发布目录.您可能希望在此期间将其设置为 true开发阶段以确保发布的目录始终是最新.不要在生产服务器上将此设置为 true,因为它会显着降低性能.

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. 链接资产 - Yii2 资产管理器
  2. 强制复制 - Yii2 资产管理器
  3. Assets-Clear-Cache - Yii2(堆栈溢出)

或者,

因为,我正在使用 Yii2-App-Basic.因此,我的资产正在ROOT/web/assets 文件夹 中创建.所以,我手动点击这个动作来清除我的缓存.这不是清除缓存的好方法.尽管如此,它暂时还是有用的.

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.

这个函数是我在SiteController.php中创建的.

This function, I created in SiteController.php.

而且,我点击了 URL Like:MyWebsite.com/site/clear-cache.

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;
}

这篇关于为什么每次刷新页面都会重新加载缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆