Subscribe to RSS Subscribe to Comments

Alan Blaine Whitney

Cakephp Caching

Recently I was working on an application that needed to have many instances.  That is, one code base in one place that served several sites.  These sites have seperate dbs, seperate configs, seperate views, seperate caches per domain.

So I needed these seperate configs for various sites.  I found this article on the bakery, that seemed to be a good start for me.  That got me so that I had a seperate config file (including db) for my different sites.

While I had seperate configs for each domain, I took the chance to set up cake themes.  So in my config this in my domain config

Configure::write('theme', 'client');

and then I set that in my app_controller in beforeFilter

$theme = Configure::read('theme');
if (isset($theme) && strlen($theme)) {
  $this->view = 'theme';
  $this->theme = $theme;
}

This will get my app so I have separate views inside of app/views/themed/client/layouts/default.ctp and so on.

This worked pretty good, but there was a problem, view caching and db caching was shared across the various sites.  Well, cake puts all of it’s writable stuff in /app/tmp and what I wanted to do was move all of that stuff into per domain directories.  I decided on my systems tmp directory, which on Linux is /tmp, so I made several directories within that one.

/tmp/broadcaster
/tmp/broadcaster/<client-name>
/tmp/broadcaster/<client-name>/cache
/tmp/broadcaster/<client-name>/cache/views
/tmp/broadcaster/<client-name>/cache/persistent
/tmp/broadcaster/<client-name>/cache/models
/tmp/broadcaster/<client-name>/logs
/tmp/broadcaster/<client-name>/sessions
/tmp/broadcaster/<client-name>/tests

So more of less, what I have here is a copy of of app/tmp for every domain.  Now we need to tell cake to use these directories.

Here is my chances in app/webroot/index.php,  right after app_dir.

define('TMP', '/tmp/broadcaster/' . $_SERVER['SERVER_NAME'] . '/');

Here is the bottom of my app/config/core.php

Cache::config('default',
  array(
    'engine' => 'File',
    'prefix' => 'cake_',
    'path' => TMP . '/cache/',
  )
);

So that’s a rough over view of per domain stuff in cake.

Based on FluidityTheme Redesigned by Kaushal Sheth Sponsored by Send Flowers