-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathCacheableClassTrait.php
More file actions
138 lines (113 loc) · 4.02 KB
/
CacheableClassTrait.php
File metadata and controls
138 lines (113 loc) · 4.02 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
<?php
namespace Emag\CacheBundle\ProxyManager;
use Emag\CacheBundle\Annotation\Cache;
use Emag\CacheBundle\Annotation\CacheExpression;
use Emag\CacheBundle\Exception\CacheException;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\Reader;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
trait CacheableClassTrait
{
/**
* Long name to avoid collision
* @var CacheItemPoolInterface
*/
protected $cacheServiceForMethod;
/**
* Long name to avoid colision
* @var AnnotationReader
*
*/
protected $readerForCacheMethod;
protected $__expressionLanguage;
/**
* @param CacheItemPoolInterface $cacheServiceForMethod
*/
public function setCacheServiceForMethod(CacheItemPoolInterface $cacheServiceForMethod)
{
$this->cacheServiceForMethod = $cacheServiceForMethod;
}
/**
* @param Reader $readerForCacheMethod
*/
public function setReaderForCacheMethod(Reader $readerForCacheMethod)
{
$this->readerForCacheMethod = $readerForCacheMethod;
}
public function setExpressionLanguage(ExpressionLanguage $language = null)
{
$this->__expressionLanguage = $language;
}
public function getCached(\ReflectionMethod $method, $params)
{
$method->setAccessible(true);
/** @var Cache $annotation */
$annotation = $this->readerForCacheMethod->getMethodAnnotation($method, Cache::class);
if ($annotation instanceof CacheExpression) {
$annotation
->setContext($this)
->setExpressionLanguage($this->__expressionLanguage)
;
}
$cacheKey = $this->getCacheKey($method, $params, $annotation);
$cacheItem = $this->cacheServiceForMethod->getItem($cacheKey);
if ($cacheItem->isHit() && !$annotation->isReset()) {
return $cacheItem->get();
}
$result = $method->invokeArgs($this, $params);
$cacheItem->set($result);
$cacheItem->expiresAfter($annotation->getTtl());
$this->cacheServiceForMethod->save($cacheItem);
return $result;
}
/**
* @param \ReflectionMethod $method
* @param $params
* @param Cache $cacheObj
* @return string
* @throws CacheException
*/
protected function getCacheKey(\ReflectionMethod $method, $params, Cache $cacheObj)
{
$refParams = $method->getParameters();
$defaultParams = [];
foreach ($refParams as $id => $param) {
try {
$defaultValue = $param->getDefaultValue();
$defaultParams[$id] = $defaultValue;
} catch (\ReflectionException $e) {
//do nothing
}
}
$arguments = $defaultParams;
foreach ($refParams as $id => $param) {
if (array_key_exists($id, $params)) {
$arguments[$id] = $params[$id];
}
}
$cacheKey = '';
if (empty($cacheObj->getKey())) {
$cacheKey = 'no_params_';
}
if (!empty($cacheObj->getKey())) {
$paramsToCache = array_map('trim', explode(',', $cacheObj->getKey()));
$paramsToCache = array_combine($paramsToCache, $paramsToCache);
foreach ($refParams as $id => $param) {
if (in_array($param->getName(), $paramsToCache)) {
if (is_scalar($arguments[$id])) {
$cacheKey .= '_' . $arguments[$id];
} else {
$cacheKey .= '_' . serialize($arguments[$id]);
}
unset($paramsToCache[$param->getName()]);
}
}
if (!empty($paramsToCache)) {
throw new CacheException('Not all requested params can be used in cache key. Missing ' . implode(',', $paramsToCache));
}
}
$cacheKey = $cacheObj->getCache() . sha1($cacheKey);
return $cacheKey;
}
}