SilverStripe cache directoryPosted Mar. 13, 2010 in SilverStripe -
SilverStripe by default creates a directory for caching the site manifest etc by using the PHP function getSysTempDir(). It is possible to locate this directory elsewhere by simply creating a directory called silverstripe-cache in the website's root directory, or at any other location by setting the TEMP_FOLDER constant.
Where is it set?
While having a problem with command line batch scripts (which I will cover in a later post) I couldn't find anywhere in the online manual which covers how to control where to locate the SilverStripe cache directory. I eventually found where it is set by looking through the source code.
The file sapphire/core/Core.php has a function named getTempFolder() which sets the temporary folder.
What is the default?
The default is to combine the value from getSysTempDir() with the full path to the root directory of the website, with the directory separators changed to hyphens. On a *nix box this would typcially be located in /tmp and would look something like this:
if the website root was /var/www/mywebsite.
Changing the directory to be within the website's root
There may be reasons you do not want to have the cache directory in the system's temporary directory. One issue with it being in /tmp is that whenever the server is restarted this directory will be deleted removing any permissions that might be set on it (again, I'll be looking at issues with this is a later post).
Some servers also have periodic processes which purge the contents of the /tmp directory. Although the cache directory will be rebuilt as and when required it's better to avoid this if possible as it can take a few seconds to build a new cache.
Create a directory under the website's root called silverstripe-cache and change the permissions so it can be written by anyone, and SilverStripe will use this instead of using getSysTempDir(). On a *nix box, run these commands from the website's root:
mkdir silverstripe-cache chmod 0777 silverstripe-cache
Alternatively you could do it using an FTP client like FileZilla by creating the directory and then setting the permissions from within the application.
Changing the directory to be somewhere else
SilverStripe sets the constant TEMP_FOLDER to the value returned from getTempFolder(), but only if it has not already been defined. This means that you can set it yourself first.
However, you cannot set it in mysite/_config.php because by the time this file is included the constant will have already been set.
Instead, create a file called _ss_environment.php in your website's root folder (or edit it if you already have one) and define it like so:
Now you can make the cache directory anywhere you want, should you need to.