-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDoctrineCacheBridge.php
149 lines (131 loc) · 3.63 KB
/
DoctrineCacheBridge.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
/*
* This file is part of php-cache organization.
*
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Cache\Bridge\Doctrine;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Cache\CacheProvider;
use Psr\Cache\CacheItemPoolInterface;
/**
* This is a bridge between a Doctrine cache and PSR6.
*
* @author Aaron Scherer <[email protected]>
*/
class DoctrineCacheBridge extends CacheProvider
{
/**
* @type CacheItemPoolInterface
*/
private $cachePool;
/**
* DoctrineCacheBridge constructor.
*
* @param CacheItemPoolInterface $cachePool
*/
public function __construct(CacheItemPoolInterface $cachePool)
{
$this->cachePool = $cachePool;
}
/**
* @return CacheItemPoolInterface
*/
public function getCachePool()
{
return $this->cachePool;
}
/**
* Fetches an entry from the cache.
*
* @param string $id The id of the cache entry to fetch.
*
* @return mixed|false The cached data or FALSE, if no cache entry exists for the given id.
*/
protected function doFetch($id)
{
$item = $this->cachePool->getItem($this->normalizeKey($id));
if ($item->isHit()) {
return $item->get();
}
return false;
}
/**
* Tests if an entry exists in the cache.
*
* @param string $id The cache id of the entry to check for.
*
* @return bool TRUE if a cache entry exists for the given cache id, FALSE otherwise.
*/
protected function doContains($id)
{
return $this->cachePool->hasItem($this->normalizeKey($id));
}
/**
* Puts data into the cache.
*
* @param string $id The cache id.
* @param string $data The cache entry/data.
* @param int $lifeTime The lifetime. If != 0, sets a specific lifetime for this
* cache entry (0 => infinite lifeTime).
*
* @return bool TRUE if the entry was successfully stored in the cache, FALSE otherwise.
*/
protected function doSave($id, $data, $lifeTime = 0)
{
$item = $this->cachePool->getItem($this->normalizeKey($id));
$item->set($data);
if ($lifeTime !== 0) {
$item->expiresAfter($lifeTime);
}
return $this->cachePool->save($item);
}
/**
* Deletes a cache entry.
*
* @param string $id The cache id.
*
* @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise.
*/
protected function doDelete($id)
{
return $this->cachePool->deleteItem($this->normalizeKey($id));
}
/**
* Flushes all cache entries.
*
* @return bool TRUE if the cache entries were successfully flushed, FALSE otherwise.
*/
protected function doFlush()
{
return $this->cachePool->clear();
}
/**
* Retrieves cached information from the data store.
*
* @since 2.2
*
* @return array|null An associative array with server's statistics if available, NULL otherwise.
*/
protected function doGetStats()
{
// Not possible, as of yet
}
/**
* We need to make sure we do not use any characters not supported.
*
* @param string $key
*
* @return string
*/
private function normalizeKey($key)
{
if (preg_match('|[\{\}\(\)/\\\@\:]|', $key)) {
return preg_replace('|[\{\}\(\)/\\\@\:]|', '_', $key);
}
return $key;
}
}