I have a factory method class that returns a cache system class (pseudo code):
class CacheFactory
{
public static function get($type) {
switch ($type) {
case 'memcache':
return new Memcache();
case 'redis':
return new Redis();
case 'default':
return new Void();
}
}
}
The cache classes implements simple get() and set() methods (it uses an abstract class defining the common methods) that allows me to easily switch cache systems if needed. The normal use will be like:
$cache = CacheFactory::get('redis');
$value = $cache->get('key');
...etc
I want to have a setting to enable/disable the cache, but I don't want to add conditionals in the code asking if the cache is enabled or not everywhere. So I was thinking in returning a Void() object that implements the abstract class methods so it will be used when the cache is disabled, the class will look like this:
class Void extends ACache
{
public function get(){};
public function set(){};
}
Would this be a good approach? How would you think will be the best way to handle the enabled/disabled setting without adding conditionals in the actual implementation?
Thanks!