Cache adds features to Doctrine Cache implementation
- Default lifetime
- Fetch with a namespace
- Save with a namespace
- Cache invalidation through namespace strategy
- CacheProvider Builder
The easiest way to install Cache is via composer.
Create the following composer.json file and run the php composer.phar install command to install it.
{
"require": {
"openclassrooms/cache": "*"
}
}<?phprequire'vendor/autoload.php';
useOpenClassrooms\Cache\Cache\Cache;
//do thingsOC Cache needs a Doctrine CacheProvider to be instantiate.
$cacheProvider = newArrayCache();
$cache = newCache($cacheProvider);A Cache builder can be used.
// Default builder, build a cache using ArrayCache Provider$cache = newCacheBuilderImpl()->build();
// Using a CacheProvider$cache = newCacheBuilderImpl()
->withCacheProvider($redisCache)
->build();
// Optional default lifetime$cache = newCacheBuilderImpl()
->withCacheProvider($redisCache)
->withDefaultLifetime(300)
->build();$cache->setDefaultLifetime(300);
$cache->save($id, $data);$data = $cache->fetchWithNamespace($id, $namespaceId);// Namespace and life time can be null$data = $cache->saveWithNamespace($id, $data, $namespaceId, $lifeTime);$cache->invalidate($namespaceId);The library provides a CacheProvider Builder
// Memcache$cacheProvider = newCacheProviderBuilderImpl()
->create(CacheProviderType::MEMCACHE)
->withHost('127.0.0.1')
->withPort(11211) // Default 11211
->withTimeout(1) // Default 1
->build();
// Memcached$cacheProvider = newCacheProviderBuilderImpl()
->create(CacheProviderType::MEMCACHED)
->withHost('127.0.0.1')
->withPort(11211) // Default 11211
->build();
// Redis$cacheProvider = newCacheProviderBuilderImpl()
->create(CacheProviderType::REDIS)
->withHost('127.0.0.1')
->withPort(6379) // Default 6379
->withTimeout(0.0) // Default 0.0
->build();
// Array$cacheProvider = newCacheProviderBuilderImpl()
->create(CacheProviderType::ARRAY_CACHE)
->build();
