Installation Add the bundle via Composer:
composer require besimple/memcached-bundle
Register the bundle in config/bundles.php:
return [
// ...
BeSimple\MemcachedBundle\BeSimpleMemcachedBundle::class => ['all' => true],
];
Configuration
Define Memcached servers in config/packages/besimple_memcached.yaml:
besimple_memcached:
servers:
- host: '127.0.0.1'
port: 11211
weight: 1
options:
prefix: 'myapp_'
compression: true
First Use Case
Inject the MemcachedClient service into a controller or service:
use BeSimple\MemcachedBundle\Client\MemcachedClientInterface;
class MyController extends AbstractController
{
public function __construct(private MemcachedClientInterface $memcached)
{
}
public function cacheExample()
{
$this->memcached->set('key', 'value', 3600); // Cache for 1 hour
$value = $this->memcached->get('key');
return new Response($value ?? 'Not found');
}
}
Caching Data
Use set()/get() for simple key-value caching:
$this->memcached->set('user_123_data', $userData, 300); // 5-minute TTL
$cachedData = $this->memcached->get('user_123_data');
Cache Invalidation Delete keys explicitly:
$this->memcached->delete('user_123_data');
Or use wildcards (if supported by Memcached server):
$this->memcached->deleteMulti(['user_*']);
Tag-Based Invalidation Leverage tags for grouped invalidation (if configured):
besimple_memcached:
options:
tags: true
$this->memcached->add('user_123_data', $data, 300, ['user', 'profile']);
$this->memcached->deleteByTags(['user']);
Integration with Symfony Cache
Use the bundle’s CacheAdapter for PSR-6 compliance:
use BeSimple\MemcachedBundle\Cache\MemcachedAdapter;
$cache = $this->container->get('besimple_memcached.cache.adapter');
$item = $cache->getItem('key');
Fallback Logic
Combine with Symfony’s CacheItemPoolInterface for fallback:
$cache = $this->container->get('besimple_memcached.cache.adapter');
$item = $cache->getItem('expensive_data', function (CacheItemInterface $item) {
$item->expiresAfter(3600);
return $this->fetchExpensiveData();
});
Distributed Locking
Use add() with a short TTL for locks:
$locked = $this->memcached->add('lock_key', true, 5);
if ($locked) {
// Critical section
$this->memcached->delete('lock_key');
}
Batch Operations Fetch multiple keys at once:
$keys = ['key1', 'key2', 'key3'];
$values = $this->memcached->getMulti($keys);
Connection Issues
servers config and test connectivity:
telnet 127.0.0.1 11211
Key Collisions
prefix in config (e.g., myapp_) to namespace keys.TTL Misconfiguration
3600 for 1 hour) and avoid 0 (unlimited).Serialization Errors
__serialize()/__unserialize() or use json_encode():
$this->memcached->set('obj', json_encode($object));
$data = json_decode($this->memcached->get('obj'), true);
Enable Logging
Add to config/packages/monolog.yaml:
handlers:
memcached:
type: fingers_crossed
action_level: error
handler: nested
buffer_size: 100
channels: ["besimple_memcached"]
Check Stats
Use the stats() method to monitor server health:
$stats = $this->memcached->getStats();
Test Locally Run Memcached in Docker for development:
docker run -p 11211:11211 memcached
Custom Client Configuration
Override the default client in config/packages/besimple_memcached.yaml:
besimple_memcached:
client_class: App\Service\CustomMemcachedClient
Event Listeners
Subscribe to MemcachedEvents (if the bundle supports them) for pre/post-cache operations:
// config/services.yaml
App\EventListener\MemcachedListener:
tags:
- { name: kernel.event_listener, event: besimple_memcached.cache.set, method: onCacheSet }
Proxy Integration
Use with Symfony’s HttpCache or ProxyClient for HTTP caching:
$client = new ProxyClient(
new MemcachedAdapter($this->memcached),
new Client()
);
How can I help you explore Laravel packages today?