|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This class provides connections to Redis for short-term caching |
| 5 | + * |
| 6 | + * @package StoryBB (storybb.org) - A roleplayer's forum software |
| 7 | + * @copyright 2018 StoryBB and individual contributors (see contributors.txt) |
| 8 | + * @license 3-clause BSD (see accompanying LICENSE file) |
| 9 | + * |
| 10 | + * @version 3.0 Alpha 1 |
| 11 | + */ |
| 12 | + |
| 13 | +namespace StoryBB\Cache; |
| 14 | + |
| 15 | +/** |
| 16 | + * Our Cache API class |
| 17 | + * @package cacheAPI |
| 18 | + */ |
| 19 | +class Redis extends API |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var \Redis The memcache instance. |
| 23 | + */ |
| 24 | + private $redis = null; |
| 25 | + |
| 26 | + /** |
| 27 | + * Checks whether we can use the cache method performed by this API. |
| 28 | + * |
| 29 | + * @param boolean $test Test if this is supported or enabled. |
| 30 | + * @return boolean Whether or not the cache is supported |
| 31 | + */ |
| 32 | + public function isSupported($test = false) |
| 33 | + { |
| 34 | + global $cache_redis; |
| 35 | + |
| 36 | + $supported = class_exists('Redis'); |
| 37 | + |
| 38 | + if ($test) |
| 39 | + return $supported; |
| 40 | + return parent::isSupported() && $supported && !empty($cache_redis); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Connects to the cache method. This defines our $key. If this fails, we return false, otherwise we return true. |
| 45 | + * |
| 46 | + * @return boolean Whether or not the cache method was connected to. |
| 47 | + */ |
| 48 | + public function connect() |
| 49 | + { |
| 50 | + global $cache_redis; |
| 51 | + |
| 52 | + switch (substr_count($cache_redis, ':')) |
| 53 | + { |
| 54 | + case 0: |
| 55 | + default: |
| 56 | + return false; |
| 57 | + |
| 58 | + case 1: |
| 59 | + list ($host, $port) = explode(':', $cache_redis); |
| 60 | + $password = null; |
| 61 | + break; |
| 62 | + |
| 63 | + case 2: |
| 64 | + list ($host, $port, $password) = explode(':', $cache_redis, 3); |
| 65 | + break; |
| 66 | + } |
| 67 | + |
| 68 | + $this->redis = new \Redis; |
| 69 | + if (empty($port)) |
| 70 | + { |
| 71 | + $port = 6379; |
| 72 | + } |
| 73 | + |
| 74 | + // Do the initial connection. |
| 75 | + $connected = $this->redis->connect($host, $port); |
| 76 | + if (!$connected) |
| 77 | + { |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + // If we have a password, use it. |
| 82 | + if (!empty($password)) |
| 83 | + { |
| 84 | + $connected = $this->redis->auth($password); |
| 85 | + } |
| 86 | + |
| 87 | + if ($connected) |
| 88 | + { |
| 89 | + $this->redis->setOption(\Redis::OPT_PREFIX, $this->prefix . ':'); |
| 90 | + $this->redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP); |
| 91 | + } |
| 92 | + |
| 93 | + return $connected; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Returns the name for the cache method performed by this API. Likely to be a brand of sorts. |
| 98 | + * |
| 99 | + * @return string The name of the cache backend |
| 100 | + */ |
| 101 | + public function getName() |
| 102 | + { |
| 103 | + return 'Redis'; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Gets data from the cache. |
| 108 | + * |
| 109 | + * @param string $key The key to use, the prefix is applied to the key name. |
| 110 | + * @param string $ttl Overrides the default TTL. |
| 111 | + * @return mixed The result from the cache, if there is no data or it is invalid, we return null. |
| 112 | + */ |
| 113 | + public function getData($key, $ttl = null) |
| 114 | + { |
| 115 | + $value = $this->redis->get($key); |
| 116 | + |
| 117 | + // $value should return either data or false (from failure, key not found or empty array). |
| 118 | + if ($value === false) |
| 119 | + return null; |
| 120 | + return $value; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Saves to data the cache. |
| 125 | + * |
| 126 | + * @param string $key The key to use, the prefix is applied to the key name. |
| 127 | + * @param mixed $value The data we wish to save. |
| 128 | + * @param string $ttl Overrides the default TTL. |
| 129 | + * @return bool Whether or not we could save this to the cache. |
| 130 | + */ |
| 131 | + public function putData($key, $value, $ttl = null) |
| 132 | + { |
| 133 | + return $this->redis->set($key, $value, $ttl ? $ttl : $this->getDefaultTTL()); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * Closes connections to the cache method. |
| 138 | + * |
| 139 | + * @return bool Whether or not we could close connections. |
| 140 | + */ |
| 141 | + public function quit() |
| 142 | + { |
| 143 | + return $this->redis->close(); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Clean out the cache. |
| 148 | + * |
| 149 | + * @param string $type If supported, the type of cache to clear, blank/data or user. |
| 150 | + * @return bool Whether or not we could clean the cache. |
| 151 | + */ |
| 152 | + public function cleanCache($type = '') |
| 153 | + { |
| 154 | + return $this->redis->flushDb(); |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Specify custom settings that the cache API supports. |
| 159 | + * |
| 160 | + * @param array $config_vars Additional config_vars, see ManageSettings.php for usage. |
| 161 | + * @return void No return is needed. |
| 162 | + */ |
| 163 | + public function cacheSettings(array &$config_vars) |
| 164 | + { |
| 165 | + global $context, $txt; |
| 166 | + |
| 167 | + $config_vars[] = $txt['cache_redis_settings']; |
| 168 | + $config_vars[] = array('cache_redis', $txt['cache_redis_server'], 'file', 'text', 0, 'cache_redis', 'postinput' => '<br /><div class="smalltext"><em>' . $txt['cache_redis_server_subtext'] . '</em></div>'); |
| 169 | + } |
| 170 | +} |
0 commit comments