Installation
composer require donkeycode/varnish-bundle
Add to config/bundles.php:
DonkeCode\VarnishBundle\DonkeCodeVarnishBundle::class => ['all' => true],
Publish Config
php bin/console donkecode:varnish:install
This generates config/packages/donkecode_varnish.yaml with default settings.
First Use Case: Cache by User Group Annotate a controller method with:
use DonkeCode\VarnishBundle\Annotation\Varnish;
/**
* @Varnish(group="premium_users")
*/
public function premiumContent()
{
return new Response('Premium content');
}
Ensure VarnishListener is enabled in config/packages/donkecode_varnish.yaml:
listeners:
varnish: true
Annotation-Based Caching
@Varnish on controller methods with:
group: Cache by Symfony security group (e.g., ROLE_ADMIN).id: Cache by user ID (e.g., id="%user.id%").ttl: Custom TTL (default: 3600 seconds)./**
* @Varnish(group="ROLE_EDITOR", ttl=86400)
*/
public function editorDashboard()
Dynamic Rules via Event Subscribers
Extend caching logic by subscribing to varnish.cache_key event:
use DonkeCode\VarnishBundle\Event\CacheKeyEvent;
public function onCacheKey(CacheKeyEvent $event)
{
if ($event->getUser() && $event->getUser()->isVerified()) {
$event->setCacheKey('verified_' . $event->getCacheKey());
}
}
Register in services.yaml:
services:
App\EventSubscriber\VarnishSubscriber:
tags:
- { name: kernel.event_subscriber }
Integration with Symfony Cache
Use VarnishCache as a drop-in replacement for Symfony’s cache:
use DonkeCode\VarnishBundle\Cache\VarnishCache;
public function __construct(private VarnishCache $varnishCache) {}
public function getCachedData()
{
return $this->varnishCache->get('my_key', function() {
return $this->fetchExpensiveData();
});
}
CLI Management
php bin/console cache:clear varnishphp bin/console cache:warmup varnishAnnotation Parsing
annotations is enabled in framework config (framework: { annotations: true }).php bin/console debug:container DonkeCode\VarnishBundle\DependencyInjection\Compiler\VarnishPass
User Context
SecurityContext. If no user is authenticated, caching falls back to public cache.TTL Overrides
config overrides method-level TTLs. Set explicitly if needed:
ttl: 300 # Overrides all methods
Varnish Server Sync
PURGE requests. Verify your Varnish instance supports:
vcl_recv {
if (req.method == "PURGE") {
return (purge);
}
}
Enable Logging
Add to config/packages/donkecode_varnish.yaml:
debug: true
Logs appear in var/log/dev.log (or prod.log).
Check Cache Keys
Use the debug:varnish command:
php bin/console debug:varnish
Lists all cached keys and their metadata.
Custom Cache Providers
Implement DonkeCode\VarnishBundle\Cache\CacheProviderInterface for alternative backends (e.g., Redis):
class RedisVarnishProvider implements CacheProviderInterface
{
public function get($key) { /* ... */ }
public function set($key, $value, $ttl) { /* ... */ }
public function purge($key) { /* ... */ }
}
Register in services.yaml:
services:
DonkeCode\VarnishBundle\Cache\VarnishCache:
arguments:
$provider: '@app.redis_varnish_provider'
Conditional Logic
Extend DonkeCode\VarnishBundle\Cache\CacheKeyGenerator to add custom rules:
public function generateKey(Request $request, UserInterface $user = null)
{
$key = parent::generateKey($request, $user);
if ($user && $user->hasRole('FEATURE_X')) {
$key .= '_feature_x';
}
return $key;
}
Varnish Purge Events
Listen for varnish.purge events to trigger external actions:
use DonkeCode\VarnishBundle\Event\PurgeEvent;
public function onPurge(PurgeEvent $event)
{
// Notify analytics, log purge, etc.
}
How can I help you explore Laravel packages today?