PHP7中的artisan缓存清理


php7中的artisan缓存清理

背景:在接手php项目,很多时候碰到需要清理缓存,在多台节点的环境中,分为多种情况。

  • 清理config配置缓存
  • 清理route配置缓存
  • 清理应用缓存

到底什么时候我们需要清理缓存,这些操作具体执行了什么操作,为什么有时候只需要一台执行,有时候需要多台?

  • 当config配置文件修改时,需要清理config配置缓存。如果有多个节点,多个节点都需要执行
  • 当route配置文件修改时,需要清理route配置环境。如果有多个节点,多个节点都需要执行
  • 当需要清理应用cache时。如果采用file模式(无共享存储),需要清理多个节点;如果采用共享文件夹或者共享存储(redis,mysql)之类的数据库时,只需要单节点执行。

常用缓存相关命令

  • route相关
    php artisan route:clear
    php artisan route:cache
  • config相关
    php artisan config:clear
    php artisan config:clear
  • cache相关
    php artisan cache:clear

route缓存机制

缓存机制

下面是php artisan中route相关的命令文件

[root@test-web-01.novalocal 14:22 /webser/www/Laravel_git/Laravel5.1/vendor/laravel/framework/src/Illuminate]
# cat Foundation/Console/Route
RouteCacheCommand.php  RouteClearCommand.php  RouteListCommand.php

在文件中找到route:cache函数

public function fire()
    {
        $this->call('route:clear');

        $routes = $this->getFreshApplicationRoutes();

        if (count($routes) == 0) {
            return $this->error("Your application doesn't have any routes.");
        }

        foreach ($routes as $route) {
            $route->prepareForSerialization();
        }

        $this->files->put(
            $this->laravel->getCachedRoutesPath(), $this->buildRouteCacheFile($routes)
        );

        $this->info('Routes cached successfully!');
    }

首先会调用route:clear命令。进行route的cache清理。然后获取刷新后的route。然后序列化获取到的route的配置。 然后将route的配置通过buildRouteCacheFile函数写到getCachedRoutesPath函数返回的route的缓存位置中。 其中,getCachedRoutesPath函数位置为/webser/www/Laravel_git/Laravel5.1/vendor/laravel/framework/src/Illuminate/Foundation/Application.php

public function getCachedRoutesPath()
    {
        return $this->basePath().'/bootstrap/cache/routes.php';
    }

所以route的缓存机制为,将route的配置序列化后,写到项目下面的bootstrap/cache/routes.php文件中。具体如下

[root@matrix-web-04.novalocal 14:20 /webser/www/new_matrix]
# cat bootstrap/cache/routes.php | more
<?php

/*
|--------------------------------------------------------------------------
| Load The Cached Routes
|--------------------------------------------------------------------------
|
| Here we will decode and unserialize the RouteCollection instance that
| holds all of the route information for an application. This allows
| us to instantaneously load the entire route map into the router.
|
*/

app('router')->setRoutes(
    unserialize(base64_decode('TzozNDoiSWxsdW1paToyO3M3M6MzoiUFVUIjtpOjQ7czo1OiJQQVRDSCI7aTo1O3M6NjoiREVMRVRFI
jt9czo5OiIAKgBhY3Rpb24iO2E6ODp7czo2OiJkb...

route清理

命令文件为

[root@test-web-01.novalocal 14:27 /webser/www/Laravel_git/Laravel5.1/vendor/laravel/framework/src/Illuminate]
# cat Foundation/Console/RouteClearCommand.php 
执行的函数
    public function fire()
    {
        $this->files->delete($this->laravel->getCachedRoutesPath());

        $this->info('Route cache cleared!');
    }

这里只执行了一个删除文件的操作。具体文件和上面创建时候函数一样。通过getCachedRoutesPath获取到相关路径。 getCachedRoutesPath函数位置为/webser/www/Laravel_git/Laravel5.1/vendor/laravel/framework/src/Illuminate/Foundation/Application.php

public function getCachedRoutesPath()
    {
        return $this->basePath().'/bootstrap/cache/routes.php';
    }

config缓存

config缓存创建

[root@test-web-01.novalocal 14:53 /webser/www/Laravel_git/Laravel5.1/vendor/laravel/framework/src/Illuminate]
# ls -lt Foundation/Console/
ConfigCacheCommand.php ConfigClearCommand.php ConsoleMakeCommand.php

具体的缓存创建函数

public function fire()
    {
        $this->call('config:clear');

        $config = $this->getFreshConfiguration();

        $this->files->put(
            $this->laravel->getCachedConfigPath(), '<?php return '.var_export($config, true).';'.PHP_EOL
        );

        $this->info('Configuration cached successfully!');
    }

先调用config:clear清理缓存,然后刷新缓存配置,在重新生成缓存配置文件。

具体的获取缓存路径的函数/webser/www/Laravel_git/Laravel5.1/vendor/laravel/framework/src/Illuminate/Foundation/Application.php

public function getCachedConfigPath()
    {
        return $this->basePath().'/bootstrap/cache/config.php';
    }

config缓存清理

删除缓存配置文件即可。函数位于/webser/www/Laravel_git/Laravel5.1/vendor/laravel/framework/src/Illuminate/Foundation/Console/ConfigClearCommand.php

public function fire()
    {
        $this->files->delete($this->laravel->getCachedConfigPath());

        $this->info('Configuration cache cleared!');
    }

cache缓存清理

cache命令的执行文件位置/webser/www/Laravel_git/Laravel5.1/vendor/laravel/framework/src/Illuminate/Cache/Console/ClearCommand.php

public function fire()
    {
        $storeName = $this->argument('store');

        $this->laravel['events']->fire('cache:clearing', [$storeName]);

        $this->cache->store($storeName)->flush();

        $this->laravel['events']->fire('cache:cleared', [$storeName]);

        $this->info('Application cache cleared!');
    }

上面调用对了cache中的store相关方法,执行了flush命令。 其中cache为cacehmanager类,用来加载配置中的cache_drive,并且初始化连接。

根据配置来看

CACHE_DRIVER=redis

执行上面的方法执行了redis的flush()函数,会将redis中的值全部清空掉。

注意:此命令慎用。redis会flush掉库里面的所有数据。