Installation:
composer require aw/zeta-cache-bundle
Add the bundle to app/AppKernel.php:
new Aw\ZetaCacheBundle\AwZetaCacheBundle(),
Configuration:
Enable the bundle in app/config/config.yml:
aw_zeta_cache:
dev_mode: true # Set to false in production
storages:
default:
type: file
path: "%kernel.cache_dir%/zeta"
options:
max_items: 1000
First Use Case: Inject the cache service in a controller or service:
use Aw\ZetaCacheBundle\Service\CacheService;
class MyController extends Controller
{
public function indexAction(CacheService $cache)
{
$data = $cache->get('my_key');
if ($data === null) {
$data = $this->fetchData(); // Expensive operation
$cache->set('my_key', $data, ['tag1', 'tag2']);
}
return $this->render('template.html.twig', ['data' => $data]);
}
}
Tag-Based Caching: Use tags to group related cache items for efficient invalidation:
$cache->set('user_profile_123', $profile, ['user', 'profile']);
$cache->deleteByTag('user'); // Invalidate all user-related items
Hierarchical Caching (Stacks):
Define a stack in config.yml to combine multiple backends (e.g., APC + File):
aw_zeta_cache:
stacks:
hybrid:
storages: [apc, file]
options:
free_rate: 0.2
Use the stack service:
$cache->getService('hybrid')->set('key', $data);
Event-Driven Cache Clearing:
Trigger cache invalidation via Symfony events (e.g., kernel.request):
# config.yml
aw_zeta_cache:
event_listeners:
kernel.request:
- { storage: 'default', tags: ['user'] }
CLI Management: Clear caches via console:
php app/console aw:zeta-cache:clear --storage=default
php app/console aw:zeta-cache:delete --tags="user,profile"
Symfony Cache Abstraction:
Extend CacheInterface to bridge Zeta Cache with Symfony’s Cache component:
class ZetaCacheAdapter implements CacheInterface {
public function fetch($id) {
return $this->zetaCache->get($id);
}
// ... other methods
}
Dependency Injection: Tag services for automatic cache invalidation:
services:
my.service:
tags: ['aw_zeta_cache.invalidator']
arguments: ['default', ['tag1', 'tag2']]
Dev Mode:
Enable dev_mode: true to bypass cache entirely during development.
Unmaintained Package:
Configuration Overhead:
max_items and free_rate to avoid performance degradation.free_rate: 0.1 for conservative memory usage.Race Conditions:
config.yml:
aw_zeta_cache:
anti_dog_pile: true
Tag Performance:
['user_role']).APC/Memcache Dependencies:
php-apc, php-memcache). Fallback to file storage if unavailable:
storages:
fallback:
type: file
path: "%kernel.cache_dir%/zeta_fallback"
Dev Mode:
Set dev_mode: true to force cache misses and test logic without clearing manually.
Log Cache Operations:
Enable Symfony’s cache profiler (%kernel.debug%: true) to monitor hits/misses.
Check Storage Limits: Use the CLI to inspect storage sizes:
php app/console aw:zeta-cache:info
Custom Replacement Strategies:
Override the default LRU/LFU by extending Zeta\Cache\Storage\ReplacementStrategy and configuring it in config.yml:
storages:
custom:
type: file
replacement_strategy: my_custom_strategy
New Storage Backends:
Implement Zeta\Cache\Storage\StorageInterface and register it via a compiler pass.
Event Listeners:
Extend cache invalidation logic by subscribing to aw_zeta_cache.invalidate events:
// services.yml
my.invalidator:
class: AppBundle\EventListener\CacheInvalidator
tags:
- { name: kernel.event_listener, event: aw_zeta_cache.invalidate, method: onInvalidate }
How can I help you explore Laravel packages today?