WIP for cache.

This commit is contained in:
Malte Hübner 2018-02-14 18:22:14 +01:00
parent 322728cf16
commit c32c5ff1a0
5 changed files with 96 additions and 27 deletions

View File

@ -3,6 +3,8 @@
namespace StaticMapLite\MapCache; namespace StaticMapLite\MapCache;
use StaticMapLite\Canvas\CanvasInterface; use StaticMapLite\Canvas\CanvasInterface;
use StaticMapLite\Output\CacheOutput;
use StaticMapLite\Output\OutputInterface;
use StaticMapLite\Printer\PrinterInterface; use StaticMapLite\Printer\PrinterInterface;
class MapCache class MapCache
@ -32,7 +34,6 @@ class MapCache
public function checkMapCache(): bool public function checkMapCache(): bool
{ {
return false;
$this->mapCacheID = md5($this->serializeParams()); $this->mapCacheID = md5($this->serializeParams());
$filename = $this->getFilename(); $filename = $this->getFilename();
@ -73,21 +74,22 @@ class MapCache
return $filename; return $filename;
} }
public function cache(CanvasInterface $canvas) public function cache(CanvasInterface $canvas): void
{ {
$this->mkdir_recursive(dirname($this->mapCacheIDToFilename()), 0777); $this->mkdir_recursive(dirname($this->mapCacheIDToFilename()), 0777);
imagepng($canvas->getImage(), $this->mapCacheIDToFilename(), 9); imagepng($canvas->getImage(), $this->mapCacheIDToFilename(), 9);
$this->sendHeader();
if (file_exists($this->mapCacheIDToFilename())) { $output = new CacheOutput();
return file_get_contents($this->mapCacheIDToFilename()); $output->setFilename($this->mapCacheIDToFilename())
} else { ->sendHeader()
return imagepng($canvas->getImage()); ->sendImage()
} ;
} }
public function mkdir_recursive($pathname, $mode) public function mkdir_recursive($pathname, $mode): bool
{ {
is_dir(dirname($pathname)) || $this->mkdir_recursive(dirname($pathname), $mode); is_dir(dirname($pathname)) || $this->mkdir_recursive(dirname($pathname), $mode);
return is_dir($pathname) || @mkdir($pathname, $mode); return is_dir($pathname) || @mkdir($pathname, $mode);
} }
@ -95,14 +97,4 @@ class MapCache
{ {
return $this->mapCacheIDToFilename(); return $this->mapCacheIDToFilename();
} }
public function sendHeader()
{
header('Content-Type: image/png');
$expires = 60 * 60 * 24 * 14;
header("Pragma: public");
header("Cache-Control: maxage=" . $expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');
}
} }

View File

@ -0,0 +1,31 @@
<?php
namespace StaticMapLite\Output;
class CacheOutput implements OutputInterface
{
protected $filename;
public function setFilename($filename): OutputInterface
{
$this->filename = $filename;
return $this;
}
public function sendHeader(): OutputInterface
{
header('Content-Type: image/png');
$expires = 60 * 60 * 24 * 14;
header('Pragma: public');
header('Cache-Control: maxage=' . $expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');
return $this;
}
public function sendImage(): void
{
echo file_get_contents($this->filename);
}
}

View File

@ -0,0 +1,31 @@
<?php
namespace StaticMapLite\Output;
class ImageOutput implements OutputInterface
{
protected $image;
public function setImage($image): OutputInterface
{
$this->image = $image;
return $this;
}
public function sendHeader(): OutputInterface
{
header('Content-Type: image/png');
$expires = 60 * 60 * 24 * 14;
header('Pragma: public');
header('Cache-Control: maxage=' . $expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $expires) . ' GMT');
return $this;
}
public function sendImage(): bool
{
return imagepng($this->image);
}
}

View File

@ -0,0 +1,8 @@
<?php
namespace StaticMapLite\Output;
interface OutputInterface
{
}

View File

@ -9,6 +9,8 @@ use StaticMapLite\Element\Polyline\Polyline;
use StaticMapLite\ElementPrinter\Marker\ExtraMarkerPrinter; use StaticMapLite\ElementPrinter\Marker\ExtraMarkerPrinter;
use StaticMapLite\ElementPrinter\Polyline\PolylinePrinter; use StaticMapLite\ElementPrinter\Polyline\PolylinePrinter;
use StaticMapLite\MapCache\MapCache; use StaticMapLite\MapCache\MapCache;
use StaticMapLite\Output\CacheOutput;
use StaticMapLite\Output\ImageOutput;
use StaticMapLite\TileResolver\CachedTileResolver; use StaticMapLite\TileResolver\CachedTileResolver;
use StaticMapLite\Util; use StaticMapLite\Util;
@ -173,23 +175,28 @@ class Printer extends AbstractPrinter
public function showMap() public function showMap()
{ {
if ($this->mapCache) { if ($this->mapCache) {
// use map cache, so check cache for map
if (!$this->mapCache->checkMapCache()) { if (!$this->mapCache->checkMapCache()) {
// map is not in cache, needs to be build
$this->makeMap(); $this->makeMap();
$this->mapCache->cache($this->canvas); $this->mapCache->cache($this->canvas);
} else { } else {
// map is in cache $output = new CacheOutput();
$this->sendHeader(); $output
return file_get_contents($this->mapCache->getFilename()); ->setFilename($this->mapCache->getFilename())
->sendHeader()
->sendImage()
;
} }
} else { } else {
// no cache, make map, send headers and deliver png // no cache, make map, send headers and deliver png
$this->makeMap(); $this->makeMap();
$this->sendHeader();
return imagepng($this->canvas->getImage());
$output = new ImageOutput();
$output
->setImage($this->image)
->sendHeader()
->sendImage()
;
} }
} }
} }