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.
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>.
20 namespace Doctrine\Common\Cache;
25 * Memcached cache provider.
27 * @link www.doctrine-project.org
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>
35 class MemcachedCache extends CacheProvider
43 * Sets the memcache instance to use.
45 * @param Memcached $memcached
49 public function setMemcached(Memcached $memcached)
51 $this->memcached = $memcached;
55 * Gets the memcached instance used by the cache.
57 * @return Memcached|null
59 public function getMemcached()
61 return $this->memcached;
67 protected function doFetch($id)
69 return $this->memcached->get($id);
75 protected function doFetchMultiple(array $keys)
77 return $this->memcached->getMulti($keys) ?: [];
83 protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
85 if ($lifetime > 30 * 24 * 3600) {
86 $lifetime = time() + $lifetime;
89 return $this->memcached->setMulti($keysAndValues, $lifetime);
95 protected function doContains($id)
97 $this->memcached->get($id);
99 return $this->memcached->getResultCode() === Memcached::RES_SUCCESS;
105 protected function doSave($id, $data, $lifeTime = 0)
107 if ($lifeTime > 30 * 24 * 3600) {
108 $lifeTime = time() + $lifeTime;
110 return $this->memcached->set($id, $data, (int) $lifeTime);
116 protected function doDeleteMultiple(array $keys)
118 return $this->memcached->deleteMulti($keys)
119 || $this->memcached->getResultCode() === Memcached::RES_NOTFOUND;
125 protected function doDelete($id)
127 return $this->memcached->delete($id)
128 || $this->memcached->getResultCode() === Memcached::RES_NOTFOUND;
134 protected function doFlush()
136 return $this->memcached->flush();
142 protected function doGetStats()
144 $stats = $this->memcached->getStats();
145 $servers = $this->memcached->getServerList();
146 $key = $servers[0]['host'] . ':' . $servers[0]['port'];
147 $stats = $stats[$key];
149 Cache::STATS_HITS => $stats['get_hits'],
150 Cache::STATS_MISSES => $stats['get_misses'],
151 Cache::STATS_UPTIME => $stats['uptime'],
152 Cache::STATS_MEMORY_USAGE => $stats['bytes'],
153 Cache::STATS_MEMORY_AVAILABLE => $stats['limit_maxbytes'],