PHP Cache Driver - it is very fast

Similar to my cache drivers for sqlite or redis I recently created a cache driver that stores and reads from php files. That means your cache will be compiled into your app and be as fast as it possible can. but you need to make sure you do not run out if memory if caching to much. It’s a sharp tool.

$cache = \Bnomei\PHPCache::singleton(); // or
$cache = elephant();

$cache->set('key', 'value', $expireInMinutes);
$value = elephant()->get('key', $default);

elephant()->remove('key');
elephant()->flush();

you can also use it for my boost and lapse plugins.
site/config/config.php

<?php
return [
    'bnomei.lapse.cache' => ['type' => 'php'],
    'bnomei.boost.cache' => ['type' => 'php'],
    //... other options
];

it got two modes, both load all data on every request but…

  1. mono file (default): writes one big file at destruct. well suited for many changes at once or when changes happen less often. This mode is NOT suited for concurrent requests but once all content in cache is more or less static.

  2. lots of files: writes each item on set to a seperate file. which is good if you have very few but often changes in cache between two requests and writing speed is important.

my boost demo which is loading 38k cached content files shows it speed very well. the server is sponsored by @kirbyzone and has been configured with enough php script memory to fit all data loaded by the php driver.

https://kirby3-boost-php.bnomei.com/

3235ms: null cache driver (pure kirby)
1909ms: sqlite cache driver
1472ms: redis cache driver with pipeline preloading enabled
1482ms: apcu cache driver
1240ms: php cache driver (a 26MB php file to be "included" at compile time)
3 Likes

thanks to @waffl i was able to squash some opcache related bugs that occurred when HTTP requests where more often than opcache.revalidate_freq or if opcache.validate_timestamp was disabled.

That is btw the same issue/error you wille experience if you edit a php file live on a server, like setting debug mode in you config.php but the server just does not seem to apply the changes or only after a few seconds/minutes.

1 Like