Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 30 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| memcached | |
0.00% |
0 / 22 |
|
0.00% |
0 / 6 |
182 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
56 | |||
| unload | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| purge | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| _read | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| _write | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| _delete | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * |
| 4 | * This file is part of the phpBB Forum Software package. |
| 5 | * |
| 6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
| 7 | * @license GNU General Public License, version 2 (GPL-2.0) |
| 8 | * |
| 9 | * For full copyright and license information, please see |
| 10 | * the docs/CREDITS.txt file. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | namespace phpbb\cache\driver; |
| 15 | |
| 16 | if (!defined('PHPBB_ACM_MEMCACHED_PORT')) |
| 17 | { |
| 18 | define('PHPBB_ACM_MEMCACHED_PORT', 11211); |
| 19 | } |
| 20 | |
| 21 | if (!defined('PHPBB_ACM_MEMCACHED_COMPRESS')) |
| 22 | { |
| 23 | define('PHPBB_ACM_MEMCACHED_COMPRESS', true); |
| 24 | } |
| 25 | |
| 26 | if (!defined('PHPBB_ACM_MEMCACHED_HOST')) |
| 27 | { |
| 28 | define('PHPBB_ACM_MEMCACHED_HOST', 'localhost'); |
| 29 | } |
| 30 | |
| 31 | if (!defined('PHPBB_ACM_MEMCACHED')) |
| 32 | { |
| 33 | //can define multiple servers with host1/port1,host2/port2 format |
| 34 | define('PHPBB_ACM_MEMCACHED', PHPBB_ACM_MEMCACHED_HOST . '/' . PHPBB_ACM_MEMCACHED_PORT); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * ACM for Memcached |
| 39 | */ |
| 40 | class memcached extends memory |
| 41 | { |
| 42 | /** @var string Extension to use */ |
| 43 | protected $extension = 'memcached'; |
| 44 | |
| 45 | /** @var \Memcached Memcached class */ |
| 46 | protected $memcached; |
| 47 | |
| 48 | /** @var int Flags */ |
| 49 | protected $flags = 0; |
| 50 | |
| 51 | /** |
| 52 | * Memcached constructor |
| 53 | * |
| 54 | * @param string $memcached_servers Memcached servers string (optional) |
| 55 | */ |
| 56 | public function __construct($memcached_servers = '') |
| 57 | { |
| 58 | // Call the parent constructor |
| 59 | parent::__construct(); |
| 60 | |
| 61 | $memcached_servers = $memcached_servers ?: PHPBB_ACM_MEMCACHED; |
| 62 | |
| 63 | $this->memcached = new \Memcached(); |
| 64 | $this->memcached->setOption(\Memcached::OPT_BINARY_PROTOCOL, true); |
| 65 | // Memcached defaults to using compression, disable if we don't want |
| 66 | // to use it |
| 67 | if (!PHPBB_ACM_MEMCACHED_COMPRESS) |
| 68 | { |
| 69 | $this->memcached->setOption(\Memcached::OPT_COMPRESSION, false); |
| 70 | } |
| 71 | |
| 72 | $server_list = []; |
| 73 | foreach (explode(',', $memcached_servers) as $u) |
| 74 | { |
| 75 | if (preg_match('#(.*)/(\d+)#', $u, $parts)) |
| 76 | { |
| 77 | $server_list[] = [trim($parts[1]), (int) trim($parts[2])]; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | $this->memcached->addServers($server_list); |
| 82 | |
| 83 | if (empty($server_list) || empty($this->memcached->getStats())) |
| 84 | { |
| 85 | trigger_error('Could not connect to memcached server(s).'); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * {@inheritDoc} |
| 91 | */ |
| 92 | public function unload() |
| 93 | { |
| 94 | parent::unload(); |
| 95 | |
| 96 | unset($this->memcached); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * {@inheritDoc} |
| 101 | */ |
| 102 | public function purge() |
| 103 | { |
| 104 | $this->memcached->flush(); |
| 105 | |
| 106 | parent::purge(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * {@inheritDoc} |
| 111 | */ |
| 112 | protected function _read(string $var) |
| 113 | { |
| 114 | return $this->memcached->get($this->key_prefix . $var); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * {@inheritDoc} |
| 119 | */ |
| 120 | protected function _write(string $var, $data, int $ttl = 2592000): bool |
| 121 | { |
| 122 | if (!$this->memcached->replace($this->key_prefix . $var, $data, $ttl)) |
| 123 | { |
| 124 | return $this->memcached->set($this->key_prefix . $var, $data, $ttl); |
| 125 | } |
| 126 | return true; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * {@inheritDoc} |
| 131 | */ |
| 132 | protected function _delete(string $var): bool |
| 133 | { |
| 134 | return $this->memcached->delete($this->key_prefix . $var); |
| 135 | } |
| 136 | } |