annotate vendor/doctrine/cache/lib/Doctrine/Common/Cache/MemcachedCache.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 7a779792577d
children
rev   line source
Chris@0 1 <?php
Chris@0 2 /*
Chris@0 3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Chris@0 4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Chris@0 5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Chris@0 6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Chris@0 7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Chris@0 8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Chris@0 9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Chris@0 10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Chris@0 11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Chris@0 12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Chris@0 13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@0 14 *
Chris@0 15 * This software consists of voluntary contributions made by many individuals
Chris@0 16 * and is licensed under the MIT license. For more information, see
Chris@0 17 * <http://www.doctrine-project.org>.
Chris@0 18 */
Chris@0 19
Chris@0 20 namespace Doctrine\Common\Cache;
Chris@0 21
Chris@0 22 use \Memcached;
Chris@0 23
Chris@0 24 /**
Chris@0 25 * Memcached cache provider.
Chris@0 26 *
Chris@0 27 * @link www.doctrine-project.org
Chris@0 28 * @since 2.2
Chris@0 29 * @author Benjamin Eberlei <kontakt@beberlei.de>
Chris@0 30 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
Chris@0 31 * @author Jonathan Wage <jonwage@gmail.com>
Chris@0 32 * @author Roman Borschel <roman@code-factory.org>
Chris@0 33 * @author David Abdemoulaie <dave@hobodave.com>
Chris@0 34 */
Chris@0 35 class MemcachedCache extends CacheProvider
Chris@0 36 {
Chris@0 37 /**
Chris@0 38 * @var Memcached|null
Chris@0 39 */
Chris@0 40 private $memcached;
Chris@0 41
Chris@0 42 /**
Chris@0 43 * Sets the memcache instance to use.
Chris@0 44 *
Chris@0 45 * @param Memcached $memcached
Chris@0 46 *
Chris@0 47 * @return void
Chris@0 48 */
Chris@0 49 public function setMemcached(Memcached $memcached)
Chris@0 50 {
Chris@0 51 $this->memcached = $memcached;
Chris@0 52 }
Chris@0 53
Chris@0 54 /**
Chris@0 55 * Gets the memcached instance used by the cache.
Chris@0 56 *
Chris@0 57 * @return Memcached|null
Chris@0 58 */
Chris@0 59 public function getMemcached()
Chris@0 60 {
Chris@0 61 return $this->memcached;
Chris@0 62 }
Chris@0 63
Chris@0 64 /**
Chris@0 65 * {@inheritdoc}
Chris@0 66 */
Chris@0 67 protected function doFetch($id)
Chris@0 68 {
Chris@0 69 return $this->memcached->get($id);
Chris@0 70 }
Chris@0 71
Chris@0 72 /**
Chris@0 73 * {@inheritdoc}
Chris@0 74 */
Chris@0 75 protected function doFetchMultiple(array $keys)
Chris@0 76 {
Chris@0 77 return $this->memcached->getMulti($keys) ?: [];
Chris@0 78 }
Chris@0 79
Chris@0 80 /**
Chris@0 81 * {@inheritdoc}
Chris@0 82 */
Chris@0 83 protected function doSaveMultiple(array $keysAndValues, $lifetime = 0)
Chris@0 84 {
Chris@0 85 if ($lifetime > 30 * 24 * 3600) {
Chris@0 86 $lifetime = time() + $lifetime;
Chris@0 87 }
Chris@0 88
Chris@0 89 return $this->memcached->setMulti($keysAndValues, $lifetime);
Chris@0 90 }
Chris@0 91
Chris@0 92 /**
Chris@0 93 * {@inheritdoc}
Chris@0 94 */
Chris@0 95 protected function doContains($id)
Chris@0 96 {
Chris@12 97 $this->memcached->get($id);
Chris@12 98
Chris@12 99 return $this->memcached->getResultCode() === Memcached::RES_SUCCESS;
Chris@0 100 }
Chris@0 101
Chris@0 102 /**
Chris@0 103 * {@inheritdoc}
Chris@0 104 */
Chris@0 105 protected function doSave($id, $data, $lifeTime = 0)
Chris@0 106 {
Chris@0 107 if ($lifeTime > 30 * 24 * 3600) {
Chris@0 108 $lifeTime = time() + $lifeTime;
Chris@0 109 }
Chris@0 110 return $this->memcached->set($id, $data, (int) $lifeTime);
Chris@0 111 }
Chris@0 112
Chris@0 113 /**
Chris@0 114 * {@inheritdoc}
Chris@0 115 */
Chris@0 116 protected function doDelete($id)
Chris@0 117 {
Chris@0 118 return $this->memcached->delete($id)
Chris@0 119 || $this->memcached->getResultCode() === Memcached::RES_NOTFOUND;
Chris@0 120 }
Chris@0 121
Chris@0 122 /**
Chris@0 123 * {@inheritdoc}
Chris@0 124 */
Chris@0 125 protected function doFlush()
Chris@0 126 {
Chris@0 127 return $this->memcached->flush();
Chris@0 128 }
Chris@0 129
Chris@0 130 /**
Chris@0 131 * {@inheritdoc}
Chris@0 132 */
Chris@0 133 protected function doGetStats()
Chris@0 134 {
Chris@0 135 $stats = $this->memcached->getStats();
Chris@0 136 $servers = $this->memcached->getServerList();
Chris@0 137 $key = $servers[0]['host'] . ':' . $servers[0]['port'];
Chris@0 138 $stats = $stats[$key];
Chris@0 139 return array(
Chris@0 140 Cache::STATS_HITS => $stats['get_hits'],
Chris@0 141 Cache::STATS_MISSES => $stats['get_misses'],
Chris@0 142 Cache::STATS_UPTIME => $stats['uptime'],
Chris@0 143 Cache::STATS_MEMORY_USAGE => $stats['bytes'],
Chris@0 144 Cache::STATS_MEMORY_AVAILABLE => $stats['limit_maxbytes'],
Chris@0 145 );
Chris@0 146 }
Chris@0 147 }