问题描述
我用 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
或者,
因为,我正在使用 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;
}
这篇关于为什么每次刷新页面都会重新加载缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!