|
| 1 | +<?php |
| 2 | + |
| 3 | +final class PhabricatorKeyValueDatabaseCache |
| 4 | + extends PhutilKeyValueCache { |
| 5 | + |
| 6 | + const CACHE_FORMAT_RAW = 'raw'; |
| 7 | + const CACHE_FORMAT_DEFLATE = 'deflate'; |
| 8 | + |
| 9 | + public function setKeys(array $keys, $ttl = null) { |
| 10 | + $call_id = null; |
| 11 | + if ($this->getProfiler()) { |
| 12 | + $call_id = $this->getProfiler()->beginServiceCall( |
| 13 | + array( |
| 14 | + 'type' => 'kvcache-set', |
| 15 | + 'name' => 'phabricator-db', |
| 16 | + 'keys' => array_keys($keys), |
| 17 | + 'ttl' => $ttl, |
| 18 | + )); |
| 19 | + } |
| 20 | + |
| 21 | + if ($keys) { |
| 22 | + $map = $this->digestKeys(array_keys($keys)); |
| 23 | + $conn_w = $this->establishConnection('w'); |
| 24 | + |
| 25 | + $sql = array(); |
| 26 | + foreach ($map as $key => $hash) { |
| 27 | + $value = $keys[$key]; |
| 28 | + |
| 29 | + list($format, $storage_value) = $this->willWriteValue($key, $value); |
| 30 | + |
| 31 | + $sql[] = qsprintf( |
| 32 | + $conn_w, |
| 33 | + '(%s, %s, %s, %s, %d, %nd)', |
| 34 | + $hash, |
| 35 | + $key, |
| 36 | + $format, |
| 37 | + $storage_value, |
| 38 | + time(), |
| 39 | + $ttl ? (time() + $ttl) : null); |
| 40 | + } |
| 41 | + |
| 42 | + $guard = AphrontWriteGuard::beginScopedUnguardedWrites(); |
| 43 | + foreach (PhabricatorLiskDAO::chunkSQL($sql) as $chunk) { |
| 44 | + queryfx( |
| 45 | + $conn_w, |
| 46 | + 'INSERT INTO %T |
| 47 | + (cacheKeyHash, cacheKey, cacheFormat, cacheData, |
| 48 | + cacheCreated, cacheExpires) VALUES %Q |
| 49 | + ON DUPLICATE KEY UPDATE |
| 50 | + cacheKey = VALUES(cacheKey), |
| 51 | + cacheFormat = VALUES(cacheFormat), |
| 52 | + cacheData = VALUES(cacheData), |
| 53 | + cacheCreated = VALUES(cacheCreated), |
| 54 | + cacheExpires = VALUES(cacheExpires)', |
| 55 | + $this->getTableName(), |
| 56 | + $chunk); |
| 57 | + } |
| 58 | + unset($guard); |
| 59 | + } |
| 60 | + |
| 61 | + if ($call_id) { |
| 62 | + $this->getProfiler()->endServiceCall($call_id, array()); |
| 63 | + } |
| 64 | + |
| 65 | + return $this; |
| 66 | + } |
| 67 | + |
| 68 | + public function getKeys(array $keys) { |
| 69 | + $call_id = null; |
| 70 | + if ($this->getProfiler()) { |
| 71 | + $call_id = $this->getProfiler()->beginServiceCall( |
| 72 | + array( |
| 73 | + 'type' => 'kvcache-get', |
| 74 | + 'name' => 'phabricator-db', |
| 75 | + 'keys' => $keys, |
| 76 | + )); |
| 77 | + } |
| 78 | + |
| 79 | + $results = array(); |
| 80 | + if ($keys) { |
| 81 | + $map = $this->digestKeys($keys); |
| 82 | + |
| 83 | + $rows = queryfx_all( |
| 84 | + $this->establishConnection('r'), |
| 85 | + 'SELECT * FROM %T WHERE cacheKeyHash IN (%Ls)', |
| 86 | + $this->getTableName(), |
| 87 | + $map); |
| 88 | + $rows = ipull($rows, null, 'cacheKey'); |
| 89 | + |
| 90 | + foreach ($keys as $key) { |
| 91 | + if (empty($rows[$key])) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + $row = $rows[$key]; |
| 96 | + |
| 97 | + if ($row['cacheExpires'] && ($row['cacheExpires'] < time())) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + try { |
| 102 | + $results[$key] = $this->didReadValue( |
| 103 | + $row['cacheFormat'], |
| 104 | + $row['cacheData']); |
| 105 | + } catch (Exception $ex) { |
| 106 | + // Treat this as a cache miss. |
| 107 | + phlog($ex); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + if ($call_id) { |
| 113 | + $this->getProfiler()->endServiceCall( |
| 114 | + $call_id, |
| 115 | + array( |
| 116 | + 'hits' => array_keys($results), |
| 117 | + )); |
| 118 | + } |
| 119 | + |
| 120 | + return $results; |
| 121 | + } |
| 122 | + |
| 123 | + public function deleteKeys(array $keys) { |
| 124 | + $call_id = null; |
| 125 | + if ($this->getProfiler()) { |
| 126 | + $call_id = $this->getProfiler()->beginServiceCall( |
| 127 | + array( |
| 128 | + 'type' => 'kvcache-del', |
| 129 | + 'name' => 'phabricator-db', |
| 130 | + 'keys' => $keys, |
| 131 | + )); |
| 132 | + } |
| 133 | + |
| 134 | + if ($keys) { |
| 135 | + $map = $this->digestKeys($keys); |
| 136 | + queryfx( |
| 137 | + $this->establishConnection('w'), |
| 138 | + 'DELETE FROM %T WHERE cacheKeyHash IN (%Ls)', |
| 139 | + $this->getTableName(), |
| 140 | + $keys); |
| 141 | + } |
| 142 | + |
| 143 | + if ($call_id) { |
| 144 | + $this->getProfiler()->endServiceCall($call_id, array()); |
| 145 | + } |
| 146 | + |
| 147 | + return $this; |
| 148 | + } |
| 149 | + |
| 150 | + public function destroyCache() { |
| 151 | + queryfx( |
| 152 | + $this->establishConnection('w'), |
| 153 | + 'DELETE FROM %T', |
| 154 | + $this->getTableName()); |
| 155 | + return $this; |
| 156 | + } |
| 157 | + |
| 158 | + |
| 159 | +/* -( Raw Cache Access )--------------------------------------------------- */ |
| 160 | + |
| 161 | + |
| 162 | + public function establishConnection($mode) { |
| 163 | + // TODO: This is the only concrete table we have on the database right |
| 164 | + // now. |
| 165 | + return id(new PhabricatorMarkupCache())->establishConnection($mode); |
| 166 | + } |
| 167 | + |
| 168 | + public function getTableName() { |
| 169 | + return 'cache_general'; |
| 170 | + } |
| 171 | + |
| 172 | + |
| 173 | +/* -( Implementation )----------------------------------------------------- */ |
| 174 | + |
| 175 | + |
| 176 | + private function digestKeys(array $keys) { |
| 177 | + $map = array(); |
| 178 | + foreach ($keys as $key) { |
| 179 | + $map[$key] = PhabricatorHash::digestForIndex($key); |
| 180 | + } |
| 181 | + return $map; |
| 182 | + } |
| 183 | + |
| 184 | + private function willWriteValue($key, $value) { |
| 185 | + if (!is_string($value)) { |
| 186 | + throw new Exception("Only strings may be written to the DB cache!"); |
| 187 | + } |
| 188 | + |
| 189 | + static $can_deflate; |
| 190 | + if ($can_deflate === null) { |
| 191 | + $can_deflate = function_exists('gzdeflate') && |
| 192 | + PhabricatorEnv::getEnvConfig('cache.enable-deflate'); |
| 193 | + } |
| 194 | + |
| 195 | + // If the value is larger than 1KB, we have gzdeflate(), we successfully |
| 196 | + // can deflate it, and it benefits from deflation, store it deflated. |
| 197 | + if ($can_deflate) { |
| 198 | + $len = strlen($value); |
| 199 | + if ($len > 1024) { |
| 200 | + $deflated = gzdeflate($value); |
| 201 | + if ($deflated !== false) { |
| 202 | + $deflated_len = strlen($deflated); |
| 203 | + if ($deflated_len < ($len / 2)) { |
| 204 | + return array(self::CACHE_FORMAT_DEFLATE, $deflated); |
| 205 | + } |
| 206 | + } |
| 207 | + } |
| 208 | + } |
| 209 | + |
| 210 | + return array(self::CACHE_FORMAT_RAW, $value); |
| 211 | + } |
| 212 | + |
| 213 | + private function didReadValue($format, $value) { |
| 214 | + switch ($format) { |
| 215 | + case self::CACHE_FORMAT_RAW: |
| 216 | + return $value; |
| 217 | + case self::CACHE_FORMAT_DEFLATE: |
| 218 | + if (!function_exists('gzinflate')) { |
| 219 | + throw new Exception("No gzinflate() to read deflated cache."); |
| 220 | + } |
| 221 | + $value = gzinflate($value); |
| 222 | + if ($value === false) { |
| 223 | + throw new Exception("Failed to deflate cache."); |
| 224 | + } |
| 225 | + return $value; |
| 226 | + default: |
| 227 | + throw new Exception("Unknown cache format."); |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + |
| 232 | +} |
0 commit comments