top-think / think

ThinkPHP Framework ——十年匠心的高性能PHP框架
http://www.thinkphp.cn
Other
7.83k stars 1.62k forks source link

咨询一下动态修改配置的问题。 #1108

Open Abbotton opened 2 years ago

Abbotton commented 2 years ago

版本:ThinkPHP v6.0.12

我定义了一个service,在boot()或者register()方法中使用如下代码动态修改配置信息,会出现部分修改后的配置不生效的问题,代码如下:

public function boot()
{
    // 修改文件系统配置
    $filesystem = config('filesystem');
    $filesystem['disks']['public']['url'] = '/storage/test';
    $filesystem['disks']['public']['root'] = $this->app->getRootPath().'public'.DIRECTORY_SEPARATOR.'storage'.DIRECTORY_SEPARATOR.'test';
    config(['filesystem' => $filesystem]);
    // 修改缓存配置
    $cache = config('cache');
    $cache['stores']['file']['path'] = $this->app->getRuntimePath().'cache'.DIRECTORY_SEPARATOR.'test';
    config(['cache' => $cache]);
}

经过测试: 修改了文件系统配置后,上传文件会存放到public/storage/test目录中; 修改了缓存配置后,通过Cache::set()方法设置缓存,缓存并没有存放到runtime/cache/test目录中,但打印Cache::getConfig()Cache::getStoreConfig('file')方法确实可以得到修改后的配置值。

问题: 如何动态修改缓存配置并使其生效?

非常感谢。