Documentation Tools
Cache
File cache with TTL and a RAM drive for maximum speed.
How the cache works
The olala\modules\cache module stores values in JSON files in the cache/ folder in the site root.
Every value has a lifetime. Values that have been read are also kept in memory,
so a repeated cache::get() within the same request doesn't touch the disk at all.
Before you start, create the folder and give the web server write access:
mkdir cache
sudo chown www-data cacheMain methods
| Method | What it does |
|---|---|
cache::get($key, $default = null) | the value for the key, or $default if it's missing or expired |
cache::set($key, $value, $time = 86400, $prolong = true) | stores the value for $time seconds |
cache::flush($key) | deletes a single key |
cache::flushDir($dir) | deletes a group of keys $dir/… |
cache::flushAll() | clears the whole cache |
A typical scenario
Check the cache; on a miss, compute and store:
use olala\modules\cache;
use olala\modules\db;
function popularPosts(): array {
$posts = cache::get('blog/popular');
if (!$posts) {
$posts = db::getAllSQL('select id, title from blog.posts order by views desc limit 5');
cache::set('blog/popular', $posts, 600); // 10 minutes
}
return $posts;
}Key groups
A slash in a key creates a subfolder: blog/popular is stored in cache/blog/popular.json.
This lets you reset related values all at once:
cache::set('blog/popular', $popular);
cache::set('blog/post/15', $post);
cache::flushDir('blog'); // after posts change — reset everything related to the blogProlonging
By default every set() restarts the lifetime. If you pass $prolong = false,
the value is updated but expires at the time set by the first save — handy for counters
and “no more than N times per hour” limits.
Empty values aren't cached. get() treats 0, '', false and an empty array as missing.
If “nothing found” is also a result worth remembering, store it in a wrapper: ['value' => 0].
In-memory cache (RAM drive)
The cache/ folder can be mounted as a RAM drive (tmpfs) on any Unix system.
The cache files then live in RAM instead of on the storage device. This gives you two benefits:
- maximum speed — cache reads and writes run at memory speed, with no waiting for the disk;
- less wear on the disk — constant small rewrites of cache files no longer load the HDD or SSD, so its lifespan is used up more slowly.
No changes to the site code are needed: the cache module still works with the cache/ folder, which now simply lives in memory.
Setting it up with one command
The framework includes a helper script for mounting. Run it from the site root and specify the drive size:
./create ramdrive <size>
./create ramdrive 64M # 64 megabytes
./create ramdrive 1G # 1 gigabyte
The command creates the cache/ folder if it doesn't exist yet and runs
sudo mount -t tmpfs -o size=<size> tmpfs ./cache, so you'll need sudo rights.
After a successful mount you'll see the message './cache' dir mounted as RAMDRIVE.
To check that the drive is mounted:
df -h cache
# Filesystem Size Used Avail Use% Mounted on
# tmpfs 64M 1.2M 63M 2% /var/www/mysite/cacheAutomatically at boot
A mount made with ./create lasts until the server reboots. To bring the RAM drive up automatically, add a line to /etc/fstab:
tmpfs /var/www/mysite/cache tmpfs size=128M,mode=0777 0 0Things to keep in mind
- The contents don't survive a reboot. That's fine for a cache: values will simply be recomputed.
Don't keep anything in
cache/that you can't afford to lose. - The size is limited. When space runs out, new values stop being saved. 64–256 MB is enough for most sites. Memory is used only by files actually written, not by the whole declared size.
- The
tmpfssyntax used by./create ramdriveworks on Linux and FreeBSD. On other Unix systems (macOS, for example) create the RAM drive with the system's own tools and mount it to the samecache/folder.
Security
Cache files live inside the site root, so block direct access to the folder — in .htaccess:
RewriteRule ^cache/ - [F]For nginx, see Deployment.