1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hgraca\Cache\Adapter; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\Cache; |
6
|
|
|
use Hgraca\Cache\CacheInterface; |
7
|
|
|
use Hgraca\Cache\Exception\CacheItemNotFoundException; |
8
|
|
|
|
9
|
|
|
final class PhpFileCacheDoctrineAdapter implements Cache |
10
|
|
|
{ |
11
|
|
|
const MODE_VAR_EXPORT = CacheInterface::MODE_VAR_EXPORT; |
12
|
|
|
const MODE_SERIALIZER = CacheInterface::MODE_SERIALIZER; |
13
|
|
|
|
14
|
|
|
/** @var CacheInterface */ |
15
|
|
|
protected $phpFileCache; |
16
|
|
|
|
17
|
6 |
|
public function __construct(CacheInterface $phpFileCache) |
18
|
|
|
{ |
19
|
6 |
|
$this->phpFileCache = $phpFileCache; |
20
|
6 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Fetches an entry from the cache. |
24
|
|
|
* |
25
|
|
|
* @param string $id the id of the cache entry to fetch |
26
|
|
|
* |
27
|
|
|
* @return mixed the cached data or FALSE, if no cache entry exists for the given id |
28
|
|
|
*/ |
29
|
2 |
|
public function fetch($id) |
30
|
|
|
{ |
31
|
|
|
try { |
32
|
2 |
|
return $this->phpFileCache->fetch($id); |
33
|
1 |
|
} catch (CacheItemNotFoundException $e) { |
34
|
1 |
|
return false; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Tests if an entry exists in the cache. |
40
|
|
|
* |
41
|
|
|
* @param string $id the cache id of the entry to check for |
42
|
|
|
* |
43
|
|
|
* @return bool tRUE if a cache entry exists for the given cache id, FALSE otherwise |
44
|
|
|
*/ |
45
|
1 |
|
public function contains($id) |
46
|
|
|
{ |
47
|
1 |
|
return $this->phpFileCache->contains($id); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Puts data into the cache. |
52
|
|
|
* |
53
|
|
|
* If a cache entry with the given id already exists, its data will be replaced. |
54
|
|
|
* |
55
|
|
|
* @param string $id the cache id |
56
|
|
|
* @param mixed $data the cache entry/data |
57
|
|
|
* @param int $lifeTime The lifetime in number of seconds for this cache entry. |
58
|
|
|
* If zero (the default), the entry never expires (although it may be deleted from the cache |
59
|
|
|
* to make place for other entries). |
60
|
|
|
* |
61
|
|
|
* @return bool tRUE if the entry was successfully stored in the cache, FALSE otherwise |
62
|
|
|
*/ |
63
|
1 |
|
public function save($id, $data, $lifeTime = 0) |
64
|
|
|
{ |
65
|
1 |
|
return $this->phpFileCache->save($id, $data, $lifeTime); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Deletes a cache entry. |
70
|
|
|
* |
71
|
|
|
* @param string $id the cache id |
72
|
|
|
* |
73
|
|
|
* @return bool TRUE if the cache entry was successfully deleted, FALSE otherwise. |
74
|
|
|
* Deleting a non-existing entry is considered successful. |
75
|
|
|
*/ |
76
|
1 |
|
public function delete($id) |
77
|
|
|
{ |
78
|
1 |
|
return $this->phpFileCache->delete($id); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Retrieves cached information from the data store. |
83
|
|
|
* |
84
|
|
|
* The server's statistics array has the following values: |
85
|
|
|
* |
86
|
|
|
* - <b>hits</b> |
87
|
|
|
* Number of keys that have been requested and found present. |
88
|
|
|
* |
89
|
|
|
* - <b>misses</b> |
90
|
|
|
* Number of items that have been requested and not found. |
91
|
|
|
* |
92
|
|
|
* - <b>uptime</b> |
93
|
|
|
* Time that the server is running. |
94
|
|
|
* |
95
|
|
|
* - <b>memory_usage</b> |
96
|
|
|
* Memory used by this server to store items. |
97
|
|
|
* |
98
|
|
|
* - <b>memory_available</b> |
99
|
|
|
* Memory allowed to use for storage. |
100
|
|
|
* |
101
|
|
|
* @since 2.2 |
102
|
|
|
* |
103
|
|
|
* @return array|null an associative array with server's statistics if available, NULL otherwise |
104
|
|
|
*/ |
105
|
1 |
|
public function getStats() |
106
|
|
|
{ |
107
|
1 |
|
$stats = $this->phpFileCache->getStats(); |
108
|
|
|
|
109
|
1 |
|
unset($stats[CacheInterface::STATS_ITEM_COUNT]); |
110
|
|
|
|
111
|
1 |
|
return $stats; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|