Cakephp Caching Configuration for ApcCache and Memcache - cakephp

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.

Related

CakePHP session refreshes at each request

I'm working on my first app w/ CakePHP 2.3 and I'm having an issue where I can login (no auth errors), but my session isn't sticking around so I'm sent back to the login page when the Auth->redirect() is called. I'm sure I'm just missing a setting or have something configured slightly wrong, but I haven't been able to find it.
# core.php
# session record is written the the database, but the same record's id changes w/ every request
Configure::write('Session', array(
'defaults' => 'database',
));
Configure::write('Security.level', 'medium');
I've tried tweaking the various Session.X parameters, but nothing has made any difference. I'm using bcrypt authentication with the following settings in my AppController:
'Auth' => array(
'authenticate' => array(
'Blowfish' => array(
'fields' => array( 'username' => 'email' ),
'scope' => array( 'active' => '1' )
),
),
'authorize' => array( 'Controller' ),
'loginAction' => array( 'admin' => false, 'controller' => 'users', 'action' => 'login' ),
'loginRedirect' => array( 'admin' => true, 'controller' => 'activities', 'action' => 'index' ),
'logoutRedirect' => array( 'admin' => false, 'controller' => 'users', 'action' => 'login' ),
),
What piece am I missing?
UPDATE
Realizing that this is only happening in my dev environment, I compared my Cake config (database, core, bootstrap) and php.ini values -- no differences. I'm stumped.
Holy Headslap, Batman.
So here's the issue. I'm storing sessions in the database. Somewhere, somehow, an (obviously) automated process changed the cake_sessions.data field to cake_sessions.DATA. Although I've looked at the database a thousand times while debugging this, I just noticed that difference.
Problem solved.
Moral of the story: Developers, don't let your database field names grow up and change case.
You need to set 'Session' as a component too.

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

Using SQLite3 with CakePHP 2.0

I'm trying to run SQLite3 with CakePHP 2.0
In these questions I saw that it's possible to do that in CakePHP 1.3:
- Using Sqlite3 with CakePHP
- How do I connect CakePHP to a SQLite database?
However, the solutions are not valid for CakePHP 2.0.
I configured the file 'database.php' and I got success on the starting page of CakePHP. It was able to connect to the database (but I do not know where to find the .db3 database file).
I used the following code:
public $default = array(
'datasource' => 'Database/Sqlite',
'persistent' => false,
'host' => 'localhost',
'login' => '',
'password' => '',
'database' => 'cake_blog_tutorial',
'prefix' => '',
//'encoding' => 'utf8',
);
I'm trying to find out:
Where should my cake_blog_tutorial.db3 file be kept
Is the datasource different for SQLite3, for example 'Database/Sqlite3'?
Thank you for your help!
In short, the answer is that Sqlite3 databases in CakePHP 2.0 take something like the following configuration:
public $default = array(
'datasource' => 'Database/Sqlite',
'persistent' => false,
'database' => 'database_name',
'prefix' => '',
//'encoding' => 'utf8',
);
The sqlite file is then automatically created in the webroot directory (unless you prepend a relative path to the database name).
Incidentally, you can use in-memory Sqlite databases (for testing purposes, for example) by changing the database name to ":memory:", e.g.:
public $default = array(
'datasource' => 'Database/Sqlite',
'persistent' => false,
'database' => ':memory:',
'prefix' => '',
//'encoding' => 'utf8',
);

cakephp - use memcache for sessions

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

Changing view caching config in CakePHP

I am working on a project in cakephp where one app will support multiple sites using different domains. This is an issue when using view caching where it only matches to the end part of the url and ignores the host.
Is there a way to prefix view caching with the host so that there isn't any conflict across different sites?
You can use a different cache configuration for each domain, like this:
app/config/core.php
switch(#$_SERVER['SERVER_NAME']) {
case 'example.com':
Cache::config('default', array(
'engine' => 'File',
'prefix' => 'example_com_'
));
break;
case 'example2.com':
Cache::config('default', array(
'engine' => 'File',
'prefix' => 'example2_com_'
));
break;
default:
Cache::config('default', array(
'engine' => 'File',
'prefix' => 'default_'
));
break;
}

Resources