Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| redis | |
0.00% |
0 / 27 |
|
0.00% |
0 / 6 |
182 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 18 |
|
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 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| _delete | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 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_REDIS_PORT')) |
| 17 | { |
| 18 | define('PHPBB_ACM_REDIS_PORT', 6379); |
| 19 | } |
| 20 | |
| 21 | if (!defined('PHPBB_ACM_REDIS_HOST')) |
| 22 | { |
| 23 | define('PHPBB_ACM_REDIS_HOST', 'localhost'); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * ACM for Redis |
| 28 | * |
| 29 | * Compatible with the php extension phpredis available |
| 30 | * at https://github.com/nicolasff/phpredis |
| 31 | * |
| 32 | */ |
| 33 | class redis extends \phpbb\cache\driver\memory |
| 34 | { |
| 35 | var $extension = 'redis'; |
| 36 | |
| 37 | var $redis; |
| 38 | |
| 39 | /** |
| 40 | * Creates a redis cache driver. |
| 41 | * |
| 42 | * The following global constants affect operation: |
| 43 | * |
| 44 | * PHPBB_ACM_REDIS_HOST |
| 45 | * PHPBB_ACM_REDIS_PORT |
| 46 | * PHPBB_ACM_REDIS_PASSWORD |
| 47 | * PHPBB_ACM_REDIS_DB |
| 48 | * |
| 49 | * There are no publicly documented constructor parameters. |
| 50 | */ |
| 51 | function __construct() |
| 52 | { |
| 53 | // Call the parent constructor |
| 54 | parent::__construct(); |
| 55 | |
| 56 | $this->redis = new \Redis(); |
| 57 | |
| 58 | $args = func_get_args(); |
| 59 | if (!empty($args)) |
| 60 | { |
| 61 | $ok = call_user_func_array(array($this->redis, 'connect'), $args); |
| 62 | } |
| 63 | else |
| 64 | { |
| 65 | $ok = $this->redis->connect(PHPBB_ACM_REDIS_HOST, PHPBB_ACM_REDIS_PORT); |
| 66 | } |
| 67 | |
| 68 | if (!$ok) |
| 69 | { |
| 70 | trigger_error('Could not connect to redis server'); |
| 71 | } |
| 72 | |
| 73 | if (defined('PHPBB_ACM_REDIS_PASSWORD')) |
| 74 | { |
| 75 | if (!$this->redis->auth(PHPBB_ACM_REDIS_PASSWORD)) |
| 76 | { |
| 77 | global $acm_type; |
| 78 | |
| 79 | trigger_error("Incorrect password for the ACM module $acm_type.", E_USER_ERROR); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | $this->redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP); |
| 84 | $this->redis->setOption(\Redis::OPT_PREFIX, $this->key_prefix); |
| 85 | |
| 86 | if (defined('PHPBB_ACM_REDIS_DB')) |
| 87 | { |
| 88 | if (!$this->redis->select(PHPBB_ACM_REDIS_DB)) |
| 89 | { |
| 90 | global $acm_type; |
| 91 | |
| 92 | trigger_error("Incorrect database for the ACM module $acm_type.", E_USER_ERROR); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * {@inheritDoc} |
| 99 | */ |
| 100 | function unload() |
| 101 | { |
| 102 | parent::unload(); |
| 103 | |
| 104 | $this->redis->close(); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * {@inheritDoc} |
| 109 | */ |
| 110 | function purge() |
| 111 | { |
| 112 | $this->redis->flushDB(); |
| 113 | |
| 114 | parent::purge(); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * {@inheritDoc} |
| 119 | */ |
| 120 | protected function _read(string $var) |
| 121 | { |
| 122 | return $this->redis->get($var); |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Store data in the cache |
| 127 | * |
| 128 | * For the info, see https://phpredis.github.io/phpredis/Redis.html#method_set, |
| 129 | * https://redis.io/docs/latest/commands/set/ |
| 130 | * and https://redis.io/docs/latest/commands/expire/#appendix-redis-expires |
| 131 | * |
| 132 | * {@inheritDoc} |
| 133 | */ |
| 134 | protected function _write(string $var, $data, int $ttl = 2592000): bool |
| 135 | { |
| 136 | return $this->redis->set($var, $data, ['EXAT' => time() + $ttl]); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * {@inheritDoc} |
| 141 | */ |
| 142 | protected function _delete(string $var): bool |
| 143 | { |
| 144 | if ($this->redis->del($var) > 0) |
| 145 | { |
| 146 | return true; |
| 147 | } |
| 148 | return false; |
| 149 | } |
| 150 | } |