Installation:
composer require driebit/http-cache-bundle
(Note: While deprecated, this package remains functional for legacy systems.)
Enable the Bundle:
Add to config/bundles.php:
return [
// ...
Driebit\HttpCacheBundle\DriebitHttpCacheBundle::class => ['all' => true],
];
Configure Cache Clients:
Edit config/packages/driebit_http_cache.yaml:
driebit_http_cache:
clients:
varnish:
type: 'varnish'
host: '127.0.0.1'
port: 6082
purge_url: 'http://%s:%d/%s'
First Use Case: Purge a URL via a controller:
use Driebit\HttpCacheBundle\Manager\CacheManager;
class CacheController extends AbstractController
{
public function purge(CacheManager $cacheManager)
{
$cacheManager->purge('varnish', '/cached-path');
return new Response('Purged!');
}
}
Purging Strategies:
$cacheManager->purge('varnish', '/path/to/resource');
$cacheManager->purgeMultiple('varnish', ['/path1', '/path2']);
$cacheManager->purgeByTag('varnish', 'user-profile');
Integration with Symfony Events:
Listen to kernel.terminate for post-request purges:
// config/services.yaml
services:
App\EventListener\CachePurgerListener:
tags:
- { name: 'kernel.event_listener', event: 'kernel.terminate', method: 'onTerminate' }
// src/EventListener/CachePurgerListener.php
class CachePurgerListener
{
public function onTerminate(Request $request, CacheManager $cacheManager)
{
if ($request->isXmlHttpRequest()) {
$cacheManager->purge('varnish', $request->getUri());
}
}
}
Custom Cache Clients:
Extend Driebit\HttpCacheBundle\Client\AbstractHttpCacheClient:
class CustomCacheClient extends AbstractHttpCacheClient
{
protected function doPurge($url)
{
// Custom logic (e.g., API call to your cache)
file_put_contents('php://stdout', "Purging: $url\n");
}
}
Register in config:
driebit_http_cache:
clients:
custom:
type: 'custom'
class: 'App\Cache\CustomCacheClient'
Conditional Purging:
Use Symfony’s Cache component to check if content is stale before purging:
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
class ConditionalCachePurger implements CacheWarmerInterface
{
public function isOptional(): bool { return true; }
public function warmUp($cacheDir): array
{
$request = Request::createFromGlobals();
if ($this->isContentStale($request)) {
$cacheManager->purge('varnish', $request->getUri());
}
return [];
}
}
Deprecation Warning:
Performance Impact:
Configuration Quirks:
port in driebit_http_cache.yaml matches your cache server’s actual port (e.g., Varnish’s default is 6082 for admin).purge_url template must support %s (host), %d (port), and %s (path). Example:
purge_url: 'http://%s:%d/purge/%s' # Varnish 4+ format
Debugging:
# config/packages/monolog.yaml
monolog:
handlers:
cache:
type: stream
path: "%kernel.logs_dir%/cache.log"
level: debug
class MockCacheClient extends AbstractHttpCacheClient
{
public function purge($url)
{
file_put_contents('php://stdout', "Mock purge: $url\n");
}
}
Environment-Specific Configs:
Override cache settings per environment (e.g., disable purging in dev):
# config/packages/dev/driebit_http_cache.yaml
driebit_http_cache:
clients:
varnish:
enabled: false
Tag-Based Invalidation:
If your cache supports tags (e.g., Varnish with ban), use them for granular control:
$cacheManager->purgeByTag('varnish', 'product-' . $productId);
Rate Limiting: Throttle purge requests to avoid overwhelming your cache server:
use Symfony\Component\RateLimiter\RateLimiterFactory;
class ThrottledCacheManager
{
public function purge($clientName, $url)
{
$rateLimiter = RateLimiterFactory::create(['limit' => 10, 'interval' => '1 minute']);
if ($rateLimiter->consume()) {
$cacheManager->purge($clientName, $url);
}
}
}
Testing:
Driebit\HttpCacheBundle\Client\NullHttpCacheClient for tests:
# config/packages/test/driebit_http_cache.yaml
driebit_http_cache:
clients:
varnish:
type: 'null'
CacheManager in PHPUnit:
$cacheManager = $this->createMock(CacheManager::class);
$cacheManager->expects($this->once())
->method('purge')
->with('varnish', '/test');
How can I help you explore Laravel packages today?