Skip to content

Instantly share code, notes, and snippets.

@sfalquier
Created July 5, 2012 08:02
Show Gist options
  • Save sfalquier/3052181 to your computer and use it in GitHub Desktop.
Save sfalquier/3052181 to your computer and use it in GitHub Desktop.
Memcached in Symfony2
//file app/config/parameters.ini
[parameters]
memcached_host = 127.0.0.1
memcached_port = 11211
memcached_key_root = mykey
memcached_object_ttl = 60
//file src/MyApp/MyBundle/Resources/config/services.yml
parameters:
memcached.servers:
- { host: %memcached_host%, port: %memcached_port% }
services:
memcached:
class: Memcached
calls:
- [ addServers, [ %memcached.servers% ]]
//file src/MyApp/MyBundle/Controller/MyController.php
$memcached = $this->get('memcached');
$memcachedObjectTtl = $this->container->getParameter('memcached_info_ttl');
$memcachedKeyRoot = $this->container->getParameter('memcached_key_root');
$object = $memcached->get($memcachedKeyRoot.$objectKey);
if(!$object) {
$object = $this->getObject($objectKey);//TODO coder la méthode getObject
$memcached->set($memcachedKeyRoot.$objectKey,$object,$memcachedObjectTtl);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment