This repository has been archived by the owner on Jul 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCachedSearchProcessor.php
126 lines (105 loc) · 3.95 KB
/
CachedSearchProcessor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
declare(strict_types=1);
/*
* This file is part of the RollerworksSearch package.
*
* (c) Sebastiaan Stok <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Rollerworks\Component\Search\Processor;
use Psr\Http\Message\ServerRequestInterface as ServerRequest;
use Psr\SimpleCache\CacheInterface as Cache;
use Rollerworks\Component\Search\Exception\UnexpectedTypeException;
use Rollerworks\Component\Search\SearchFactory;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface as PropertyAccessor;
/**
* The CachedSearchProcessor caches the SearchPayload
* of a successfully processed search operation.
*
* The data is stored in a PSR-16 (SimpleCache) storage,
* and should not be stored in filesystem or long-term storage.
*
* If your configuration disallows deep nested structures
* it may be a better to not cache the SearchPayload.
*
* @author Sebastiaan Stok <[email protected]>
*/
class CachedSearchProcessor extends AbstractSearchProcessor
{
private $processor;
private $cache;
/**
* Constructor.
*
* @param Cache $cache
* @param SearchProcessor $processor
* @param SearchFactory $searchFactory
* @param PropertyAccessor $propertyAccessor
*/
public function __construct(Cache $cache, SearchProcessor $processor, SearchFactory $searchFactory, PropertyAccessor $propertyAccessor = null)
{
parent::__construct($searchFactory, $propertyAccessor);
$this->cache = $cache;
$this->processor = $processor;
}
/**
* {@inheritdoc}
*/
public function processRequest($request, ProcessorConfig $config): SearchPayload
{
if (!$request instanceof ServerRequest) {
throw new UnexpectedTypeException($request, ServerRequest::class);
}
if (0 === strcasecmp($request->getMethod(), 'POST')) {
$payload = $this->processor->processRequest($request, $config);
$this->storeCache($config, $payload);
return $payload;
}
$currentCode = $this->getRequestParam($request->getQueryParams(), $config, 'search', '');
if (!$currentCode) {
return new SearchPayload();
}
if (null === $payload = $this->loadCondition($config, $currentCode)) {
$payload = $this->processor->processRequest($request, $config);
$this->storeCache($config, $payload);
}
return $payload;
}
private function loadCondition(ProcessorConfig $config, string $searchCode): ?SearchPayload
{
$cacheKey = $this->getConditionCacheKey($config, $searchCode);
/** @var SearchPayload $payload */
if (null !== $payload = $this->cache->get($cacheKey)) {
try {
$payload->searchCondition = $this->searchFactory->getSerializer()->unserialize(
$payload->searchCondition
);
return $payload;
} catch (\Exception $e) {
$this->cache->delete($cacheKey);
}
}
return null;
}
private function storeCache(ProcessorConfig $config, SearchPayload $payload)
{
if ('' === $payload->searchCode || !$payload->isValid()) {
return;
}
// Store the SearchCondition in the custom serialized format to allow deserialization later-on.
$payload = clone $payload;
$payload->changed = false;
$payload->searchCondition = $this->searchFactory->getSerializer()->serialize($payload->searchCondition);
$this->cache->set(
$this->getConditionCacheKey($config, $payload->searchCode),
$payload,
$config->getCacheTTL()
);
}
private function getConditionCacheKey(ProcessorConfig $config, string $searchCode): string
{
return hash('sha256', $config->getFieldSet()->getSetName().'~'.$searchCode);
}
}