|
23 | 23 | use Pimcore\Model\Document\Link;
|
24 | 24 | use Pimcore\Model\Document\PageSnippet;
|
25 | 25 | use Pimcore\Model\Element\AbstractElement;
|
| 26 | +use Symfony\Component\EventDispatcher\GenericEvent; |
26 | 27 |
|
27 | 28 | class CacheListener
|
28 | 29 | {
|
@@ -58,18 +59,13 @@ public static function addCachingItem(
|
58 | 59 | self::$cachingItems = new \SplObjectStorage();
|
59 | 60 | }
|
60 | 61 |
|
61 |
| - // Load object to get cache tags. |
62 |
| - $object = Concrete::getById($objectId); |
63 |
| - if ($object) { |
64 |
| - self::$cachingItems[$operation] = self::$cachingItems[$operation] ?? []; |
65 |
| - $cacheTags = ['datahub-cache'] + (self::getObjectCacheTags($object) ?? []); |
66 |
| - self::$cachingItems[$operation] += [$cid => [ |
67 |
| - 'path' => $path, |
68 |
| - 'tags' => $cacheTags, |
69 |
| - 'indexKey' => $indexKey, |
70 |
| - 'lifetime' => $lifetime, |
71 |
| - ]]; |
72 |
| - } |
| 62 | + self::$cachingItems[$operation] = self::$cachingItems[$operation] ?? []; |
| 63 | + self::$cachingItems[$operation] += [$cid => [ |
| 64 | + 'objectId' => $objectId, |
| 65 | + 'path' => $path, |
| 66 | + 'indexKey' => $indexKey, |
| 67 | + 'lifetime' => $lifetime, |
| 68 | + ]]; |
73 | 69 | }
|
74 | 70 |
|
75 | 71 | public static function clearCachingItems(OperationParams $operation = null): void
|
@@ -100,35 +96,72 @@ public static function arrayGetNestedValue(array &$array, array $parents, &$key_
|
100 | 96 | return $ref;
|
101 | 97 | }
|
102 | 98 |
|
| 99 | + /** |
| 100 | + * Add the cache item meta-data before saving the cache items. |
| 101 | + * |
| 102 | + * Uses shutdown because at this point the response should be already |
| 103 | + * delivered and any further processing shouldn't affect reponse times. |
| 104 | + * |
| 105 | + * @param \Symfony\Component\EventDispatcher\GenericEvent $event |
| 106 | + * |
| 107 | + * @return void |
| 108 | + */ |
| 109 | + public function onPimcoreShutdown(GenericEvent $event): void |
| 110 | + { |
| 111 | + foreach (self::$cachingItems ?? [] as $operation) { |
| 112 | + // Find items declared for caching in result set and store them in cache. |
| 113 | + foreach (self::$cachingItems[$operation] as $cid => $item) { |
| 114 | + // Extract the related cache tags. |
| 115 | + $cacheTags = []; |
| 116 | + $object = Concrete::getById($item['objectId']); |
| 117 | + if (!empty($object)) { |
| 118 | + $cacheTags = ['datahub-cache'] + (self::getObjectCacheTags($object) ?? []); |
| 119 | + } |
| 120 | + // Not sure why force is necessary. |
| 121 | + Cache::save( |
| 122 | + $item['data'], |
| 123 | + $cid, |
| 124 | + $cacheTags, |
| 125 | + $item['lifetime'] ?? null, |
| 126 | + 0, |
| 127 | + true |
| 128 | + ); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Adds the actual data to the for caching prepared cache items. |
| 135 | + * |
| 136 | + * Do NOT save yet - wait for that till shutdown to ensure response is |
| 137 | + * out before triggering any further processing. |
| 138 | + * |
| 139 | + * |
| 140 | + * @param \Pimcore\Bundle\DataHubBundle\Event\GraphQL\Model\CacheItemEvent $event |
| 141 | + * |
| 142 | + * @return void |
| 143 | + */ |
103 | 144 | public function onCacheItemEvent(CacheItemEvent $event): void
|
104 | 145 | {
|
105 | 146 | $operation = $event->getOperation();
|
106 |
| - |
107 | 147 | if (isset(self::$cachingItems[$operation])) {
|
108 | 148 | $result = $event->getResult();
|
109 | 149 | $data = $result->data;
|
110 |
| - |
111 | 150 | // Find items declared for caching in result set and store them in cache.
|
112 |
| - foreach (self::$cachingItems[$operation] as $cid => $item) { |
| 151 | + $cachedItems = self::$cachingItems[$operation]; |
| 152 | + foreach ($cachedItems as $cid => $item) { |
113 | 153 | // replace the "delta" placeholder with the effective index to extract data
|
114 | 154 | $path = $item['path'];
|
115 | 155 | if ($index = array_search('delta', $path, true)) {
|
116 | 156 | $path[$index] = $item['indexKey'];
|
117 | 157 | }
|
118 | 158 | // Extract the cacheable portion from the result.
|
119 | 159 | $value = self::arrayGetNestedValue($data, $path);
|
120 |
| - $cacheTags = $item['tags'] ?? []; |
121 |
| - Cache::save( |
122 |
| - $value, |
123 |
| - $cid, |
124 |
| - $cacheTags, |
125 |
| - $item['lifetime'] ?? null, |
126 |
| - 0, |
127 |
| - true |
128 |
| - ); |
| 160 | + $item['data'] = $value; |
| 161 | + $cachedItems[$cid] = $item; |
| 162 | + self::$cachingItems->offsetSet($operation, $cachedItems); |
129 | 163 | }
|
130 | 164 | }
|
131 |
| - self::clearCachingItems($operation); |
132 | 165 | }
|
133 | 166 |
|
134 | 167 | /**
|
|
0 commit comments