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 20namespace Doctrine\Common\Cache; 21 22use \Memcache; 23 24/** 25 * Memcache cache provider. 26 * 27 * @link www.doctrine-project.org 28 * @since 2.0 29 * @author Benjamin Eberlei <kontakt@beberlei.de> 30 * @author Guilherme Blanco <guilhermeblanco@hotmail.com> 31 * @author Jonathan Wage <jonwage@gmail.com> 32 * @author Roman Borschel <roman@code-factory.org> 33 * @author David Abdemoulaie <dave@hobodave.com> 34 */ 35class MemcacheCache extends CacheProvider 36{ 37 /** 38 * @var Memcache|null 39 */ 40 private $memcache; 41 42 /** 43 * Sets the memcache instance to use. 44 * 45 * @param Memcache $memcache 46 * 47 * @return void 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|null 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 $flags = null; 78 $this->memcache->get($id, $flags); 79 80 //if memcache has changed the value of "flags", it means the value exists 81 return ($flags !== null); 82 } 83 84 /** 85 * {@inheritdoc} 86 */ 87 protected function doSave($id, $data, $lifeTime = 0) 88 { 89 if ($lifeTime > 30 * 24 * 3600) { 90 $lifeTime = time() + $lifeTime; 91 } 92 return $this->memcache->set($id, $data, 0, (int) $lifeTime); 93 } 94 95 /** 96 * {@inheritdoc} 97 */ 98 protected function doDelete($id) 99 { 100 // Memcache::delete() returns false if entry does not exist 101 return $this->memcache->delete($id) || ! $this->doContains($id); 102 } 103 104 /** 105 * {@inheritdoc} 106 */ 107 protected function doFlush() 108 { 109 return $this->memcache->flush(); 110 } 111 112 /** 113 * {@inheritdoc} 114 */ 115 protected function doGetStats() 116 { 117 $stats = $this->memcache->getStats(); 118 return array( 119 Cache::STATS_HITS => $stats['get_hits'], 120 Cache::STATS_MISSES => $stats['get_misses'], 121 Cache::STATS_UPTIME => $stats['uptime'], 122 Cache::STATS_MEMORY_USAGE => $stats['bytes'], 123 Cache::STATS_MEMORY_AVAILABLE => $stats['limit_maxbytes'], 124 ); 125 } 126} 127