Created
January 25, 2012 09:43
-
-
Save christeredvartsen/1675661 to your computer and use it in GitHub Desktop.
Metadata cache for Imbo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class metadatacache implements Imbo\EventManager\ListenerInterface { | |
private $memcached; | |
public function __construct() { | |
$this->memcached = new Memcached(); | |
$this->memcached->addServer('localhost', 11211); | |
} | |
public function getEvents() { | |
return array( | |
'metadata.get.pre', | |
'metadata.get.post', | |
); | |
} | |
public function invoke(Imbo\EventManager\EventInterface $event) { | |
$response = $event->getResponse(); | |
$imageIdentifier = $response->getHeaders()->get('x-imbo-imageidentifier'); | |
$cacheKey = 'metadata:' . $imageIdentifier; | |
if ($event->getName() === 'metadata.get.pre') { | |
$result = $this->memcached->get($cacheKey); | |
if ($result instanceof Imbo\Http\Response\ResponseInterface) { | |
$result->send(); | |
exit; | |
} | |
} else { | |
$this->memcached->set($cacheKey, $response); | |
} | |
} | |
} | |
$request = new Imbo\Http\Request\Request($_GET, $_POST, $_SERVER); | |
$response = new Imbo\Http\Response\Response(); | |
$eventManager = new Imbo\EventManager\EventManager($request, $response); | |
$eventManager->attachListener(new metadatacache()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment