Skip to content

Commit

Permalink
Merge branch 'pimcore-x-webonyx-features' into OF-308-feature-draft-d…
Browse files Browse the repository at this point in the history
…ynamic-cs-filter
  • Loading branch information
mugge6 committed Nov 6, 2024
2 parents 0c669e9 + 655f123 commit 1c3a338
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 29 deletions.
83 changes: 58 additions & 25 deletions src/EventListener/CacheListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Pimcore\Model\Document\Link;
use Pimcore\Model\Document\PageSnippet;
use Pimcore\Model\Element\AbstractElement;
use Symfony\Component\EventDispatcher\GenericEvent;

class CacheListener
{
Expand Down Expand Up @@ -58,18 +59,13 @@ public static function addCachingItem(
self::$cachingItems = new \SplObjectStorage();
}

// Load object to get cache tags.
$object = Concrete::getById($objectId);
if ($object) {
self::$cachingItems[$operation] = self::$cachingItems[$operation] ?? [];
$cacheTags = ['datahub-cache'] + (self::getObjectCacheTags($object) ?? []);
self::$cachingItems[$operation] += [$cid => [
'path' => $path,
'tags' => $cacheTags,
'indexKey' => $indexKey,
'lifetime' => $lifetime,
]];
}
self::$cachingItems[$operation] = self::$cachingItems[$operation] ?? [];
self::$cachingItems[$operation] += [$cid => [
'objectId' => $objectId,
'path' => $path,
'indexKey' => $indexKey,
'lifetime' => $lifetime,
]];
}

public static function clearCachingItems(OperationParams $operation = null): void
Expand Down Expand Up @@ -100,35 +96,72 @@ public static function arrayGetNestedValue(array &$array, array $parents, &$key_
return $ref;
}

/**
* Add the cache item meta-data before saving the cache items.
*
* Uses shutdown because at this point the response should be already
* delivered and any further processing shouldn't affect reponse times.
*
* @param \Symfony\Component\EventDispatcher\GenericEvent $event
*
* @return void
*/
public function onPimcoreShutdown(GenericEvent $event): void
{
foreach (self::$cachingItems ?? [] as $operation) {
// Find items declared for caching in result set and store them in cache.
foreach (self::$cachingItems[$operation] as $cid => $item) {
// Extract the related cache tags.
$cacheTags = [];
$object = Concrete::getById($item['objectId']);
if (!empty($object)) {
$cacheTags = ['datahub-cache'] + (self::getObjectCacheTags($object) ?? []);
}
// Not sure why force is necessary.
Cache::save(
$item['data'],
$cid,
$cacheTags,
$item['lifetime'] ?? null,
0,
true
);
}
}
}

/**
* Adds the actual data to the for caching prepared cache items.
*
* Do NOT save yet - wait for that till shutdown to ensure response is
* out before triggering any further processing.
*
*
* @param \Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\CacheItemEvent $event
*
* @return void
*/
public function onCacheItemEvent(CacheItemEvent $event): void
{
$operation = $event->getOperation();

if (isset(self::$cachingItems[$operation])) {
$result = $event->getResult();
$data = $result->data;

// Find items declared for caching in result set and store them in cache.
foreach (self::$cachingItems[$operation] as $cid => $item) {
$cachedItems = self::$cachingItems[$operation];
foreach ($cachedItems as $cid => $item) {
// replace the "delta" placeholder with the effective index to extract data
$path = $item['path'];
if ($index = array_search('delta', $path, true)) {
$path[$index] = $item['indexKey'];
}
// Extract the cacheable portion from the result.
$value = self::arrayGetNestedValue($data, $path);
$cacheTags = $item['tags'] ?? [];
Cache::save(
$value,
$cid,
$cacheTags,
$item['lifetime'] ?? null,
0,
true
);
$item['data'] = $value;
$cachedItems[$cid] = $item;
self::$cachingItems->offsetSet($operation, $cachedItems);
}
}
self::clearCachingItems($operation);
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/GraphQL/Resolver/QueryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -1016,8 +1016,13 @@ public function resolveFilter($value = null, $args = [], $context = [], ResolveI
// $resultList->addCondition(['terms' => ['system.path' => $readablePaths]]);
}

// set category if there is one from type AbstractCategory
/** @var AbstractCategory $category */
if (!empty($args['category']) && ($category = AbstractObject::getById($args['category']))) {
if (
!empty($args['category']) &&
($category = AbstractObject::getById($args['category'])) &&
$category instanceof AbstractCategory
) {
$resultList->setCategory($category);
}

Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/eventlistener.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ services:
Pimcore\Bundle\DataHubBundle\EventListener\CacheListener:
tags:
- { name: kernel.event_listener, event: pimcore.datahub.graphql.cache.item, method: onCacheItemEvent }
- { name: kernel.event_listener, event: pimcore.system.shutdown, method: onPimcoreShutdown }

Pimcore\Bundle\DataHubBundle\EventListener\AdminListener:
bind:
Expand Down
12 changes: 9 additions & 3 deletions src/Service/OutputCacheService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Pimcore\Http\RequestHelper;
use Pimcore\Logger;
use SplObjectStorage;
use Symfony\Component\Cache\CacheItem;
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
Expand Down Expand Up @@ -226,9 +227,9 @@ public function getOperationCid(OperationParams $operation): string
public function getOperationOutputCid(OperationParams $operation, DocumentNode $parsedQuery): string
{
$cid = $this->getOperationCid($operation);
$cid .= '-' . ($this->operationData[$operation]['filterValues'] ?? '') .
'-' . ($this->operationData[$operation]['sortValues'] ?? '');

$filterValues = $this->operationData[$operation]['filterValues'] ?? '';
$sortValues = $this->operationData[$operation]['sortValues'] ?? '';
$cid .= '-' . self::cleanTag($filterValues) . '-' . self::cleanTag($sortValues);
$event = new OutputCacheGenerateCidEvent($cid, $operation, $parsedQuery);
$this->eventDispatcher->dispatch($event, OutputCacheEvents::GENERATE_CID);

Expand Down Expand Up @@ -486,4 +487,9 @@ private function getImplodedFilterValues(array $variables): string

return implode(',', $filterValues);
}

public static function cleanTag(string $tag): string
{
return str_replace(str_split(CacheItem::RESERVED_CHARACTERS), '-', $tag);
}
}

0 comments on commit 1c3a338

Please sign in to comment.