ugiw/lumen-config-cache

为Lumen添加Laravel命令 `php artisan config:cache`

1.0.0 2020-09-29 13:11 UTC

This package is not auto-updated.

Last update: 2024-09-26 07:31:50 UTC


README

将Laravel artisan命令 config:cache 添加到Lumen。

安装后,您可以在控制台中键入 php artisan lumen-config:cache 以将所有Lumen配置文件加载到缓存中,这将提高Lumen应用程序的响应时间。

安装

此包可用于Laravel Lumen 7或更高版本,且PHP版本为7.0或更高。

您可以通过composer安装此包

composer require ugiw/lumen-config-cache

现在在 bootstrap/app.php 文件中注册服务提供者

$app->register(Ugiw\ConfigCache\ServiceProviders\ConfigCacheServiceProvider::class);

如果您想自动发布配置文件,您必须安装一个像lumen-vendor-publish这样的包,该包包含一个命令,可让您将包配置文件发布到Lumen应用程序的配置文件夹。

因此,您可以像在Laravel中一样发布配置文件

php artisan vendor:publish --tag="lumen-config-cache"

如果您决定不安装任何额外的包,则可以将文件 vendor/gipro/lumen-config-cache/src/config/config-cache.php 复制到您的应用程序的 config 文件夹中。

这是发布的 config/config-cache.php 配置文件的内容

return [

    /**
     * The name of the key where the config is stored in cache
     */
    'cache_key' => 'config_cache',

    /**
     * Expiration time for the config in cache
     */
    'cache_expiration_time' => 60*24, // One day

    /**
     * The config files (app, database, queue, etc.) to be cached
     * Add to this array whatever config files you want to load in cache
     */
    'config_files' => [
        'app',
    ],

];

您应该在 bootstrap/app.php 文件中取消以下行的注释以启用Lumen中facade的使用

// $app->withFacades();

用法

您可以使用 ConfigCache::get facade 方法访问您的配置键

use Ugiw\ConfigCache\Facades\ConfigCache;

...

$api_url = ConfigCache::get('app.URL');

您还可以使用 ConfigCache::refresh facade 方法强制刷新缓存的配置

use Ugiw\ConfigCache\Facades\ConfigCache;

...

ConfigCache::refresh();

您还可以使用 artisan 命令 lumen-config:cache 强制刷新缓存的配置

提示:将此命令添加到您的部署脚本中,以确保在部署新应用程序版本后,您有最新的配置缓存

php artisan lumen-config:cache