php Laravel 5 – 清除共享托管服务器中的缓存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31455829/
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
Laravel 5 – Clear Cache in Shared Hosting Server
提问by Rinto George
The question is pretty clear.
这个问题很清楚。
php artisan cache:clear
Is there any workaround to clear the cache like above we using in CLI. I am using a popular shared hosting service, but as per my plan, I don't have control panel access.
是否有任何解决方法可以清除我们在 CLI 中使用的上述缓存。我正在使用流行的共享托管服务,但根据我的计划,我没有控制面板访问权限。
** I want to clear the views cache.**
** 我想清除视图缓存。**
I saw a questionalmost the same as this, but it doesn't help me.
我看到了一个与此几乎相同的问题,但这对我没有帮助。
回答by Marco Pallante
You can call an Artisan command outside the CLI.
您可以在 CLI 之外调用 Artisan 命令。
Route::get('/clear-cache', function() {
$exitCode = Artisan::call('cache:clear');
// return what you want
});
You can check the official doc here http://laravel.com/docs/5.0/artisan#calling-commands-outside-of-cli
您可以在此处查看官方文档 http://laravel.com/docs/5.0/artisan#calling-commands-outside-of-cli
Update
更新
There is no way to delete the view cache. Neither php artisan cache:clear
does that.
没有办法删除视图缓存。那也不行php artisan cache:clear
。
If you really want to clear the view cache, I think you have to write your own artisan
command and call it as I said before, or entirely skip the artisan
path and clear the view cache in some class that you call from a controller or a route.
如果您真的想清除视图缓存,我认为您必须编写自己的artisan
命令并像我之前所说的那样调用它,或者完全跳过artisan
路径并清除您从控制器或路由调用的某个类中的视图缓存。
But, my real question is do you really need to clear the view cache? In a project I'm working on now, I have almost 100 cached views and they weight less then 1 Mb, while my vendor
directory is > 40 Mb. I don't think view cache is a real bottleneck in disk usage and never had a real need to clear it.
但是,我真正的问题是您真的需要清除视图缓存吗?在我现在正在处理的一个项目中,我有近 100 个缓存视图,它们的重量小于 1 Mb,而我的vendor
目录大于 40 Mb。我不认为视图缓存是磁盘使用的真正瓶颈,而且从来没有真正需要清除它。
As for the application cache, it is stored in the storage/framework/cache
directory, but only if you configured the file
driver in config/cache.php
. You can choose many different drivers, such as Redisor Memcached, to improve performances over a file-based cache.
至于应用程序缓存,它存储在storage/framework/cache
目录中,但前提是您file
在config/cache.php
. 您可以选择许多不同的驱动程序,例如Redis或Memcached,以提高基于文件的缓存的性能。
回答by The Dead Guy
I hope this helps someone
我希望这可以帮助别人
Go to laravelFolder/bootstrap/cache
then rename config.php
to anything you want eg. config.php_old
and reload your site. That should work like voodoo.
转到laravelFolder/bootstrap/cache
然后重命名config.php
为您想要的任何内容,例如。config.php_old
并重新加载您的网站。这应该像伏都教一样工作。
Happy Coding...
快乐编码...
回答by Francesco
As I can see: http://itsolutionstuff.com/post/laravel-5-clear-cache-from-route-view-config-and-all-cache-data-from-applicationexample.html
正如我所看到的:http: //itsolutionstuff.com/post/laravel-5-clear-cache-from-route-view-config-and-all-cache-data-from-applicationexample.html
is it possible to use the code below with the new clear cache commands:
是否可以将以下代码与新的清除缓存命令一起使用:
//Clear Cache facade value:
Route::get('/clear-cache', function() {
$exitCode = Artisan::call('cache:clear');
return '<h1>Cache facade value cleared</h1>';
});
//Reoptimized class loader:
Route::get('/optimize', function() {
$exitCode = Artisan::call('optimize');
return '<h1>Reoptimized class loader</h1>';
});
//Route cache:
Route::get('/route-cache', function() {
$exitCode = Artisan::call('route:cache');
return '<h1>Routes cached</h1>';
});
//Clear Route cache:
Route::get('/route-clear', function() {
$exitCode = Artisan::call('route:clear');
return '<h1>Route cache cleared</h1>';
});
//Clear View cache:
Route::get('/view-clear', function() {
$exitCode = Artisan::call('view:clear');
return '<h1>View cache cleared</h1>';
});
//Clear Config cache:
Route::get('/config-cache', function() {
$exitCode = Artisan::call('config:cache');
return '<h1>Clear Config cleared</h1>';
});
It's not necessary to give the possibility to clear the caches to everyone, especially in a production enviroment, so I suggest to comment that routes and, when it's needed, to de-comment the code and run the routes.
没有必要向所有人提供清除缓存的可能性,尤其是在生产环境中,因此我建议对该路由进行注释,并在需要时取消注释代码并运行路由。
回答by Maulik
Config cachingThe laravel config spreads across dozens of files, and including
every one of them for each request is a costly process. To combine all of your config files into one, use:
配置缓存laravel 配置分布在数十个文件中,including
每个请求的每个文件都是一个代价高昂的过程。要将所有配置文件合并为一个,请使用:
php artisan config:cache
Keep in mind that any changes to the config will not have any effect once you cache it. To refresh the config cache, run the above command again. In case you want to completely get rid of the config cache, run
请记住,一旦缓存配置,对配置的任何更改都不会产生任何影响。要刷新配置缓存,请再次运行上述命令。如果您想完全摆脱配置缓存,请运行
php artisan config:clear
Routes cachingRouting is also an expensive task in laravel. To cache the routes.php file run the below command:
路由缓存路由在 Laravel 中也是一项昂贵的任务。要缓存 routes.php 文件,请运行以下命令:
php artisan route:cache
Mind that it doesn't work with closures. In case you're using closures this is a great chance to move them into a controller, as the artisan command will throw an exception when trying to compile routes that are bound to closures instead of proper controller methods. In the same as the config cache, any changes to routes.php will not have any effect anymore. To refresh the cache, run the above command everytime you do a change to the routes file. To completely get rid of the route cache, run the below command:
请注意,它不适用于闭包。如果您正在使用闭包,这是将它们移动到控制器中的绝佳机会,因为在尝试编译绑定到闭包而不是正确的控制器方法的路由时,artisan 命令将抛出异常。与配置缓存相同,对 routes.php 的任何更改都不再有任何影响。要刷新缓存,请在每次更改路由文件时运行上述命令。要完全摆脱路由缓存,请运行以下命令:
php artisan route:clear
Classmap optimization
类图优化
It's not uncommon for a medium-sized project to be spread across hundreds of PHP files. As good coding behaviours dictate us, everything has its own file. This, of course, does not come without drawbacks. Laravel has to include dozens of different files for each request, which is a costly thing to do.
一个中型项目分布在数百个 PHP 文件中的情况并不少见。正如良好的编码行为所指示的那样,一切都有自己的文件。当然,这并非没有缺点。Laravel 必须为每个请求包含几十个不同的文件,这是一件代价高昂的事情。
Hence, a good optimization method is declaring which files are used for every request (this is, for example, all your service providers, middlewares and a few more) and combining them in only one file, which will be afterwards loaded for each request. This not different from combining all your javascript files into one, so the browser will have to make fewer requests to the server.
因此,一种好的优化方法是声明每个请求使用哪些文件(例如,您的所有服务提供者、中间件等)并将它们组合在一个文件中,然后为每个请求加载该文件。这与将所有 javascript 文件合并为一个没有什么不同,因此浏览器将不得不向服务器发出更少的请求。
The additional compiles files (again: service providers, middlewares and so on) should be declared by you in config/compile.php, in the files key. Once you put there everything essential for every request made to your app, concatenate them in one file with:
额外的编译文件(同样:服务提供者、中间件等)应该由您在 config/compile.php 中的 files 键中声明。一旦你把对你的应用程序发出的每个请求所必需的一切都放在那里,将它们连接到一个文件中:
php artisan optimize --force
Optimizing the composer autoload
优化 composer 自动加载
This one is not only for laravel, but for any application that's making use of composer.
这不仅适用于 laravel,也适用于任何使用 composer 的应用程序。
I'll explain first how the PSR-4 autoload works, and then I'll show you what command you should run to optimize it. If you're not interested in knowing how composer works, I recommend you jumping directly to the console command.
我将首先解释 PSR-4 自动加载的工作原理,然后我将向您展示应该运行什么命令来优化它。如果您对了解 composer 如何工作不感兴趣,我建议您直接跳转到控制台命令。
When you ask composer for the App\Controllers\AuthController
class, it first searches for a direct association in the classmap. The classmap is an array with 1-to-1 associations of classes and files. Since, of course, you did not manually add the Login class and its associated file to the classmap, composer will move on and search in the namespaces.
Because App is a PSR-4 namespace, which comes by default with Laravel and it's associated to the app/
folder, composer will try converting the PSR-4 class name to a filename with basic string manipulation procedures. In the end, it guesses that App\Controllers\AuthController
must be located in an AuthController.php file, which is in a Controllers/
folder that should luckily be in the namespace folder, which is app/
.
当您向App\Controllers\AuthController
Composer询问类时,它首先在类图中搜索直接关联。classmap 是一个具有类和文件一对一关联的数组。当然,由于您没有手动将 Login 类及其关联文件添加到类映射,composer 将继续搜索名称空间。因为 App 是一个 PSR-4 命名空间,它是 Laravel 默认附带的并且它与app/
文件夹相关联,composer 将尝试使用基本的字符串操作过程将 PSR-4 类名转换为文件名。最后,它猜测它App\Controllers\AuthController
必须位于一个 AuthController.php 文件中,该文件位于一个文件Controllers/
夹中,幸运的是它应该位于命名空间文件夹中,即app/
.
All this hard work only to get that the App\Controllers\AuthController
class exists in the app/Controllers/AuthController.php
file. In order to have composer scanning your entire application and create direct 1-to-1 associations of classes and files, run the following command:
所有这些努力只是为了让App\Controllers\AuthController
该类存在于app/Controllers/AuthController.php
文件中。为了让 composer 扫描整个应用程序并创建类和文件的直接一对一关联,请运行以下命令:
composer dumpautoload -o
Keep in mind that if you already ran php artisan optimize --force, you don't have to run this one anymore. Since the optimize command already tells composer to create an optimized autoload.
请记住,如果您已经运行了 php artisan optimize --force,您就不必再运行这个了。由于优化命令已经告诉作曲家创建一个优化的自动加载。
回答by Afraz Ahmad
This package is for php ^7.0 and ^laravel5.5.
这个包适用于 php ^7.0 和 ^laravel5.5。
Use this package in cronjobthat I have created for this purpose only. I was also facing same situation. https://packagist.org/packages/afrazahmad/clear-cached-dataInstall it and run:
在我为此目的创建的cronjob中使用这个包。我也面临同样的情况。 https://packagist.org/packages/afrazahmad/clear-cached-data安装并运行:
php artisan clear:data
and it will run the following commands automcatically
它将自动运行以下命令
php artisan cache:clear
php artisan view:clear
php artisan route:clear
php artisan clear-compiled
php artisan config:cache
Hope it helps.
希望能帮助到你。
If you want to run it automatically at specific time then you will have to setup crnjob first. e.g.
如果您想在特定时间自动运行它,那么您必须先设置 crnjob。例如
in app/console/kernel.php
In schedule function:
在计划功能中:
$schedule->command('clear:data')->dailyAt('07:00');
回答by Laurence
Basically I want to clear the views cache.
基本上我想清除视图缓存。
There is now a command in Laravel 5.1 for that
现在在 Laravel 5.1 中有一个命令
php artisan view:clear
回答by gandra404
You can connect via FTP and clear storage\framework\views
folder for laravel 5
or app\storage\views
for laravel 4
.
您可以通过FTP连接和清除storage\framework\views
文件夹laravel 5
或 app\storage\views
为laravel 4
。
回答by Amos Chihi
To clear all cache outside CLI, Do this; This works for me.
要清除 CLI 之外的所有缓存,请执行此操作;这对我有用。
Route::get('/clear', function() {
Artisan::call('cache:clear');
Artisan::call('config:clear');
Artisan::call('config:cache');
Artisan::call('view:clear');
return "Cleared!";
});
回答by Spidi
php artisan view:clear
will clear the cached views
将清除缓存的视图
回答by pableiros
You can do this if you are using Lumen
from Laravel
on your routes/web.php
file:
如果您在文件中使用Lumen
from Laravel
,则可以执行此routes/web.php
操作:
use Illuminate\Support\Facades\Artisan;
$app->get('/clear-cache', function () {
$code = Artisan::call('cache:clear');
return 'cache cleared';
});