Imbo\EventListener\MetadataCache::loadFromCache PHP Method

loadFromCache() public method

Get data from the cache
public loadFromCache ( Imbo\EventManager\EventInterface $event )
$event Imbo\EventManager\EventInterface The event instance
    public function loadFromCache(EventInterface $event)
    {
        $request = $event->getRequest();
        $response = $event->getResponse();
        $cacheKey = $this->getCacheKey($request->getUser(), $request->getImageIdentifier());
        $result = $this->cache->get($cacheKey);
        if (is_array($result) && isset($result['lastModified']) && $result['lastModified'] instanceof DateTime && isset($result['metadata'])) {
            $model = new Model\Metadata();
            $model->setData($result['metadata']);
            $response->setModel($model)->setLastModified($result['lastModified']);
            $response->headers->set('X-Imbo-MetadataCache', 'Hit');
            // Stop propagation of listeners for this event
            $event->stopPropagation();
            return;
        } else {
            if ($result) {
                // Invalid result stored in the cache. Delete
                $this->cache->delete($cacheKey);
            }
        }
        $response->headers->set('X-Imbo-MetadataCache', 'Miss');
    }

Usage Example

Beispiel #1
0
 /**
  * @covers Imbo\EventListener\MetadataCache::loadFromCache
  */
 public function testDeletesInvalidCachedData()
 {
     $this->cache->expects($this->once())->method('get')->with($this->isType('string'))->will($this->returnValue(['lastModified' => 'preformatted date', 'metadata' => ['key' => 'value']]));
     $this->cache->expects($this->once())->method('delete')->with($this->isType('string'));
     $this->responseHeaders->expects($this->once())->method('set')->with('X-Imbo-MetadataCache', 'Miss');
     $this->response->expects($this->never())->method('setModel');
     $this->listener->loadFromCache($this->event);
 }