1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Codefocus\ManagedCache; |
4
|
|
|
|
5
|
|
|
use BadFunctionCallException; |
6
|
|
|
use Codefocus\ManagedCache\Events\Event; |
7
|
|
|
use Codefocus\ManagedCache\Traits\HandlesEloquentEvents; |
8
|
|
|
use Exception; |
9
|
|
|
use Illuminate\Cache\MemcachedStore; |
10
|
|
|
use Illuminate\Cache\Repository as CacheRepository; |
11
|
|
|
use Illuminate\Foundation\Application; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* ManagedCache. |
15
|
|
|
* |
16
|
|
|
* @method DefinitionChain setForgetConditions(array $conditions) |
17
|
|
|
*/ |
18
|
|
|
class ManagedCache |
19
|
|
|
{ |
20
|
|
|
use HandlesEloquentEvents; |
21
|
|
|
|
22
|
|
|
// Cache keys. |
23
|
|
|
const TAG_MAP_CACHE_KEY = 'ManagedCache_TagMap'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Application |
27
|
|
|
*/ |
28
|
|
|
protected $application; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var CacheRepository |
32
|
|
|
*/ |
33
|
|
|
protected $store; |
34
|
|
|
|
35
|
|
|
private $isDebugModeEnabled = false; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Maps cache keys to their tags. |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
protected $tagMap = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor. |
46
|
|
|
* |
47
|
|
|
* @param Application $application |
48
|
|
|
*/ |
49
|
|
|
public function __construct(Application $application) |
50
|
|
|
{ |
51
|
|
|
// Inject Application. |
52
|
|
|
$this->application = $application; |
53
|
|
|
// Get the configured cache store. |
54
|
|
|
$this->store = $this->application['cache.store']; |
55
|
|
|
if ( ! ($this->store->getStore() instanceof MemcachedStore)) { |
56
|
|
|
throw new Exception('Memcached not configured. Cache store is "' . class_basename($this->store) . '"'); |
57
|
|
|
} |
58
|
|
|
// Register the event listeners. |
59
|
|
|
$this->registerEventListener($this->application['events']); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function enableDebugMode() |
63
|
|
|
{ |
64
|
|
|
$this->isDebugModeEnabled = true; |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function isDebugModeEnabled() |
70
|
|
|
{ |
71
|
|
|
return $this->isDebugModeEnabled; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getTagMap(): array |
75
|
|
|
{ |
76
|
|
|
if (empty($this->tagMap)) { |
77
|
|
|
$this->tagMap = $this->store->get(self::TAG_MAP_CACHE_KEY, []); |
78
|
|
|
if ( ! is_array($this->tagMap)) { |
79
|
|
|
$this->tagMap = []; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this->tagMap; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getTagsForKey(string $key): array |
87
|
|
|
{ |
88
|
|
|
$tagMap = $this->getTagMap(); |
89
|
|
|
if ( ! isset($tagMap[$key])) { |
90
|
|
|
return []; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $tagMap[$key]; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function setTagsForKey(string $key, array $tags): void |
97
|
|
|
{ |
98
|
|
|
$this->getTagMap(); |
99
|
|
|
$this->tagMap[$key] = $tags; |
100
|
|
|
$this->store->forever(self::TAG_MAP_CACHE_KEY, $this->tagMap); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function deleteTagsForKey(string $key) |
104
|
|
|
{ |
105
|
|
|
$this->getTagMap(); |
106
|
|
|
if (isset($this->tagMap[$key])) { |
107
|
|
|
unset($this->tagMap[$key]); |
108
|
|
|
$this->store->forever(self::TAG_MAP_CACHE_KEY, $this->tagMap); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns the Cache store instance. |
114
|
|
|
* |
115
|
|
|
* @return CacheRepository |
116
|
|
|
*/ |
117
|
|
|
public function getStore(): CacheRepository |
118
|
|
|
{ |
119
|
|
|
return $this->store; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Route function calls to a new DefinitionChain. |
124
|
|
|
* |
125
|
|
|
* @param string $name |
126
|
|
|
* @param array $arguments |
127
|
|
|
* |
128
|
|
|
* @throws BadFunctionCallException |
129
|
|
|
*/ |
130
|
|
|
public function __call(string $name, array $arguments) |
131
|
|
|
{ |
132
|
|
|
$definitionChain = new DefinitionChain($this); |
133
|
|
|
if ( ! method_exists($definitionChain, $name)) { |
134
|
|
|
throw new BadFunctionCallException('Function ' . $name . ' does not exist.'); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return call_user_func_array([$definitionChain, $name], $arguments); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|