Choose your SilverStripe cache location carefully

Let me begin today's lesson in the world of SilverStripe with an explanation of my usage. This site is currently hosted on a shared host, it is cheap, has all I need and I am not expecting a high volume of viewers at any one time. This suits my usage perfectly. In fact, any of my smaller work tends to be on shared hosts. The problem I have had with shared hosts is that you never quite know how they are configured until you have access to them. Some use PHP 5.3, others 5.2 but allow you to choose a different version, some even more rubbish ones are still back in the stone age of PHP 4. Even then, some are missing various Apache modules that are just, what I call, must haves and others, dare I say it use Microsoft IIs, which is a pile of stinky turd. All that said, I found that the host I am on, works with SilverStripe 3 just perfectly ... or so I thought.

So I have been fiddling with my site, making some adjustments so it handles mobile and handheld screens much better and some much appreciated feedback from friends. This has meant some CSS changes and occasionally adjustments to the templates. Provided I am logged in, a simple flush and all is fine. Until the other night ...

The weirdness begins

So I am putting together a small thought on the over dependence of Javascript I have noticed and my particular hate of when Javascript blurs into the world of layout and look of a  website and the dangers of doing it. I transfer a few screenshots of the images via FTP to my assets directory and head over to the CMS admin Files area to synch up and it is just comes up with a white screen. I hit Firebug to see what the request has sent back, empty JSON. I go directly to the admin/files URL and all that is loaded is the left menu. Hang on, I haven't changed any templates to do with the administration! So I use the flush, flush=1, flush=all parameters ... go to the toilet, flush that ... oh, ok, it loaded.

Then I am editting the article and some of the popups from the TinyMCE editor are very slow to load, if at all. The links button brings up an nearly empty popup. The images button has errors when I paginate between images in a folder. Luckily at this point I notice that the error coming back is to do with a template located in the host's temporary directory, the default location SilverStripe will put it's caches for manifest, templates and the like. Something is not right at that location.

At the mercy of shared locations

I remember that you can explicitly set the location of the silverstripe cache by simply creating a directory called silverstripe-cache at the same level as the modules directories. I do that, flush=all and it builds beautifully into this new local silverstripe-cache. All problems have seemed to have fixed, in fact, performance of the site seems to have gotten better. What the deuce was happening into the global temporary directory? I haven't a clue, I can't access it, and that is the problem!

So where is the mythical system temporary directory? Was it full? Was some other website spewing rubbish it? Was it split over on another virtual machine and some weirdness of previous versions being used? I haven't a clue and honestly, I don't care.

Lesson of the story

If you are on a shared host, explicitly set your cache directory by placing an empty directory at the same level as your modules. Ensure it is writable to the same user that your website will run as. The htaccess file, provided you haven't tampered with it, has protection to stop direct access to it:

<IfModule mod_alias.c>
    RedirectMatch 403 /silverstripe-cache(/|$)
</IfModule>

 You can also explicitly define the location in your _ss_environment.php file:

     define('TEMP_FOLDER', '/path/to/cache/directory');

Either way you do it, I suggest doing it when using a shared host to avoid being subject to whims of the system temporary directory. May save you a lot of swearing and WTF moments.