1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* This file is part of phpFastCache. |
6
|
|
|
* |
7
|
|
|
* @license MIT License (MIT) |
8
|
|
|
* |
9
|
|
|
* For full copyright and license information, please see the docs/CREDITS.txt file. |
10
|
|
|
* |
11
|
|
|
* @author Georges.L (Geolim4) <[email protected]> |
12
|
|
|
* @author PastisD https://github.com/PastisD |
13
|
|
|
* @author Alexander (asm89) <[email protected]> |
14
|
|
|
* @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com |
15
|
|
|
* |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace phpFastCache\Bundle\Twig\CacheExtension\CacheStrategy; |
19
|
|
|
|
20
|
|
|
use phpFastCache\Bundle\Twig\CacheExtension\CacheProviderInterface; |
21
|
|
|
use phpFastCache\Bundle\Twig\CacheExtension\CacheStrategyInterface; |
22
|
|
|
use phpFastCache\Bundle\Twig\CacheExtension\Exception\InvalidCacheLifetimeException; |
23
|
|
|
use phpFastCache\Bundle\DataCollector\CacheCollector; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Strategy for caching with a pre-defined lifetime. |
27
|
|
|
* |
28
|
|
|
* The value passed to the strategy is the lifetime of the cache block in |
29
|
|
|
* seconds. |
30
|
|
|
* |
31
|
|
|
* @author Alexander <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class LifetimeCacheStrategy implements CacheStrategyInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
private $twigCachePrefix = '_TwigCacheLCS_'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
private $config = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var \phpFastCache\Bundle\Twig\CacheExtension\CacheProviderInterface |
47
|
|
|
*/ |
48
|
|
|
private $cache; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @var \phpFastCache\Bundle\DataCollector\CacheCollector |
52
|
|
|
*/ |
53
|
|
|
private $cacheCollector; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* LifetimeCacheStrategy constructor. |
57
|
|
|
* @param \phpFastCache\Bundle\Twig\CacheExtension\CacheProviderInterface $cache |
58
|
|
|
* @param \phpFastCache\Bundle\DataCollector\CacheCollector $cacheCollector |
59
|
|
|
* @param array $config |
60
|
|
|
*/ |
61
|
|
|
public function __construct(CacheProviderInterface $cache, CacheCollector $cacheCollector = null, $config) |
62
|
|
|
{ |
63
|
|
|
$this->cache = $cache; |
64
|
|
|
$this->cacheCollector = $cacheCollector; |
65
|
|
|
$this->config = (array) $config; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* {@inheritDoc} |
70
|
|
|
*/ |
71
|
|
|
public function fetchBlock($key) |
72
|
|
|
{ |
73
|
|
|
$generationTimeMc = microtime(true); |
74
|
|
|
$cacheData = $this->cache->fetch($key['key']); |
75
|
|
|
$generationTime = microtime(true) - $generationTimeMc; |
76
|
|
|
$unprefixedKey = substr($key['key'], strlen($this->twigCachePrefix)); |
77
|
|
|
|
78
|
|
View Code Duplication |
if($this->cacheCollector instanceof CacheCollector){ |
|
|
|
|
79
|
|
|
$this->cacheCollector->setTwigCacheBlock($unprefixedKey, [ |
80
|
|
|
'cacheHit' => $cacheData !== null, |
81
|
|
|
'cacheTtl' => $key['lifetime'], |
82
|
|
|
'cacheSize' => mb_strlen((string) $cacheData), |
83
|
|
|
'cacheGenTime' => $generationTime |
84
|
|
|
]); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if(!empty($cacheData) && $this->config['twig_block_debug']){ |
88
|
|
|
return "<!-- BEGIN CACHE BLOCK OUTPUT '{$unprefixedKey}' -->\n{$cacheData}\n<!-- // END CACHE BLOCK OUTPUT '{$unprefixedKey}' -->"; |
89
|
|
|
}else{ |
90
|
|
|
return $cacheData; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritDoc} |
96
|
|
|
*/ |
97
|
|
|
public function generateKey($annotation, $value) |
98
|
|
|
{ |
99
|
|
|
if (!is_numeric($value)) { |
100
|
|
|
throw new InvalidCacheLifetimeException($value); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return array( |
104
|
|
|
'lifetime' => $value, |
105
|
|
|
'key' => $this->twigCachePrefix . $annotation, |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritDoc} |
111
|
|
|
*/ |
112
|
|
|
public function saveBlock($key, $block, $generationTime) |
113
|
|
|
{ |
114
|
|
|
$unprefixedKey = substr($key['key'], strlen($this->twigCachePrefix)); |
115
|
|
|
|
116
|
|
View Code Duplication |
if($this->cacheCollector instanceof CacheCollector){ |
|
|
|
|
117
|
|
|
$this->cacheCollector->setTwigCacheBlock($unprefixedKey, [ |
118
|
|
|
'cacheHit' => false, |
119
|
|
|
'cacheTtl' => $key['lifetime'], |
120
|
|
|
'cacheSize' => mb_strlen((string) $block), |
121
|
|
|
'cacheGenTime' => $generationTime |
122
|
|
|
]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return $this->cache->save($key['key'], $block, $key['lifetime']); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.