Ocasional error warning when CakePHP is writing to the cache

心不动则不痛 提交于 2019-11-30 20:59:32

This happens probably when a process different from Apache create files in the cache. This can be the case for instance when you run shell commands as you probably do it as a different user than apache.

By default the File cache creates files with permissions allowing only the user that has created the files to modify them, but this can be fixed by setting a mask in the Cache config in core.php:

Cache::config('_cake_core_', array(
    'engine' => $engine,
    'prefix' => 'cake_core_',
    'path' => CACHE . 'persistent' . DS,
    'serialize' => ($engine === 'File'),
    'duration' => $duration,
    'mask' => 0666
));

Cache::config('_cake_model_', array(
    'engine' => $engine,
    'prefix' => 'cake_model_',
    'path' => CACHE . 'models' . DS,
    'serialize' => ($engine === 'File'),
    'duration' => $duration,
    'mask' => 0666
));
Code Commander

If you want to avoid giving read/write access to the "other" group, check out my other solution here:

https://stackoverflow.com/a/18703956/385979

Simple really, assuming and you are a sudoer and your user name is martinlutherking

sudo adduser martinlutherking www-data

This way cake console commands can read cache files created by apache2, however you may need to do the inverse group add to ensure that www-data can read cache files created by martinlutherking

Just in case anybody is seeing this and wonders how it works in cakePHP 3.x:

modify /config/app.php and add 'mask' => 0666 to

/**
 * Configure the cache adapters.
 */
'Cache' => [
    'default' => [
        'className' => 'File',
        'path' => CACHE,
        'url' => env('CACHE_DEFAULT_URL', null),
        'mask' => 0666
    ],

and probably you also want to add it to the log files:

/**
 * Configures logging options
 */
'Log' => [
    'debug' => [
        'className' => 'Cake\Log\Engine\FileLog',
        'path' => LOGS,
        'file' => 'debug',
        'levels' => ['notice', 'info', 'debug'],
        'url' => env('LOG_DEBUG_URL', null),
        'mask' => 0666
    ],
    'error' => [
        'className' => 'Cake\Log\Engine\FileLog',
        'path' => LOGS,
        'file' => 'error',
        'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
        'url' => env('LOG_ERROR_URL', null),
        'mask' => 0666
    ],
],
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!