|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Cache\Adapter; |
| 13 | + |
| 14 | +use Symfony\Component\Cache\Exception\InvalidArgumentException; |
| 15 | + |
| 16 | +/** |
| 17 | + * @author Rob Frawley 2nd <rmf@src.run> |
| 18 | + */ |
| 19 | +class MemcacheAdapter extends AbstractAdapter |
| 20 | +{ |
| 21 | + use MemcacheAdapterTrait; |
| 22 | + |
| 23 | + /** |
| 24 | + * Construct adapter by passing a \Memcache instance and an optional namespace and default cache entry ttl. |
| 25 | + * |
| 26 | + * @param \Memcache $client |
| 27 | + * @param string|null $namespace |
| 28 | + * @param int $defaultLifetime |
| 29 | + */ |
| 30 | + public function __construct(\Memcache $client, $namespace = '', $defaultLifetime = 0) |
| 31 | + { |
| 32 | + parent::__construct($namespace, $defaultLifetime); |
| 33 | + $this->client = $client; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Factory creation method that provides an instance of this adapter with a\Memcache client instantiated and setup. |
| 38 | + * |
| 39 | + * Valid DSN values include the following: |
| 40 | + * - memcache://localhost : Specifies only the host (defaults used for port and weight) |
| 41 | + * - memcache://example.com:1234 : Specifies host and port (defaults weight) |
| 42 | + * - memcache://example.com:1234?weight=50 : Specifies host, port, and weight (no defaults used) |
| 43 | + * |
| 44 | + * @param string|null $dsn |
| 45 | + * |
| 46 | + * @return MemcacheAdapter |
| 47 | + */ |
| 48 | + public static function create($dsn = null) |
| 49 | + { |
| 50 | + if (!extension_loaded('memcache') || !version_compare(phpversion('memcache'), '3.0.8', '>')) { |
| 51 | + throw new InvalidArgumentException('Failed to create memcache client due to missing "memcache" extension or version <3.0.9.'); |
| 52 | + } |
| 53 | + |
| 54 | + $adapter = new static(new \Memcache()); |
| 55 | + $adapter->setup($dsn ? array($dsn) : array()); |
| 56 | + |
| 57 | + return $adapter; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * {@inheritdoc} |
| 62 | + */ |
| 63 | + protected function doSave(array $values, $lifetime) |
| 64 | + { |
| 65 | + $result = true; |
| 66 | + |
| 67 | + foreach ($values as $id => $val) { |
| 68 | + $result = $this->client->set($id, $val, null, $lifetime) && $result; |
| 69 | + } |
| 70 | + |
| 71 | + return $result; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * {@inheritdoc} |
| 76 | + */ |
| 77 | + protected function doFetch(array $ids) |
| 78 | + { |
| 79 | + foreach ($this->client->get($ids) as $id => $val) { |
| 80 | + yield $id => $val; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * {@inheritdoc} |
| 86 | + */ |
| 87 | + protected function doHave($id) |
| 88 | + { |
| 89 | + return $this->client->get($id) !== false; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * {@inheritdoc} |
| 94 | + */ |
| 95 | + protected function doDelete(array $ids) |
| 96 | + { |
| 97 | + $remaining = array_filter($ids, function ($id) { |
| 98 | + return false !== $this->client->get($id) && false === $this->client->delete($id); |
| 99 | + }); |
| 100 | + |
| 101 | + return 0 === count($remaining); |
| 102 | + } |
| 103 | + |
| 104 | + private function getIdsByPrefix($namespace) |
| 105 | + { |
| 106 | + $ids = array(); |
| 107 | + foreach ($this->client->getExtendedStats('slabs') as $slabGroup) { |
| 108 | + foreach ($slabGroup as $slabId => $slabMetadata) { |
| 109 | + if (!is_array($slabMetadata)) { |
| 110 | + continue; |
| 111 | + } |
| 112 | + foreach ($this->client->getExtendedStats('cachedump', (int) $slabId, 1000) as $slabIds) { |
| 113 | + if (is_array($slabIds)) { |
| 114 | + $ids = array_merge($ids, array_keys($slabIds)); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return array_filter((array) $ids, function ($id) use ($namespace) { |
| 121 | + return 0 === strpos($id, $namespace); |
| 122 | + }); |
| 123 | + } |
| 124 | + |
| 125 | + private function addServer($dsn) |
| 126 | + { |
| 127 | + list($host, $port, $weight) = $this->dsnExtract($dsn); |
| 128 | + |
| 129 | + return $this->isServerInClientPool($host, $port) |
| 130 | + || $this->client->addServer($host, $port, false, $weight); |
| 131 | + } |
| 132 | + |
| 133 | + private function setOption($opt, $val) |
| 134 | + { |
| 135 | + return true; |
| 136 | + } |
| 137 | + |
| 138 | + private function isServerInClientPool($host, $port) |
| 139 | + { |
| 140 | + $restore = error_reporting(~E_ALL); |
| 141 | + $srvStat = $this->client->getServerStatus($host, $port); |
| 142 | + error_reporting($restore); |
| 143 | + |
| 144 | + return 1 === $srvStat; |
| 145 | + } |
| 146 | +} |
0 commit comments