besimple/memcached
Laravel-friendly Memcached integration that simplifies using the PHP Memcached extension for caching and session storage. Provides configuration helpers and adapter glue so your app can talk to Memcached servers with minimal setup.
Installation
composer require besimple/memcached
(Note: Due to lack of maintenance, verify compatibility with your Laravel version.)
Configuration
Add to config/cache.php under stores:
'memcached' => [
'driver' => 'memcached',
'host' => env('MEMCACHED_HOST', '127.0.0.1'),
'port' => env('MEMCACHED_PORT', 11211),
'prefix' => env('MEMCACHED_PREFIX', 'laravel_'),
'options' => [
'retry_interval' => 150,
'timeout' => 2000,
],
],
First Use Case Cache a value with a 10-minute TTL:
Cache::put('key', 'value', now()->addMinutes(10));
$value = Cache::get('key'); // Retrieve
Cache-as-Sidecar Use alongside database queries to reduce load:
$posts = Cache::remember('posts:latest', now()->addHours(1), function () {
return Post::latest()->limit(10)->get();
});
Tag-Based Invalidation
(If supported; verify via Cache::tags())
Cache::tags(['users', 'profile'])->put('user:1:profile', $profileData);
Cache::tags(['users'])->flush(); // Invalidate all tagged keys
Fallback Logic
$data = Cache::remember('expensive:data', now()->addDays(7), function () {
return ExpensiveOperation::run();
});
Cache::put('job:payload:123', $payload, now()->addMinutes(5));
Cache-Control headers:
return Cache::remember('api:users', now()->addMinutes(5), function () {
return Http::get('https://api.example.com/users');
});
No Maintenance
predis/predis or spatie/laravel-memcached if critical.Connection Issues
memcached service status (sudo systemctl status memcached) and verify host/port in .env.Prefix Collisions
prefix (e.g., laravel_app_).$keys = Cache::getStore()->getConnection()->getKeys('laravel_*');
php artisan cache:clear
(Note: May not work if the package lacks proper Laravel cache integration.)options array (if supported):
'options' => [
'serializer' => new \Memcached\Serializer\JsonSerializer(),
],
Cache::store events.)memcached-tool to check memory usage:
memcached-tool 127.0.0.1:11211 stats
file driver for resilience:
Cache::store('memcached')->remember(...);
How can I help you explore Laravel packages today?