I'm developing a CakePHP 2.2 site locally with MAMP. Every so often, I get one or more warnings similar to this, about not being able to write to one or more cache files:
Warning: SplFileInfo::openFile(/Applications/MAMP/htdocs/mywebsite/www/app/tmp/cache/persistent/myapp_cake_core_cake_console_en-au): failed to open stream: Permission denied in /Applications/MAMP/htdocs/mywebsite/www/lib/Cake/Cache/Engine/FileEngine.php on line 313
The weird thing is, /tmp is 777, tmp/cache is 777, and tmp/cache/persistent is 777 (don't worry... it won't be 777 on the server!). The file itself inside tmp/cache/persistent is 644 - but I assume Cake is creating and managing that file, and does so with the permissions it needs.
If I just refresh the page, the error goes away (and then re-appears sometime later). I'm not doing any explicit caching, so this stuff is just Cake doing whatever it automatically does.
So my question is:
a) How does this automatic caching of Cake's work? Is it trying to write to that file on every page refresh, and failing only once in a while? Or is it only trying to write to that file once in a while, but failing every time it tries?
b) If it's only failing only once in a while, can I safely just ignore it? And if it's failing every time it tries, how can I fix it?
Thanks in advance for any help!
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
));
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
],
],
Related
Upgraded CakePHP from 3.5 -> 3.6 -> 3.7. The error message, Property _transportConfig does not exist, is displayed as soon as the application starts in the browser.
Email Transport config in app.php
'EmailTransport' => [
'default' => [
'className' => 'Smtp',
'host' => 'smtp.gmail.com',
'port' => 587,
'username' => '*******#gmail.com',
'password' => '********************',
'log' => true,
'tls' => true
],
],
I've found some information in the migration guide, here is a solution that may work;
First, you need to add this to your bootstrap file
use Cake\Mailer\TransportFactory;
then replace
Email::setConfigTransport(Configure::consume('EmailTransport'));
by
TransportFactory::setConfig(Configure::consume('EmailTransport'));
finally you might consider updating the debugger via composer :
λ composer require --update-with-dependencies "cakephp/debug_kit"
Might not be the best way to, but it worked for me!
I'm working on a project which only a few packages of cake 3 :
cakephp/orm
cakephp/validation
cakephp/i18n
cakephp/cache
I just installed the last one (cache).
I uploaded my project to a production server, and was surprised to see that my queries using the ORM are extremely slow (a query that lasts about 100ms on my local machine can take up to 5 or 10 seconds on the production server).
It seems that there are queries on the information_schema table that take much time and resources. So I've went on the web and saw that I needed the enable cacheMetaData param in my config.
My config looks like this :
ConnectionManager::config('default', [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'host' => 'my-host',
'database' => 'my-database',
'username' => 'my-username',
'password' => 'my-password',
'encoding' => 'utf8',
'timezone' => 'UTC',
'cacheMetaData' => true // If set to `true` you need to install the optional "cakephp/cache" package.
]);
I followed the instruction above and installed the cakephp/cache package. But I'm guessing I need to enable it somehow (or somewhere), but can't figure out how (or where).
Here is what I tried :
\Cake\Cache\Cache::config('_cake_model_', [
'className' => 'File',
'prefix' => 'myapp_cake_model_',
'path' => '/cache/models/',
'serialize' => true,
'duration' => '+2 minutes',
]);
But it's still not working, my cache or cache/models/ folder is still empty and the requests are taking a long time.
How can I fix this ?
Thanks for your time
kinkaz
For a detailed solution on this topic, please see http://discourse.cakephp.org/t/orm-cache-metadata-issue/1071
I'am Using Cakephp 2.4.6 and i want use the caching in my application.
in my Bootstrap.php i found that
Cache::config('default', array('engine' => 'File'));
and in my core.php i found
Cache::config('_cake_core_', array(
'engine' => $engine,
'prefix' => $prefix . 'cake_core_',
'path' => CACHE . 'persistent' . DS,
'serialize' => ($engine === 'File'),
'duration' => $duration
));
Configure the cache for model and datasource caches. This cache configuration
is used to store schema descriptions, and table listings in connections.
Cache::config('_cake_model_', array(
'engine' => $engine,
'prefix' => $prefix . 'cake_model_',
'path' => CACHE . 'models' . DS,
'serialize' => ($engine === 'File'),
'duration' => $duration
));
After reading the document Cakephp Caching¶
i have tried to cofigure Apc Cach in Core.php
Cache::config('default', array(
'engine' => 'Apc',
'duration'=> 9000,
'probability'=> 100,
'prefix' => Inflector::slug(APP_DIR) . '_',
));
it showing the error
Fatal error: Uncaught exception 'CacheException' with message
'Cache engine default is not properly configured.' in C:\xampp\htdocs\go4add\lib\Cake\Cache\Cache.php:181 Stack trace: #0
then i tried it to configuring in bootstrap.php , same effect.
Please help me..i am confusing..
Is there any Installation needed form Apc Cache and memcache in cakephp?
if needed please tel me how to Instal Apc i nCakeph
Don't touch any thing. Just change the $engine = 'File'; to $engine = 'Apc'; at core.php on line 352. And don't forget to install Apc.
I'd like to set a up log file so that only messages with a scope of soap get logged to that file. Conversely, I'd also like to prevent messages with a scope of soap being logged against the default logs of debug and error.
Here is my current setup in bootstrap.php.
CakeLog::config('soap', array(
'engine' => 'FileLog',
'types' => array('info','debug','error'),
'scopes' => array('soap'),
'file' => 'soap'
));
CakeLog::config('debug', array(
'engine' => 'FileLog',
'types' => array('notice', 'info', 'debug'),
'file' => 'debug',
));
CakeLog::config('error', array(
'engine' => 'FileLog',
'types' => array('warning', 'error', 'critical', 'alert', 'emergency'),
'file' => 'error',
));
Here is a snippet from one of my libs that makes soap calls.
CakeLog::debug("REQUEST:\n" . $client->__getLastRequest() . "\n", 'soap');
CakeLog::debug("RESPONSE:\n" . $client->__getLastResponse() . "\n", 'soap');
} catch (SoapFault $e) {
CakeLog::error(print_r($result,true), 'soap');
CakeLog::error('Exception: (' . $e->getCode() . ') ' . $e->getMessage(), 'soap');
if (isset($client)) {
CakeLog::error("Errored REQUEST:\n" . print_r($client->__getLastRequest(), true) . "\n", 'soap');
}
When I do this current set up, I get the debug soap messages in debug.log and soap.log, which is not desirable.
According CakeLog::config documentation
If you don't define any scopes an adapter will catch all scopes that match the handled levels.
so, debug logger write all messages with type 'types' => array('notice', 'info', 'debug'),
Now how you can avoid it? Seems you have 2 ways:
use CakeLog::write with custom error type
CakeLog::write('soap', "REQUEST:\n" . $client->__getLastRequest() . "\n");
in this case, according documentation of CakeLog::write method
integer|string $type
Type of message being written. When value is an integer or a string matching the recognized levels, then it will be treated log levels. Otherwise it's treated as scope.
scope and type will same and have 'soap' value, so, message will not logged in debug logger, but only in soap.
leave it as now, cause save all debug messages in one file and some messages in some specific logs not such bad idea.
A logger with no defined scopes will log all scopes, as is the case with the debug and error log configuration in the question.
To prevent the debug+error logs from containing soap messages, you'll need to defined explicitly what scopes they should act upon:
CakeLog::config('debug', array(
'engine' => 'FileLog',
'types' => array('notice', 'info', 'debug'),
// restrict to scope=type only
'scopes' => array('notice', 'info', 'debug'),
'file' => 'debug',
));
CakeLog::config('error', array(
'engine' => 'FileLog',
'types' => array('warning', 'error', 'critical', 'alert', 'emergency'),
// restrict to scope=type only
'scopes' => array('warning', 'error', 'critical', 'alert', 'emergency'),
'file' => 'error',
));
Cake does have a logging safety net so that if a log message has been filtered out completely (by what is effectively, bad config) it will at least be written to the default logger. So a call like this:
$this->log('special', 'foo');
will still be written to the foo log (with default config).
A debug log without all messages isn't a debug log
As answered by Vadim - it's not a bad idea to have a debug log with all log messages in it. This permits grepping a single file for information, instead of needing to look in several different files when things aren't quite going to plan in your application.
You can use CakeLog::disable('debug') and CakeLog::disable('error') to disable these two default log streams temporarily before CakeLog::write to 'soap'.
And don't forget to immediately restore by using CakeLog::enable('debug') and CakeLog::enable('error') after 'soap' log was written.
Sample:
CakeLog::debug('THIS MESSAGE WILL APPEAR IN debug.log and soap.log', 'soap');
CakeLog::disable('debug');
CakeLog::debug('THIS MESSAGE WILL NOT APPEAR IN debug.log but soap.log only', 'soap');
CakeLog::enable('debug');
CakeLog::debug('THIS MESSAGE WILL APPEAR IN debug.log and soap.log', 'soap');
CakeLog::disable
I am quite new to cakephp and I'm having trouble getting it configured to work on my live server. It works fine on my local machine.
I think the problem is that my live server is configured to use Memcache. When I visit the live site I get:
Warning (2): session_start() [function.session-start]: open(=1&retry;_interval=15/sess_mt8tpui04vorqojg7s945e5sf5, O_RDWR) failed: No such file or directory (2) [CORE/Cake/Model/Datasource/CakeSession.php, line 615]
Warning (2): session_write_close() [function.session-write-close]: open(=1&retry;_interval=15/sess_mt8tpui04vorqojg7s945e5sf5, O_RDWR) failed: No such file or directory (2) [CORE/Cake/Controller/Controller.php, line 712]
Warning (2): session_write_close() [function.session-write-close]: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (tcp://127.0.0.1:11211?persistent=1&weight;=1&timeout;=1&retry;_interval=15) [CORE/Cake/Controller/Controller.php, line 712]
So i've tried enabling cake to use memcache by adding the following to app/Config/core.php:
Cache::config('default', array(
'engine' => 'Memcache'
));
But I still get the same error.
The php.ini is configured to use memcache correctly.
Any ideas?
Thanks
Your Cache::config looks incomplete!
It should look like this and This code block will be in app/Config/bootstrap.php
Cache::config('default', array(
'engine' => 'Memcache', //[required]
'duration' => 3600, //[optional]
'probability' => 100, //[optional]
'prefix' => Inflector::slug(APP_DIR) . '_', //[optional] prefix every cache file with this string
'servers' => array(
'127.0.0.1:11211' // localhost, default port 11211
), //[optional]
'persistent' => true, // [optional] set this to false for non-persistent connections
'compress' => false, // [optional] compress data in Memcache (slower, but uses less memory)
));
Also you need to set a session handler http://book.cakephp.org/2.0/en/development/sessions.html#cache-sessions
Mine looks like this, note that I have called "sessiones"
& This code block will be in app/Config/core.php
Configure::write('Session', array(
'defaults' => 'cache',
'handler' => array(
'config' => 'sessiones'
),
'cookie' => 'PHPSESSID',
'timeout' => 3600,
'cookieTimeout' => 0,
'autoRegenerate' => false,
'checkAgent' => true,
'ini' => array(
'session.cookie_secure' => false,
'session.cookie_httponly' => true,
)
));
And then set up the Cache:config for the handler "sessiones"
and This code block will be in app/Config/bootstrap.php
Cache::config('sessiones', array('engine' => 'Memcache','duration'=> 3600,/*'prefix' =>'es',*/ 'servers' => array(array('127.0.0.1:11211'), 'compress' => false));