Doctrine Cache Service Provider
The Doctrine Cache Service Provider add Doctrine Cache package for Silex Microframework or every Pimple Container project's., (*1)
Cache Drivers
Doctrine Cache have some cache drivers, for a complete list and updated you can check here., (*2)
Parameters
cache.profiles
In this array you can configure every parameters for the Service Provider., (*3)
For more informations about every cache driver options check below in this section., (*4)
Services
Here the list of every services used in this Service Provider., (*5)
cache
The Doctrine Cache driver implemented with Doctrine\Common\Cache\Cache. The main way to interact with the Service Provider., (*6)
This service use the FIRST profile available so take attention when you register the profiles for the order how you register., (*7)
cache.profiles.initializer
This service initialize the default profile for the Service Provider., (*8)
cache.stores
This service use multiple profiles (_example for use different servers for different environments_), you can manage with this example:, (*9)
// Register the service provider with multiple connections
$app->register(new \NunoPress\Silex\Provider\DoctrineCacheServiceProvider(), [
'cache.profiles' => [
'local' => [
'driver' => 'xcache'
],
'remote' => [
'driver' => 'array',
'namespace' => 'test'
],
'test' => [
'driver' => 'filesystem',
'parameters' => [
'directory' => '/cache'
]
]
]
]);
// Access to different profiles
$app['cache.stores']['local']->fetch('cache_key');
// or with DoctrineCacheTrait
$app->cache('local')->fetch('cache_key');
cache.store.filesystem
Return the Doctrine\Common\Cache\FilesystemCache object., (*10)
cache.store.array
Return the Doctrine\Common\Cache\ArrayCache object., (*11)
cache.store.apcu
Return the Doctrine\Common\Cache\ApcuCache object., (*12)
cache.store.mongodb
Return the Doctrine\Common\Cache\MongoDBCache object., (*13)
cache.mongodb.connector
return the MongoCollection object after connected to MongoDB server., (*14)
cache.store.redis
Return the Doctrine\Common\Cache\RedisCache object., (*15)
cache.redis.connector
Return the Redis object after connected to Redis server., (*16)
cache.store.xcache
Return the Doctrine\Common\Cache\XcacheCache object., (*17)
cache.store.chain
Return the Doctrine\Common\Cache\ChainCache object., (*18)
cache.store.memcache
Return the Doctrine\Common\Cache\MemcacheCache object., (*19)
cache.memcache.connector
Return the Memcache object after connected to Memcache server., (*20)
cache.store.memcached
Return the Doctrine\Common\Cache\MemcachedCache object., (*21)
cache.memcached.connector
Return the Memcached object after connected to Memcached server., (*22)
cache.store.couchbase
Return the Doctrine\Common\Cache\CouchbaseCache object., (*23)
cache.couchbase.connector (need testing)
Return the Couchbase object after connected to Couchbase server., (*24)
cache.store.phpfile
Return the Doctrine\Common\Cache\PhpFileCache object., (*25)
cache.store.predis
Return the Doctrine\Common\Cache\PredisCache object., (*26)
cache.predis.connector (need testing)
Return the Predis\Client object after connected to Predis server., (*27)
cache.store.riak
Return the Doctrine\Common\Cache\RiakCache object., (*28)
cache.riak.connector (need testing)
Return the Riak\Bucket object after connected to Riak server., (*29)
cache.store.sqlite3
Return the Doctrine\Common\Cache\Sqlite3Cache object., (*30)
cache.sqlite3.connector
Return the Sqlite3 object after connected to Sqlite3., (*31)
cache.store.void
Return the Doctrine\Common\Cache\VoidCache object., (*32)
cache.store.wincache
Return the Doctrine\Common\Cache\WinCacheCache object., (*33)
cache.store.zenddata
Return the Doctrine\Common\Cache\ZendDataCache object., (*34)
cache.store.pdo
Return the NunoPress\Doctrine\Common\Cache\PDOCache object., (*35)
cache.pdo.connector
Return the PDO object after connected to PDO server., (*36)
cache.factory
This service used to choice the right cache driver., (*37)
cache.default_options
Simple array with defined the default options. This the default options:, (*38)
$app['cache.default_options'] = [
'driver' => 'array',
'namespace' => null,
'parameters' => []
];
Not all connectors are tested, so please be careful and send any issue with that., (*39)
Registering
$app->register(new NunoPress\Silex\Provider\DoctrineCacheServiceProvider(), [
'cache.profiles' => [
'default' => [
'driver' => 'array'
]
]
]);
If you need more connections you can define more arrays following this example:, (*40)
$app->register(new NunoPress\Silex\Provider\DoctrineCacheServiceProvider(), [
'cache.profiles' => [
'default' => [
'driver' => 'array'
],
'local' => [
'driver' => 'filesystem',
'parameters' => [
'cache_dir' => '/cache'
]
]
]
]);
Cache Driver Options
Now the list of required parameters for every cache driver:, (*41)
filesystem
cache_dir
Directory where the Service Provider save the cache., (*42)
array
No configuration., (*43)
apcu
No Configuration., (*44)
mongodb
server
MongoDB server address., (*45)
name
MongoDB name., (*46)
collection
MongoDB collection., (*47)
Redis
host
Redis server address., (*48)
port
Redis server port., (*49)
password (optional)
Password for access to Redis server., (*50)
xcache
No configuration., (*51)
chain
Register the cache drivers to use the Chain Cache, example:, (*52)
$app->register(new NunoPress\Silex\Provider\DoctrineCacheServiceProvider(), [
'cache.profiles' => [
'default' => [
'driver' => 'chain',
'parameters' => [
[
'driver' => 'filesystem',
'parameters' => [
'cache_dir' => __DIR__ . '/../cache'
]
],
[
'driver' => 'array'
]
]
]
]
]);
This system to configure the Service Provider is under development, some modifications can change in the next release., (*53)
memcache
host
Memcache server address., (*54)
port
Memcache server port., (*55)
memcached
host
Memcached server address., (*56)
port
Memcached server port., (*57)
couchbase
host
Couchbase server address., (*58)
port
Couchbase server port., (*59)
username (optional)
Username for access to Couchbase server., (*60)
password (optional)
Password for access to Couchbase server., (*61)
bucket (optional)
Bucket name (_default is the default name for the bucket_)., (*62)
phpfile
directory
Directory where the cache files are saved., (*63)
extension (optional)
Extension for the cache files., (*64)
umask (optional)
Umask for the cache files., (*65)
predis
scheme
Use tcp or socket for the Predis connection., (*66)
host
Predis server address., (*67)
port
Predis server port., (*68)
path
Use this instead of host and port for socket scheme., (*69)
riak
host
Riak server address., (*70)
port
Riak server port., (*71)
bucket
Riak bucket name., (*72)
sqlite3
filename
Sqlite3 filename complete with path., (*73)
table
Sqlite3 table name., (*74)
flags (optional)
Sqlite3 flags options., (*75)
encryption_key (optional)
Sqlite3 encryption key., (*76)
void
No configuration., (*77)
wincache
No configuration., (*78)
zenddata
No configuration., (*79)
pdo
dns
For more information's about the format see here., (*80)
username (optional)
Username for connect to PDO., (*81)
password (optional)
Password for connect to PDO., (*82)
options (optional)
Options for connect to PDO., (*83)
Usage
The Config provider provides a config service:, (*84)
// Read cache
$app['cache']->fetch('cache_key');
// Check cache
$app['cache']->contains('cache_key');
// Save cache
$app['cache']->save('cache_key', 'cache_value', 100); // the third param is a lifetime in seconds.
// Delete cache
$app['cache']->delete('cache_key');
Read the reference for all methods available., (*85)
Traits
Define this trait in your Application class:, (*86)
class App extends \Silex\Application
{
use \NunoPress\Silex\Application\DoctrineCacheTrait;
}
$app = new App();
$name = $app->readCache('cache_key');
NunoPress\Silex\Application\DoctrineCacheTrait adds the following shortcuts:, (*87)
readCache
// Read cache
$app['cache']->fetch('cache_key');
containsCache
// Check cache
$app['cache']->contains('cache_key');
saveCache
// Save cache
$app['cache']->save('cache_key', 'cache_value', 100); // the third param is a lifetime in seconds.
deleteCache
// Delete cache
$app['cache']->delete('cache_key');
cache
// More readable with DoctrineCacheTrait: $app->cache('profile_name')->contains('cache_key');
$app['cache.stores']['profile_name']->contains('cache_key');
Customization
DoctrineCacheTrait
Our developers used a personal vision for the cache method, this our implementation:, (*88)
namespace App\Traits;
/**
* Class DoctrineCacheTrait
* @package App\Traits
*/
trait DoctrineCacheTrait
{
use \NunoPress\Silex\Application\DoctrineCacheTrait;
/**
* @param null $profile
* @return \Doctrine\Common\Cache\Cache
*/
public function cache($profile = null)
{
$profile = $profile ?: $this['environment'];
return $this['cache.stores'][$profile];
}
}
With this implementation we can use in our code $app->cache()->get('cache_key') and in development environment we use the VoidCache and in production we use another caching profile., (*89)
This section still in development because we need to rewrite all the configuration system for manage the driver options., (*90)