Answer the question
In order to leave comments, you need to log in
CacheProvider, what standards and practices to use when writing in php?
CacheProvider, what standards and practices to use when writing in php?
Actually, I conceived the cache provider only as a class of a single caching API, but the question arose, how to specify a driver for working with the cache?
At the moment I have this:
<?php
$cache = new CacheProvider(CacheProvider::DRIVER_REDIS, $configRedis);
$cache->set('key', 'value');
?>
const DRIVER_REDIS = 0;
const DRIVER_MEMCADHED = 1;
const DRIVER_FILE = 2;
<?php
$config = array(
'drivers' => array(
'0' => array(
'name' => 'RedisDriver',
'lib' => '\Architect\Cache\Drivers\RedisDriver'
),
'1' => array(
'name' => 'MemcachedDriver',
'lib' => '\Architect\Cache\Drivers\FileDriver'
),
'2' => array(
'name' => 'FileDriver',
'lib' => '\Architect\Cache\Drivers\FileDriver'
)
)
);
?>
$cache = CacheProvider::driver('REDIS');
$cache->set('key', 'value');
$cache = new CacheProvider('REDIS', $driverConfig);
Answer the question
In order to leave comments, you need to log in
It's better to create a concrete instance of a class like RedisCacheProvider that implements a common interface like CacheProviderInterface.
You can peek here: https://github.com/doctrine/cache
Also, I would put the creation of objects in a DI container. And read about dependency inversion (if necessary).
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question