cakephp - use memcache for sessions - cakephp

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));

Related

CakePHP 3 ORM : Cache metadata issue

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

MIME type error

With Cakephp 2.6 I have the following warning:
Can not determine the mimetype.
Error: An Internal Error Has Occurred.
Below is my Model:
'mimeType' => array(
'rule' => array('mimeType', array('image/gif', 'image/png', 'image/jpg', 'image/jpeg')),
'message' => 'Please only upload images (gif, png, jpg).',
'allowEmpty' => FALSE,
),
In php.ini (\xampp\php\php.ini) I uncommented
extension=php_fileinfo.dll
But I still have the same error message.
There is no line number in the error message even if my debug is set to 2. the only error message displaying is :
Can not determine the mimetype.Error: An Internal Error Has Occurred.
for the stack trace, the value of 'Validation::mimeType(array, array)' is
array(
'name' => '2009_Infini-jaune-2.jpg',
'type' => 'image/jpeg',
'tmp_name' => 'C:\xampp\tmp\phpEC6C.tmp',
'error' => (int) 0,
'size' => (int) 186994)
array(
(int) 0 => 'image/png',
(int) 1 => 'image/jpeg'
)`
When enabling or disabling extensions, be sure to restart the web server (or PHP if you are running PHP as a FastCGI instance).
Change the text ;extension=php_fileinfo.dll into extension=php_fileinfo.dll on php.ini
It works for me. Hope it helps you guys. I'm using xampp.
Don't forget to restart the web server after that.

Cakephp Caching Configuration for ApcCache and Memcache

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.

Ocasional error warning when CakePHP is writing to the cache

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
],
],

unload() CakePHP Uploader Plugin does not seem to work

I'm using CakePHP 2.1.1 and Miles Johnson's Uploader Plugin v 3.5.
It's working pretty sweet although I think I have a problem when trying to unload the FileValidation behavior.
I have set up both the behaviors Uploader.Attachment and Uploader.FileValidator (see the bottom of the question).
In the afterSave callback I now need to save the Post again to add a Translated field for a different locale.
When I do save again, this seems to cause en error in the FileValidation behavior. I get the error:
failed to open stream: No such file or directory [APP/Plugin/Uploader/Model/Behavior/FileValidationBehavior.php, line 296]
Somehow the Behavior is looking again for the tmp file.
When I do not define the FileValidation behavior at all, all goes well. So I figured to disable the behavior after it has done it's job during regular save(), right before I go for my second save().
In afterSave($created) I therefore state
$this->Behaviors->unload('FileValidation');
$this->save($data);
The error disappears, but I get 4 warnings in return:
Warning (512): Could not find validation handler maxWidth for file [CORE/Cake/Model/Model.php, line 3155]
Warning (512): Could not find validation handler maxHeight for file [CORE/Cake/Model/Model.php, line 3155]
Warning (512): Could not find validation handler filesize for file [CORE/Cake/Model/Model.php, line 3155]
Warning (512): Could not find validation handler required for file [CORE/Cake/Model/Model.php, line 3155]
I also tried $this->Behaviors->disable('FileValidation') but to no avail.
Is this a bug in the Behavior (not properly unloading itself) or am I not properly handling the unload?
kind regards,
Bart
The behavior setup:
public $actsAs = array('Uploader.Attachment' => array(
'file' => array(
'name' => 'uniqueFilename', // Name of the function to use to format filenames
'baseDir' => APP, // See UploaderComponent::$baseDir
'uploadDir' => 'webroot/img/upload/', // See UploaderComponent::$uploadDir
'dbColumn' => 'uploadPath', // The database column name to save the path to
'importFrom' => '', // Path or URL to import file
'defaultPath' => '', // Default file path if no upload present
'maxNameLength' => 30, // Max file name length
'overwrite' => false, // Overwrite file with same name if it exists
'stopSave' => true, // Stop the model save() if upload fails
'transforms' => array(), // What transformations to do on images: scale, resize, etc
's3' => array(), // Array of Amazon S3 settings
'metaColumns' => array( // Mapping of meta data to database fields
'ext' => 'ext',
'type' => 'type',
'size' => 'size',
'group' => 'group',
'width' => 'width',
'height' => 'height',
'filesize' => 'size',
'name'=>'name'
)
)
),
'Uploader.FileValidation' => array(
'file' => array(
'maxWidth' => array(
'value' => 1000,
'error' => 'Image too wide. Max 1000px'
),
'maxHeight' => array(
'value' => 1000,
'error' => 'Image too high. Max 1000px'
),
'extension' => array(
'value' => array('gif', 'jpg', 'png', 'jpeg'),
'error' => 'Mimetype incorrect',
),
'filesize' => array(
'value' => 1048576,
'error' => 'Filesize too high. Max 1 MB'
)
)
)
);
Not directly related, but are you game to try another file upload plugin? This one might do the trick: http://bakery.cakephp.org/articles/srs2012/2012/03/12/ajaxmultiupload_plugin_for_cake_2_0_x_and_2_1
Not sure if this will fix yours but I found (for some reason I don't know why) this error gets thrown if you don't have debug set to 0 in your Config/core.php

Resources