php 开发中的 Laravel 和视图缓存——无法立即看到变化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20579182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:18:10  来源:igfitidea点击:

Laravel and view caching in development -- can't see changes right away

phpcachinglaravellaravel-4

提问by Sethen

Some friends and I decided to start working on a project and we came across Laravel and thought it might be a good tool. We started using it locally to develop out some of our pages and noticed something strange.

我和一些朋友决定开始一个项目,我们遇到了 Laravel 并认为它可能是一个很好的工具。我们开始在本地使用它来开发我们的一些页面并注意到一些奇怪的事情。

When we update a view with different information, it would take almost 5 to 10 minutes before the views information would change. It's like Laravel is caching the view and put a TTL on it.

当我们用不同的信息更新一个视图时,视图信息改变之前大约需要 5 到 10 分钟。这就像 Laravel 正在缓存视图并在其上放置 TTL。

I know this isn't anything I am doing on my local web server because I have used other frameworks and I have never encountered this issue.

我知道这不是我在本地 Web 服务器上所做的任何事情,因为我使用过其他框架并且从未遇到过这个问题。

Upon searching the Internet, I can't find a great answer on how to disable this. I want to use Laravel, but find it worthless if it takes a while for my views to update each time I want to make a change. In fact, it sounds counter productive.

在互联网上搜索时,我找不到关于如何禁用它的很好的答案。我想使用 Laravel,但如果每次我想进行更改时我的视图都需要一段时间来更新,我发现它毫无价值。事实上,这听起来适得其反。

Is there any way to disable this? Why are my views taking forever to update right out of the box?

有什么办法可以禁用它吗?为什么我的视图需要很长时间才能立即更新?

回答by Sethen

The #laravel IRC channel is a God send. This had nothing to do with Laravel's behavior at all. This was actually something PHP 5.5 was doing.

#laravel IRC 频道是上帝派来的。这与 Laravel 的行为完全无关。这实际上是 PHP 5.5 正在做的事情。

The reason this was so baffling is because I upgraded my PHP version from 5.3 and never had this issue.

之所以如此莫名其妙是因为我从 5.3 升级了我的 PHP 版本并且从未遇到过这个问题。

In your .ini file, you need to tweak your OPcache settings. For me, these settings began at line 1087 in the .ini file and looked something like this:

在您的 .ini 文件中,您需要调整您的 OPcache 设置。对我来说,这些设置从 .ini 文件的第 1087 行开始,看起来像这样:

opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
opcache.enable_cli=1

Take particular note of the opcache.revalidate_freq=60. This is what is actually making your views cache. If this is not the desired behavior, set the value to 0and your views will update every time you make a change. Yay!

特别注意opcache.revalidate_freq=60. 这实际上是使您的视图缓存。如果这不是所需的行为,请将值设置为0并且每次进行更改时您的视图都会更新。好极了!

EDIT AUGUST 21, 2014

编辑 2014 年 8 月 21 日

As mentioned by Matt below, make sure to restart your web server to see your changes take effect after you have changed your .ini file.

正如下面 Matt 提到的,确保在更改 .ini 文件后重新启动 Web 服务器以查看更改生效。

回答by Wendtly

With newer versions of PHP, opcache doesn't work. This is what I use (in app/filters.php):

使用较新版本的 PHP,opcache 不起作用。这是我使用的(在 app/filters.php 中):

App::before(function($request)
{
    // Clear view cache in sandbox (only) with every request
    if (App::environment() == 'sandbox') {
        $cachedViewsDirectory=app('path.storage').'/views/';
        $files = glob($cachedViewsDirectory.'*');
        foreach($files as $file) {
            if(is_file($file)) {
                @unlink($file);
            }
        }
    }
});

回答by joemaller

It's possible this isn't a caching issue at all and doesn't have anything to do with Laravel, Apache or PHP. If you're sharing files into a Virtual Machine like Vagrant, be sure your editor is not using "Atomic Saves" when writing files.

这可能根本不是缓存问题,并且与 Laravel、Apache 或 PHP 没有任何关系。如果您将文件共享到 Vagrant 之类的虚拟机中,请确保您的编辑器在写入文件时没有使用“原子保存”。

To test this, make a small edit (single character) to a watched file using several different text-editors. Changes saved from editors which implement atomic saves likely won't be noticed by the VM's filesystem.

要对此进行测试,请使用多个不同的文本编辑器对监视的文件进行小规模编辑(单个字符)。从实现原子保存的编辑器保存的更改可能不会被 VM 的文件系统注意到。

I'm editing with Sublime Text 3 on a Mac, saving files to a folder which is mounted into a Vagrant VM with NFS. Files are being watched on the local filesystem via Gulp and a livereload refresh is requested from the Vagrant host whenever a file changes.

我正在 Mac 上使用 Sublime Text 3 进行编辑,将文件保存到一个文件夹中,该文件夹安装到带有 NFS 的 Vagrant VM 中。通过 Gulp 在本地文件系统上监视文件,每当文件更改时,都会从 Vagrant 主机请求实时重新加载刷新。

