From c4980c481d5a076d3af917f8d4f5899ee4f44a42 Mon Sep 17 00:00:00 2001 From: Stefan Heimes Date: Fri, 25 Nov 2022 17:53:28 +0100 Subject: [PATCH] Update for PHP 8.1 and MetaModels 2.3 --- src/Bundle/Resources/config/services.yml | 6 ++-- src/MapProvider.php | 44 +++++++++++++++--------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/Bundle/Resources/config/services.yml b/src/Bundle/Resources/config/services.yml index 66cbab6..bfbc800 100644 --- a/src/Bundle/Resources/config/services.yml +++ b/src/Bundle/Resources/config/services.yml @@ -29,12 +29,14 @@ services: - '%netzmacht.contao_leaflet.filters%' netzmacht.contao_leaflet.cache.default: - class: Doctrine\Common\Cache\FilesystemCache + class: Symfony\Component\Cache\Adapter\FilesystemAdapter arguments: + - 'netzmacht.contao_leaflet' + - 0 - '%netzmacht.contao_leaflet.cache_dir%' netzmacht.contao_leaflet.cache.debug: - class: Doctrine\Common\Cache\ArrayCache + class: Symfony\Component\Cache\Adapter\ArrayAdapter netzmacht.contao_leaflet.frontend.value_filter: class: Netzmacht\Contao\Leaflet\Frontend\ValueFilter diff --git a/src/MapProvider.php b/src/MapProvider.php index 192a80c..60f8574 100644 --- a/src/MapProvider.php +++ b/src/MapProvider.php @@ -13,7 +13,6 @@ namespace Netzmacht\Contao\Leaflet; use Contao\Input; -use Doctrine\Common\Cache\Cache; use Netzmacht\Contao\Leaflet\Encoder\ContaoAssets; use Netzmacht\Contao\Leaflet\Event\GetJavascriptEvent; use Netzmacht\Contao\Leaflet\Filter\Filter; @@ -28,7 +27,9 @@ use Netzmacht\Contao\Toolkit\View\Template\TemplateRenderer; use Netzmacht\LeafletPHP\Definition\Map; use Netzmacht\LeafletPHP\Leaflet; use Netzmacht\LeafletPHP\Value\GeoJson\FeatureCollection; +use Symfony\Component\Cache\CacheItem; use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as EventDispatcher; +use Symfony\Contracts\Cache\CacheInterface as Cache; /** * Class MapProvider. @@ -212,25 +213,29 @@ class MapProvider if ($doCache) { $cacheKey = $this->getCacheKey($mapId, $filter, $elementId, $template, $style); - if ($this->cache->contains($cacheKey)) { - $cached = $this->cache->fetch($cacheKey); - $this->assets->fromArray($cached['assets']); + if ($this->cache->hasItem($cacheKey)) { + $cached = $this->cache->getItem($cacheKey); + $achedData = $cached->get(); + $this->assets->fromArray($achedData['assets']); - return $cached['javascript']; + return $achedData['javascript']; + } else { + $cached = $this->cache->getItem($cacheKey); } } $buffer = $this->doGenerate($model, $filter, $elementId, $template, $style); if ($doCache) { - $this->cache->save( - $cacheKey, - [ - 'assets' => $this->assets->toArray(), - 'javascript' => $buffer, - ], - (int) $model->cacheLifeTime - ); + $cached + ->expiresAfter((int)$model->cacheLifeTime) + ->set( + [ + 'assets' => $this->assets->toArray(), + 'javascript' => $buffer, + ] + ); + $this->cache->save($cached); } return $buffer; @@ -270,12 +275,19 @@ class MapProvider $cacheKey .= '.filter_' . md5($filter->toRequest()); } - if ($this->cache->contains($cacheKey)) { - return $this->cache->fetch($cacheKey); + if ($this->cache->hasItem($cacheKey)) { + $cachedItem = $this->cache->getItem($cacheKey); + + return $cachedItem->get(); + } else { + $cachedItem = $this->cache->getItem($cacheKey); } $collection = $this->mapper->handleGeoJson($model, $request); - $this->cache->save($cacheKey, $collection, $model->cacheLifeTime); + $cachedItem + ->expiresAfter($model->cacheLifeTime) + ->set($collection); + $this->cache->save($cachedItem); return $collection; }