|
1
|
|
|
<?php namespace Anomaly\Streams\Platform\View\Cache; |
|
2
|
|
|
|
|
3
|
|
|
use Asm89\Twig\CacheExtension\CacheProviderInterface; |
|
4
|
|
|
use Asm89\Twig\CacheExtension\CacheStrategy\KeyGeneratorInterface; |
|
5
|
|
|
use Asm89\Twig\CacheExtension\CacheStrategyInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class CacheStrategy |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://pyrocms.com/ |
|
11
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
|
12
|
|
|
* @author Ryan Thompson <[email protected]> |
|
13
|
|
|
* @package Anomaly\Streams\Platform\View\Cache |
|
14
|
|
|
*/ |
|
15
|
|
|
class CacheStrategy implements CacheStrategyInterface |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* The cache provider. |
|
20
|
|
|
* |
|
21
|
|
|
* @var CacheProviderInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $cache; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* The key generator. |
|
27
|
|
|
* |
|
28
|
|
|
* @var KeyGeneratorInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
private $generator; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* The default lifetime. |
|
34
|
|
|
* |
|
35
|
|
|
* @var int |
|
36
|
|
|
*/ |
|
37
|
|
|
private $lifetime; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Create a new CacheStrategy instance. |
|
41
|
|
|
* |
|
42
|
|
|
* @param CacheProviderInterface $cache |
|
43
|
|
|
* @param KeyGeneratorInterface $generator |
|
44
|
|
|
* @param integer $lifetime |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(CacheProviderInterface $cache, KeyGeneratorInterface $generator, $lifetime = 0) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->cache = $cache; |
|
49
|
|
|
$this->generator = $generator; |
|
50
|
|
|
$this->lifetime = $lifetime; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Fetch the block for a given key. |
|
55
|
|
|
* |
|
56
|
|
|
* @param mixed $key |
|
57
|
|
|
* |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
public function fetchBlock($key) |
|
61
|
|
|
{ |
|
62
|
|
|
if (is_array($key)) { |
|
63
|
|
|
$key = $key['key']; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $this->cache->fetch($key); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Generate a key for the value. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $annotation |
|
73
|
|
|
* @param mixed $value |
|
74
|
|
|
* |
|
75
|
|
|
* @return mixed |
|
76
|
|
|
*/ |
|
77
|
|
|
public function generateKey($annotation, $value) |
|
78
|
|
|
{ |
|
79
|
|
|
if (is_numeric($value)) { |
|
80
|
|
|
return [ |
|
81
|
|
|
'lifetime' => $value, |
|
82
|
|
|
'key' => '__CS__' . $annotation, |
|
83
|
|
|
]; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$key = $this->generator->generateKey($value); |
|
87
|
|
|
|
|
88
|
|
|
if ($key === null) { |
|
89
|
|
|
throw new \RuntimeException('You must provide a cache key.'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return $annotation . '__CS__' . $key; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Save the contents of a rendered block. |
|
97
|
|
|
* |
|
98
|
|
|
* @param mixed $key |
|
99
|
|
|
* @param string $block |
|
100
|
|
|
*/ |
|
101
|
|
|
public function saveBlock($key, $block) |
|
102
|
|
|
{ |
|
103
|
|
|
if (is_array($key)) { |
|
104
|
|
|
$lifetime = $key['lifetime']; |
|
105
|
|
|
$key = $key['key']; |
|
106
|
|
|
} else { |
|
107
|
|
|
$lifetime = $this->lifetime; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $this->cache->save($key, $block, $lifetime); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|