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 LGPL. For more information, see |
---|
18 | * <http://www.doctrine-project.org>. |
---|
19 | */ |
---|
20 | |
---|
21 | namespace Doctrine\Common\Cache; |
---|
22 | |
---|
23 | use \Memcache; |
---|
24 | |
---|
25 | /** |
---|
26 | * Memcache cache provider. |
---|
27 | * |
---|
28 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
---|
29 | * @link www.doctrine-project.org |
---|
30 | * @since 2.0 |
---|
31 | * @author Benjamin Eberlei <kontakt@beberlei.de> |
---|
32 | * @author Guilherme Blanco <guilhermeblanco@hotmail.com> |
---|
33 | * @author Jonathan Wage <jonwage@gmail.com> |
---|
34 | * @author Roman Borschel <roman@code-factory.org> |
---|
35 | * @author David Abdemoulaie <dave@hobodave.com> |
---|
36 | */ |
---|
37 | class MemcacheCache extends CacheProvider |
---|
38 | { |
---|
39 | /** |
---|
40 | * @var Memcache |
---|
41 | */ |
---|
42 | private $memcache; |
---|
43 | |
---|
44 | /** |
---|
45 | * Sets the memcache instance to use. |
---|
46 | * |
---|
47 | * @param Memcache $memcache |
---|
48 | */ |
---|
49 | public function setMemcache(Memcache $memcache) |
---|
50 | { |
---|
51 | $this->memcache = $memcache; |
---|
52 | } |
---|
53 | |
---|
54 | /** |
---|
55 | * Gets the memcache instance used by the cache. |
---|
56 | * |
---|
57 | * @return Memcache |
---|
58 | */ |
---|
59 | public function getMemcache() |
---|
60 | { |
---|
61 | return $this->memcache; |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * {@inheritdoc} |
---|
66 | */ |
---|
67 | protected function doFetch($id) |
---|
68 | { |
---|
69 | return $this->memcache->get($id); |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * {@inheritdoc} |
---|
74 | */ |
---|
75 | protected function doContains($id) |
---|
76 | { |
---|
77 | return (bool) $this->memcache->get($id); |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * {@inheritdoc} |
---|
82 | */ |
---|
83 | protected function doSave($id, $data, $lifeTime = 0) |
---|
84 | { |
---|
85 | if ($lifeTime > 30 * 24 * 3600) { |
---|
86 | $lifeTime = time() + $lifeTime; |
---|
87 | } |
---|
88 | return $this->memcache->set($id, $data, 0, (int) $lifeTime); |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * {@inheritdoc} |
---|
93 | */ |
---|
94 | protected function doDelete($id) |
---|
95 | { |
---|
96 | return $this->memcache->delete($id); |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * {@inheritdoc} |
---|
101 | */ |
---|
102 | protected function doFlush() |
---|
103 | { |
---|
104 | return $this->memcache->flush(); |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * {@inheritdoc} |
---|
109 | */ |
---|
110 | protected function doGetStats() |
---|
111 | { |
---|
112 | $stats = $this->memcache->getStats(); |
---|
113 | return array( |
---|
114 | Cache::STATS_HITS => $stats['get_hits'], |
---|
115 | Cache::STATS_MISSES => $stats['get_misses'], |
---|
116 | Cache::STATS_UPTIME => $stats['uptime'], |
---|
117 | Cache::STATS_MEMORY_USAGE => $stats['bytes'], |
---|
118 | Cache::STATS_MEMORY_AVAILIABLE => $stats['limit_maxbytes'], |
---|
119 | ); |
---|
120 | } |
---|
121 | } |
---|