Changing a single character with Sublime Text 3 using the default atomic_save: truetriggers a change but doesn't serve the updated file. Editing in Vim, TextEdit, Sublime Text 2 and TextWrangler all triggered updates and served the updated file contents. Switching to atomic_saves: falsebrings Sublime Text 3 inline with the other editors, triggering an update and serving the correct file.

使用默认值使用 Sublime Text 3 更改单个字符atomic_save: true会触发更改,但不会提供更新的文件。在 Vim、TextEdit、Sublime Text 2 和 TextWrangler 中编辑都会触发更新并提供更新的文件内容。切换到atomic_saves: false使 Sublime Text 3 与其他编辑器内联,触发更新并提供正确的文件。

Sublime Text 3's default preferences includes this comment:

Sublime Text 3 的默认首选项包括以下评论:

// Save via writing to an alternate file, and then renaming it over the
// original file.
"atomic_save": true,

The problem might have something to do with changes being written to an unwatched tempfile, then that tempfile replacing our watched file. The modification happens when the tempfile is written, not when it replaces the file we're watching, so no update is triggered. That or something with the NFS cache or VirtualBox's NFS gateway -- there's a lot of stuff in the middle.

问题可能与写入未监视临时文件的更改有关,然后该临时文件替换了我们监视的文件。修改发生在临时文件被写入时,而不是在它替换我们正在观察的文件时发生,因此不会触发更新。NFS 缓存或 VirtualBox 的 NFS 网关之类的东西——中间有很多东西。

Many hours were wasted fiddling with opcache, Apache mods and Laravel hacks before discovering this was just an editor setting.

在发现这只是一个编辑器设置之前,很多时间都被浪费在了 opcache、Apache mods 和 Laravel hacks 上。

回答by tremby

Another possibility if you're using a VM (like Vagrant) and it is sharing files from your host via NFS is that NFS is caching the modification times. This would lead Laravel to think that the cached compiled templates are still fresh. This is the problem I had today, and I solved it (and a related problem of gulp-watch not noticing that stylesheet and javascript source files were changing) by adding the NFS mount option lookupcache=none.

如果您使用的是 VM(如 Vagrant)并且它通过 NFS 从您的主机共享文件,另一种可能性是 NFS 正在缓存修改时间。这会导致 Laravel 认为缓存的编译模板仍然是新鲜的。这是我今天遇到的问题,我通过添加 NFS 挂载选项解决了它(以及 gulp-watch 没有注意到样式表和 javascript 源文件正在更改的相关问题)lookupcache=none

I wrote about it here: Watching files for changes on Vagrant, file modification times not updating

我在这里写过它:Watching files for changes on Vagrant,文件修改时间不更新

回答by Tom Sarduy

I had the same problem trying to avoid cache in the admin since the uploaded images were not refreshing. I don't recommend to disable cache for all your php apps, you can do it changing the headers. Add/Edit this function in app/filters.php:

由于上传的图像没有刷新,我在尝试避免在管理员中缓存时遇到了同样的问题。我不建议为您的所有 php 应用程序禁用缓存,您可以更改标题。添加/编辑此功能app/filters.php

Route::filter('after', function($response)
{
    // No caching for pages, you can do some checks before
    $response->header("Pragma", "no-cache");
    $response->header("Cache-Control", "no-store, no-cache, must-revalidate, max-age=0");
});

回答by prograhammer

I had to also adjust the date/time.

我还必须调整日期/时间。

I'm using phpStorm to sftp sync my files (since it will keep server pages loading faster on the VM) with VirtualBox Laravel Homestead. In addition to the opcache.revalidate_freq=0fix, I had to also make sure the Homestead VM had a date/time that was older than the host OS. Otherwise the system doesn't think anything has changed.

我正在使用 phpStorm与 VirtualBox Laravel Homestead sftp 同步我的文件(因为它会使服务器页面在 VM 上加载速度更快)。除了opcache.revalidate_freq=0修复之外,我还必须确保 Homestead VM 的日期/时间早于主机操作系统。否则系统认为没有任何变化。

In ubuntu, do sudo dpkg-reconfigure tzdataand set your timezone. Then if for example your host OS is currently at 11:01:00 am, set the VM to a slightly older time sudo date --set 11:00:50.

在 ubuntu 中,执行sudo dpkg-reconfigure tzdata并设置您的时区。然后,例如,如果您的主机操作系统当前处于上午 11:01:00,请将 VM 设置为稍旧的时间sudo date --set 11:00:50

Then sudo nginx restart. Worked like a charm!

然后sudo nginx restart。像魅力一样工作!

回答by qwertzman

Futhermore, don't forget this taken from: http://php.net/manual/en/opcache.configuration.php#ini.opcache.revalidate-freq

此外,不要忘记这取自:http: //php.net/manual/en/opcache.configuration.php#ini.opcache.revalidate-freq

"This configuration directive is ignored if opcache.validate_timestamps is disabled."

“如果 opcache.validate_timestamps 被禁用,这个配置指令将被忽略。”

Which was the case for me.

我就是这种情况。