scratch – Blame information for rev 122
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
120 | office | 1 | <?php |
2 | /* |
||
3 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||
4 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||
5 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||
6 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||
7 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||
8 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||
9 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
10 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
11 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
12 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||
13 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
14 | * |
||
15 | * This software consists of voluntary contributions made by many individuals |
||
16 | * and is licensed under the MIT license. For more information, see |
||
17 | * <http://www.doctrine-project.org>. |
||
18 | */ |
||
19 | |||
20 | namespace Doctrine\Common\Cache; |
||
21 | |||
22 | /** |
||
23 | * Array cache driver. |
||
24 | * |
||
25 | * @link www.doctrine-project.org |
||
26 | * @since 2.0 |
||
27 | * @author Benjamin Eberlei <kontakt@beberlei.de> |
||
28 | * @author Guilherme Blanco <guilhermeblanco@hotmail.com> |
||
29 | * @author Jonathan Wage <jonwage@gmail.com> |
||
30 | * @author Roman Borschel <roman@code-factory.org> |
||
31 | * @author David Abdemoulaie <dave@hobodave.com> |
||
32 | */ |
||
33 | class ArrayCache extends CacheProvider |
||
34 | { |
||
35 | /** |
||
36 | * @var array[] $data each element being a tuple of [$data, $expiration], where the expiration is int|bool |
||
37 | */ |
||
38 | private $data = []; |
||
39 | |||
40 | /** |
||
41 | * @var int |
||
42 | */ |
||
43 | private $hitsCount = 0; |
||
44 | |||
45 | /** |
||
46 | * @var int |
||
47 | */ |
||
48 | private $missesCount = 0; |
||
49 | |||
50 | /** |
||
51 | * @var int |
||
52 | */ |
||
53 | private $upTime; |
||
54 | |||
55 | /** |
||
56 | * {@inheritdoc} |
||
57 | */ |
||
58 | public function __construct() |
||
59 | { |
||
60 | $this->upTime = time(); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * {@inheritdoc} |
||
65 | */ |
||
66 | protected function doFetch($id) |
||
67 | { |
||
68 | if (! $this->doContains($id)) { |
||
69 | $this->missesCount += 1; |
||
70 | |||
71 | return false; |
||
72 | } |
||
73 | |||
74 | $this->hitsCount += 1; |
||
75 | |||
76 | return $this->data[$id][0]; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * {@inheritdoc} |
||
81 | */ |
||
82 | protected function doContains($id) |
||
83 | { |
||
84 | if (! isset($this->data[$id])) { |
||
85 | return false; |
||
86 | } |
||
87 | |||
88 | $expiration = $this->data[$id][1]; |
||
89 | |||
90 | if ($expiration && $expiration < time()) { |
||
91 | $this->doDelete($id); |
||
92 | |||
93 | return false; |
||
94 | } |
||
95 | |||
96 | return true; |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | protected function doSave($id, $data, $lifeTime = 0) |
||
103 | { |
||
104 | $this->data[$id] = [$data, $lifeTime ? time() + $lifeTime : false]; |
||
105 | |||
106 | return true; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | protected function doDelete($id) |
||
113 | { |
||
114 | unset($this->data[$id]); |
||
115 | |||
116 | return true; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * {@inheritdoc} |
||
121 | */ |
||
122 | protected function doFlush() |
||
123 | { |
||
124 | $this->data = []; |
||
125 | |||
126 | return true; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | protected function doGetStats() |
||
133 | { |
||
134 | return [ |
||
135 | Cache::STATS_HITS => $this->hitsCount, |
||
136 | Cache::STATS_MISSES => $this->missesCount, |
||
137 | Cache::STATS_UPTIME => $this->upTime, |
||
138 | Cache::STATS_MEMORY_USAGE => null, |
||
139 | Cache::STATS_MEMORY_AVAILABLE => null, |
||
140 | ]; |
||
141 | } |
||
142 | } |