Installation:
composer require becklyn/cache
Ensure Becklyn\CacheBundle\BecklynCacheBundle::class is registered in config/bundles.php.
First Use Case:
Inject SimpleCacheFactory into a service and fetch a cached item with a fallback generator:
use Becklyn\Cache\Cache\SimpleCacheFactory;
class MyService {
public function __construct(private SimpleCacheFactory $cacheFactory) {}
public function getCachedData() {
return $this->cacheFactory->getItem(
"my.key",
fn() => $this->expensiveOperation()
)->get();
}
}
Key Locations:
SimpleCacheFactory (entry point for cache operations).config/packages/becklyn_cache.yaml (if extended).Lazy-Loading with Fallback:
$cacheItem = $cacheFactory->getItem("key", fn() => $this->fetchFromDB());
$data = $cacheItem->get(); // Calls fallback if cache misses
Config-Driven Invalidation:
Use Symfony’s ResourceInterface for cache invalidation (e.g., config files, routes):
$cacheItem = $cacheFactory->getItem(
"config.cache",
fn() => $this->loadConfig(),
[new \Symfony\Component\Config\Resource\FileResource('/path/to/config.yaml')]
);
Two-Level Caching:
CacheInterface).ConfigCache for invalidation (triggered by tracked resources).Integration with Laravel:
SimpleCacheFactory to Laravel’s container in AppServiceProvider:
$this->app->bind(SimpleCacheFactory::class, function ($app) {
return new SimpleCacheFactory($app['cache']);
});
app.feature.data) to avoid collisions.$cacheItem->expiresAfter(3600); // 1 hour
SimpleCacheFactory::getMultiple() for batch fetching:
$items = $cacheFactory->getMultiple([
"key1" => fn() => $data1,
"key2" => fn() => $data2,
]);
Concurrency Warning:
@cache tags.Cache component for critical sections.Resource Tracking:
ResourceInterface implementations may break invalidation. Test with:
$resource = new \Symfony\Component\Config\Resource\FileResource('/nonexistent');
$cacheItem = $cacheFactory->getItem("test", fn() => [], [$resource]);
$cacheItem->invalidate(); // Verify invalidation works.
Serialization Issues:
serialize()/unserialize() or JSON encoding for complex data.Laravel-Specific Quirks:
Cache facade may not support all CacheInterface methods. Use CacheManager for compatibility:
$cache = app('cache')->driver('array'); // or 'file', 'redis'
$cacheFactory->getItem("key")->isHit(); // Check if cached.
$cacheItem->invalidate(); // Add logging before/after.
microtime() to compare against Laravel’s Cache::remember():
$start = microtime(true);
$data = $cacheItem->get();
echo "Time: " . (microtime(true) - $start);
Custom Cache Drivers:
Extend SimpleCacheFactory to support Laravel’s Cache drivers:
class LaravelCacheFactory extends SimpleCacheFactory {
public function __construct(\Illuminate\Contracts\Cache\Store $cache) {
parent::__construct($cache);
}
}
Event Listeners:
Hook into CacheItemInvalidatedEvent (if extended) to sync external caches:
// Hypothetical extension
$cacheItem->onInvalidate(function () {
Cache::forget('external_key');
});
Config Overrides:
Override default TTLs in config/packages/becklyn_cache.yaml:
becklyn_cache:
default_ttl: 7200 # 2 hours
stale_ttl: 300 # 5 minutes
How can I help you explore Laravel packages today?