1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DoctrineModule\Cache; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Cache\Cache; |
8
|
|
|
use Doctrine\Common\Cache\CacheProvider; |
9
|
|
|
use Laminas\Cache\Storage\AvailableSpaceCapableInterface; |
10
|
|
|
use Laminas\Cache\Storage\FlushableInterface; |
11
|
|
|
use Laminas\Cache\Storage\StorageInterface; |
12
|
|
|
use Laminas\Cache\Storage\TotalSpaceCapableInterface; |
13
|
|
|
use function assert; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Bridge class that allows usage of a Laminas Cache Storage as a Doctrine Cache |
17
|
|
|
* |
18
|
|
|
* @link http://www.doctrine-project.org/ |
19
|
|
|
*/ |
20
|
|
|
class LaminasStorageCache extends CacheProvider |
21
|
|
|
{ |
22
|
|
|
/** @var StorageInterface */ |
23
|
|
|
protected $storage; |
24
|
|
|
|
25
|
5 |
|
public function __construct(StorageInterface $storage) |
26
|
|
|
{ |
27
|
5 |
|
$this->storage = $storage; |
28
|
5 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritDoc} |
32
|
|
|
*/ |
33
|
4 |
|
protected function doFetch($id) |
34
|
|
|
{ |
35
|
4 |
|
$hit = $this->storage->getItem($id); |
36
|
|
|
|
37
|
4 |
|
return $hit ?? false; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritDoc} |
42
|
|
|
*/ |
43
|
4 |
|
protected function doContains($id) |
44
|
|
|
{ |
45
|
4 |
|
return $this->storage->hasItem($id); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* {@inheritDoc} |
50
|
|
|
*/ |
51
|
4 |
|
protected function doSave($id, $data, $lifeTime = false) |
52
|
|
|
{ |
53
|
|
|
// @todo check if lifetime can be set |
54
|
4 |
|
return $this->storage->setItem($id, $data); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritDoc} |
59
|
|
|
*/ |
60
|
1 |
|
protected function doDelete($id) |
61
|
|
|
{ |
62
|
1 |
|
return $this->storage->removeItem($id); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritDoc} |
67
|
|
|
*/ |
68
|
1 |
|
protected function doFlush() |
69
|
|
|
{ |
70
|
1 |
|
if ($this->storage instanceof FlushableInterface) { |
71
|
1 |
|
$storage = $this->storage; |
72
|
1 |
|
assert($storage instanceof FlushableInterface); |
73
|
|
|
|
74
|
1 |
|
return $storage->flush(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritDoc} |
82
|
|
|
*/ |
83
|
1 |
|
protected function doGetStats() |
84
|
|
|
{ |
85
|
1 |
|
$storage = $this->storage; |
86
|
1 |
|
assert($storage instanceof TotalSpaceCapableInterface); |
87
|
1 |
|
assert($storage instanceof AvailableSpaceCapableInterface); |
88
|
|
|
|
89
|
|
|
return [ |
90
|
1 |
|
Cache::STATS_HITS => $this->storage->getMetadata(Cache::STATS_HITS), |
91
|
1 |
|
Cache::STATS_MISSES => $this->storage->getMetadata(Cache::STATS_MISSES), |
92
|
1 |
|
Cache::STATS_UPTIME => $this->storage->getMetadata(Cache::STATS_UPTIME), |
93
|
1 |
|
Cache::STATS_MEMORY_USAGE => $storage instanceof TotalSpaceCapableInterface |
94
|
1 |
|
? $storage->getTotalSpace() |
95
|
|
|
: null, |
96
|
1 |
|
Cache::STATS_MEMORY_AVAILIABLE => $storage instanceof AvailableSpaceCapableInterface |
|
|
|
|
97
|
1 |
|
? $storage->getAvailableSpace() |
98
|
|
|
: null, |
99
|
|
|
]; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
This class constant has been deprecated.