Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / common / lib / Doctrine / Common / Cache / CacheProvider.php
1 <?php
2
3 /*
4  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15  *
16  * This software consists of voluntary contributions made by many individuals
17  * and is licensed under the MIT license. For more information, see
18  * <http://www.doctrine-project.org>.
19  */
20
21 namespace Doctrine\Common\Cache;
22
23 /**
24  * Base class for cache provider implementations.
25  *
26  * @since   2.2
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  Fabio B. Silva <fabio.bat.silva@gmail.com>
32  */
33 abstract class CacheProvider implements Cache
34 {
35     const DOCTRINE_NAMESPACE_CACHEKEY = 'DoctrineNamespaceCacheKey[%s]';
36
37     /**
38      * @var string The namespace to prefix all cache ids with
39      */
40     private $namespace = '';
41
42     /**
43      * @var string The namespace version
44      */
45     private $namespaceVersion;
46
47     /**
48      * Set the namespace to prefix all cache ids with.
49      *
50      * @param string $namespace
51      * @return void
52      */
53     public function setNamespace($namespace)
54     {
55         $this->namespace = (string) $namespace;
56     }
57
58     /**
59      * Retrieve the namespace that prefixes all cache ids.
60      *
61      * @return string
62      */
63     public function getNamespace()
64     {
65         return $this->namespace;
66     }
67
68     /**
69      * {@inheritdoc}
70      */
71     public function fetch($id)
72     {
73         return $this->doFetch($this->getNamespacedId($id));
74     }
75
76     /**
77      * {@inheritdoc}
78      */
79     public function contains($id)
80     {
81         return $this->doContains($this->getNamespacedId($id));
82     }
83
84     /**
85      * {@inheritdoc}
86      */
87     public function save($id, $data, $lifeTime = 0)
88     {
89         return $this->doSave($this->getNamespacedId($id), $data, $lifeTime);
90     }
91
92     /**
93      * {@inheritdoc}
94      */
95     public function delete($id)
96     {
97         return $this->doDelete($this->getNamespacedId($id));
98     }
99
100     /**
101      * {@inheritdoc}
102      */
103     public function getStats()
104     {
105         return $this->doGetStats();
106     }
107
108     /**
109      * Deletes all cache entries.
110      *
111      * @return boolean TRUE if the cache entries were successfully flushed, FALSE otherwise.
112      */
113     public function flushAll()
114     {
115         return $this->doFlush();
116     }
117
118     /**
119      * Delete all cache entries.
120      *
121      * @return boolean TRUE if the cache entries were successfully deleted, FALSE otherwise.
122      */
123     public function deleteAll()
124     {
125         $namespaceCacheKey = $this->getNamespaceCacheKey();
126         $namespaceVersion  = $this->getNamespaceVersion() + 1;
127
128         $this->namespaceVersion = $namespaceVersion;
129
130         return $this->doSave($namespaceCacheKey, $namespaceVersion);
131     }
132
133     /**
134      * Prefix the passed id with the configured namespace value
135      *
136      * @param string $id  The id to namespace
137      * @return string $id The namespaced id
138      */
139     private function getNamespacedId($id)
140     {
141         $namespaceVersion  = $this->getNamespaceVersion();
142
143         return sprintf('%s[%s][%s]', $this->namespace, $id, $namespaceVersion);
144     }
145
146     /**
147      * Namespace cache key
148      *
149      * @return string $namespaceCacheKey
150      */
151     private function getNamespaceCacheKey()
152     {
153         return sprintf(self::DOCTRINE_NAMESPACE_CACHEKEY, $this->namespace);
154     }
155
156     /**
157      * Namespace version
158      *
159      * @return string $namespaceVersion
160      */
161     private function getNamespaceVersion()
162     {
163         if (null !== $this->namespaceVersion) {
164             return $this->namespaceVersion;
165         }
166
167         $namespaceCacheKey = $this->getNamespaceCacheKey();
168         $namespaceVersion = $this->doFetch($namespaceCacheKey);
169
170         if (false === $namespaceVersion) {
171             $namespaceVersion = 1;
172
173             $this->doSave($namespaceCacheKey, $namespaceVersion);
174         }
175
176         $this->namespaceVersion = $namespaceVersion;
177
178         return $this->namespaceVersion;
179     }
180
181     /**
182      * Fetches an entry from the cache.
183      *
184      * @param string $id cache id The id of the cache entry to fetch.
185      * @return string The cached data or FALSE, if no cache entry exists for the given id.
186      */
187     abstract protected function doFetch($id);
188
189     /**
190      * Test if an entry exists in the cache.
191      *
192      * @param string $id cache id The cache id of the entry to check for.
193      * @return boolean TRUE if a cache entry exists for the given cache id, FALSE otherwise.
194      */
195     abstract protected function doContains($id);
196
197     /**
198      * Puts data into the cache.
199      *
200      * @param string $id The cache id.
201      * @param string $data The cache entry/data.
202      * @param bool|int $lifeTime The lifetime. If != false, sets a specific lifetime for this
203      *                           cache entry (null => infinite lifeTime).
204      *
205      * @return boolean TRUE if the entry was successfully stored in the cache, FALSE otherwise.
206      */
207     abstract protected function doSave($id, $data, $lifeTime = false);
208
209     /**
210      * Deletes a cache entry.
211      *
212      * @param string $id cache id
213      * @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise.
214      */
215     abstract protected function doDelete($id);
216
217     /**
218      * Deletes all cache entries.
219      *
220      * @return boolean TRUE if the cache entry was successfully deleted, FALSE otherwise.
221      */
222     abstract protected function doFlush();
223
224      /**
225      * Retrieves cached information from data store
226      *
227      * @since   2.2
228      * @return  array An associative array with server's statistics if available, NULL otherwise.
229      */
230     abstract protected function doGetStats();
231 }