Installation Add the bundle via Composer:
composer require beryllium/cachebundle:dev-master
Or manually via deps file (legacy setups).
Enable the Bundle
Register in AppKernel.php:
new Beryllium\CacheBundle\BerylliumCacheBundle(),
Basic Configuration
Define Memcache servers in config.yml:
beryllium_cache:
servers:
default:
host: "127.0.0.1"
port: 11211
weight: 1
First Use Case Inject the cache service in a controller/service:
use Beryllium\CacheBundle\Cache\CacheInterface;
class MyController extends Controller
{
public function index(CacheInterface $cache)
{
$cache->set('key', 'value', 3600); // Store for 1 hour
$value = $cache->get('key');
return new Response($value);
}
}
Dependency Injection
CacheInterface in services/controllers for type-hinted caching:
public function __construct(CacheInterface $cache) { ... }
@cacheable).Multi-Server Setup
Configure multiple server pools in config.yml:
beryllium_cache:
servers:
primary:
host: "memcache1.example.com"
port: 11211
secondary:
host: "memcache2.example.com"
port: 11211
Access via:
$cache->getClient('primary')->get('key');
Integration with Symfony Cache
Extend Symfony’s Cache component:
$cache = $this->get('beryllium_cache.cache.default');
$cache->save($item, $key, ['ttl' => 3600]);
Event-Driven Caching
Listen for cache events (e.g., beryllium_cache.on_miss):
services:
my.cache_listener:
class: AppBundle\Listener\CacheListener
tags:
- { name: kernel.event_listener, event: beryllium_cache.on_miss, method: onCacheMiss }
Custom Cache Adapters
Implement Beryllium\CacheBundle\Cache\CacheInterface for APC/Redis:
class ApcCache implements CacheInterface { ... }
Register in services.yml:
services:
app.apc_cache:
class: AppBundle\Cache\ApcCache
tags:
- { name: beryllium_cache.cache, alias: apc }
Server Health Checks
timeout and retry_interval in config.yml:
beryllium_cache:
servers:
default:
timeout: 1.5
retry_interval: 30
config.yml:
beryllium_cache:
logging: true
Connection Pooling
persistent_id to maintain connections:
beryllium_cache:
servers:
default:
persistent_id: "app_memcache"
Namespace Collisions
$cache->set('app_' . $env . '_key', $value);
Serialization Quirks
serialize()/unserialize() explicitly:
$cache->set('key', serialize($object));
$object = unserialize($cache->get('key'));
Configuration Overrides
%kernel.environment% in config.yml:
beryllium_cache:
servers:
%kernel.environment%_memcache:
host: "local-memcache"
Custom Cache Drivers
Extend Beryllium\CacheBundle\Cache\AbstractCache for new backends (e.g., DynamoDB):
class DynamoCache extends AbstractCache {
public function get($key) { ... }
public function set($key, $value, $ttl) { ... }
}
Middleware for Cache Intercept requests to add cache headers:
$cache->addMiddleware(function ($request, $next) {
if ($request->headers->has('X-Cache-Key')) {
$key = $request->headers->get('X-Cache-Key');
$response = $cache->get($key);
return $response ?: $next($request);
}
return $next($request);
});
Cache Warmup Pre-load critical data during deployment:
$cache->warmup([
'homepage' => $this->renderView('homepage.html.twig'),
'products' => $this->getProducts(),
]);
Monitoring Track cache hits/misses via Symfony Profiler:
$cache->enableProfiler();
View data in /_profiler.
How can I help you explore Laravel packages today?