openclassrooms / cache
Cache library that extends Doctrine\Cache capabilities
Installs: 396 198
Dependents: 4
Suggesters: 0
Security: 0
Stars: 0
Watchers: 28
Forks: 1
Open Issues: 0
Requires
- php: >=8.2
- doctrine/cache: ^2.0.0
Requires (Dev)
- phpunit/phpunit: ~3.7
This package is auto-updated.
Last update: 2025-01-11 17:54:06 UTC
README
Cache adds features to Doctrine Cache implementation
- Default lifetime
- Fetch with a namespace
- Save with a namespace
- Cache invalidation through namespace strategy
- CacheProvider Builder
Installation
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": "*" } }
<?php require 'vendor/autoload.php'; use OpenClassrooms\Cache\Cache\Cache; //do things
Usage
Instantiation
OC Cache needs a Doctrine CacheProvider to be instantiate.
$cacheProvider = new ArrayCache(); $cache = new Cache($cacheProvider);
A Cache builder can be used.
// Default builder, build a cache using ArrayCache Provider $cache = new CacheBuilderImpl()->build(); // Using a CacheProvider $cache = new CacheBuilderImpl() ->withCacheProvider($redisCache) ->build(); // Optional default lifetime $cache = new CacheBuilderImpl() ->withCacheProvider($redisCache) ->withDefaultLifetime(300) ->build();
Default lifetime
$cache->setDefaultLifetime(300); $cache->save($id, $data);
Fetch with namespace
$data = $cache->fetchWithNamespace($id, $namespaceId);
Save with namespace
// Namespace and life time can be null $data = $cache->saveWithNamespace($id, $data, $namespaceId, $lifeTime);
Cache invalidation
$cache->invalidate($namespaceId);
CacheProvider Builder
The library provides a CacheProvider Builder
// Memcache $cacheProvider = new CacheProviderBuilderImpl() ->create(CacheProviderType::MEMCACHE) ->withHost('127.0.0.1') ->withPort(11211) // Default 11211 ->withTimeout(1) // Default 1 ->build(); // Memcached $cacheProvider = new CacheProviderBuilderImpl() ->create(CacheProviderType::MEMCACHED) ->withHost('127.0.0.1') ->withPort(11211) // Default 11211 ->build(); // Redis $cacheProvider = new CacheProviderBuilderImpl() ->create(CacheProviderType::REDIS) ->withHost('127.0.0.1') ->withPort(6379) // Default 6379 ->withTimeout(0.0) // Default 0.0 ->build(); // Array $cacheProvider = new CacheProviderBuilderImpl() ->create(CacheProviderType::ARRAY_CACHE) ->build();