Allow cache to be extended with additional drivers
nblackburn opened this issue · comments
Cache doesn't look like it can be extended in the same was as thumb
and email
, it would be nice if it could so additional cache drivers could be implemented without hacking the core.
See #229
You can just define the cache driver class in a plugin. If it's in the correct namespace, it will be picked up automatically.
@lukasbestle What would that namespace be as i have tried a bunch of things and can't seem to get it to work.
Well, Cache\Driver
. :)
@lukasbestle Yeah, that doesn't work because when i come to setting the option of c::set('cache.driver', 'redisdriver')
then it throw an error because it doesn't exist.
site/plugins/redisdriver/redisdriver.php
<?php
namespace Cache\Driver;
use Cache\Driver;
use Predis\Client;
/**
* Redis
*
* @package Kirby Toolkit
* @author Bastian Allgeier <bastian@getkirby.com>
* @link http://getkirby.com
* @copyright Bastian Allgeier
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
class RedisDriver extends Driver {
site/config/config.php
c::set('debug', true);
c::set('cache', true);
c::set('cache.driver', 'RedisDriver');
Fatal error: Uncaught ReflectionException: Class Cache\Driver\RedisDriver does not exist in C:\Users\nathan\Desktop\website\kirby\vendor\getkirby\toolkit\lib\cache.php:31 Stack trace: #0 C:\Users\nathan\Desktop\website\kirby\vendor\getkirby\toolkit\lib\cache.php(31): ReflectionClass->__construct('Cache\\Driver\\Re...') #1 C:\Users\nathan\Desktop\website\kirby\kirby.php(560): Cache::setup('RedisDriver', Array) #2 C:\Users\nathan\Desktop\website\kirby\kirby.php(534): Kirby->cache() #3 C:\Users\nathan\Desktop\website\panel\app\src\panel.php(260): Kirby->site() #4 C:\Users\nathan\Desktop\website\panel\app\src\panel.php(100): Kirby\Panel->site() #5 C:\Users\nathan\Desktop\website\panel\index.php(41): Kirby\Panel->__construct(Object(Kirby), 'C:\\Users\\nathan...') #6 {main} thrown in C:\Users\nathan\Desktop\website\kirby\vendor\getkirby\toolkit\lib\cache.php on line 31
You are right, this is a loading order issue.
Kirby first initializes the cache as some plugins need the cache to work. But if the cache driver is defined inside a plugin, the cache can't be initialized without first loading the plugin.
As you can see, we can't really fix this as loading plugins before initializing the cache would break the plugins that depend on the cache.
You can however load your cache driver from a custom site.php
file, it will then be available before Kirby needs the class.
Another thing: You call your redis driver class RedisDriver
. The other drivers are just called like Redis
. But this is not related to this issue.
@lukasbestle Kind of defeats the point of being a plugin if it requires a lot of work from the user.
The naming issue you mentioned was because the file was called redisdriver.php
so wanted to keep the class the same as it is no longer part of the framework as in my original PR.
Yes, I agree with you that this does not really make sense as a plugin. I didn't know that this won't work before you tested it. :)
But as I outlined above, there is just no other way.
@lukasbestle Shame, i feel this is best in the core but understand the dependency is a problem.