1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
<?php
require_once APP . DS . 'vendors' . DS . 'shells'. DS . 'bedita_base.php';
class RedisShell extends BeditaBaseShell {
protected $skipKeysPrefix = array(
'objects_nickname-'
);
public function ls() {
$this->out('----------------------------------');
$this->out('List keys (grouped by type)');
$this->out('----------------------------------');
$redis = $this->getRedisConn();
$it = NULL;
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
$count = array(
'index' => 0,
'type' => 0,
'path' => 0,
'perms' => 0,
'parent' => 0,
'children' => 0,
'nickname' => 0,
'hash' => 0
);
$sizes = array(
'index' => 0,
'type' => 0,
'path' => 0,
'perms' => 0,
'parent' => 0,
'children' => 0,
'nickname' => 0,
'hash' => 0
);
while ($arrKeys = $redis->scan($it)) {
foreach ($arrKeys as $key) {
$managedKey = $this->managedKey($key);
if (!empty($managedKey)) {
$val = $redis->get($key);
$s = strlen($val);
$count[$managedKey]++;
$sizes[$managedKey]+= $s;
} else {
$this->out($key);
}
}
}
$out = '';
foreach ($count as $k => $v) {
$avg = ($v != 0) ? $sizes[$k] / $v : 0;
$out.= "\n\t$k: $v | size: " . $sizes[$k] . ' | average size: ' . $avg;
}
$this->out("keys: $out");
$this->out('----------------------------------');
}
public function check() {
$this->out('----------------------------------');
$this->out('Check integrity keys');
$this->out('----------------------------------');
$redis = $this->getRedisConn();
$it = NULL;
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
$count = 0;
$check = 0;
$del = 0;
while ($arrKeys = $redis->scan($it)) {
foreach ($arrKeys as $key) {
$count++;
if (!$this->isObjectKey($key)) {
$this->out("key skipped: $key");
} else {
if (!$this->skipKey($key) && !$this->isIndexKey($key)) {
$id = $this->getIdFromKey($key);
if (empty($id)) {
$this->out("id not valid of key $key");
} else {
$indexKey = 'objects_' . $id . '_index';
$val = $redis->get($indexKey);
if (empty($val)) {
$this->out("index not found $indexKey for key $key");
$this->deleteKey($redis, $key);
$del++;
} else {
$keyToSearch = substr($key,8);
if (!stripos($val,$keyToSearch)) {
$this->out("\nchecking key: $key, id: $id, index: $indexKey - key $keyToSearch not found in $indexKey value");
$this->out($val);
$check++;
$this->deleteKey($redis, $key);
$del++;
}
}
}
}
}
if ($count % 10000 === 0) {
$this->out("$count keys analyzed");
}
}
}
$this->out("$count keys analyzed. $check keys found with integrity problems. $del keys deleted.");
$this->out('----------------------------------');
}
public function show() {
$this->out('----------------------------------');
$this->out('Show keys by id');
$this->out('----------------------------------');
if (empty($this->params['id'])) {
$this->out('Param -id missing. Usage:');
$this->out('./cake.sh redis show -id <objectId>');
exit;
}
$redis = $this->getRedisConn();
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
$arrKeys1 = $redis->keys('objects_' . $this->params['id'] . '_*');
$arrKeys2 = $redis->keys('objects_' . $this->params['id'] . '-*');
$arrKeys = array_merge($arrKeys1, $arrKeys2);
foreach ($arrKeys as $key) {
if ($this->isIndexKey($key)) {
$this->out("$key: " . $redis->get($key));
} else {
$this->out($key);
}
}
}
public function countById() {
$this->out('----------------------------------');
$this->out('Count keys by id');
$this->out('----------------------------------');
$verbose = isset($this->params['v']);
if ($verbose) {
$this->out('verbose mode on');
}
$log = isset($this->params['log']);
if ($log) {
$this->out('writing details to log ' . $this->params['log']);
}
$counter = 10;
if (isset($this->params['limit'])) {
$counter = $this->params['limit'];
}
$redis = $this->getRedisConn();
$redis->setOption(Redis::OPT_SCAN, Redis::SCAN_RETRY);
$it = NULL;
$ids = array();
while ($counter > 0 && $arrKeys = $redis->scan($it)) {
foreach ($arrKeys as $key) {
if ($counter > 0) {
if (!$this->isObjectKey($key)) {
$this->out("key skipped: $key");
} else {
if (!$this->skipKey($key) && !$this->isIndexKey($key)) {
$id = $this->getIdFromKey($key);
if (empty($id)) {
$this->out("id not valid of key $key");
} else {
$counter--;
$ids[] = $id;
}
}
}
}
}
}
foreach ($ids as $id) {
$arrKeys1 = $redis->keys('objects_' . $id . '_*');
$arrKeys2 = $redis->keys('objects_' . $id . '-*');
$arrKeys = array_merge($arrKeys1, $arrKeys2);
$out = "$id -> " . count($arrKeys) . " keys";
if ($verbose) {
$out.= " (" . implode(' ',$arrKeys) . ")";
}
if ($log) {
$this->log($out, $this->params['log']);
} else {
$this->out($out);
}
}
}
public function countByPrefix(){
if (empty($this->args[0])) {
$this->out('Argument 0 is required');
return;
}
$response = $this->in('Method KEYS can be very slow. Do NOT use this method in PRODUCTION enviroment. Continue?', array('y', 'n'), 'n');
if ($response === 'n') {
$this->out('Bye');
return;
}
$prefix = $this->args[0];
$redis = $this->getRedisConn();
$keys = $redis->keys($prefix.'*');
$redis->close();
$this->out(count($keys) . ' keys found with prefix "' . $prefix . '"');
}
public function clearByPrefix(){
if (empty($this->args[0])) {
$this->out('Argument 0 is required');
return;
}
$response = $this->in('Method KEYS can be very slow. Do NOT use this method in PRODUCTION enviroment. Continue?', array('y', 'n'), 'n');
if ($response === 'n') {
$this->out('Bye');
return;
}
$prefix = $this->args[0];
$redis = $this->getRedisConn();
$keys = $redis->keys($prefix.'*');
if (!empty($keys)) {
$response = $this->in(count($keys) . ' keys will be deleted. Continue?', array('y', 'n'), 'n');
if ($response == 'n') {
$redis->close();
$this->out('Bye');
return;
}
foreach ($keys as $key) {
$redis->delete($key);
}
}
$redis->close();
$this->out(count($keys) . ' keys deleted');
}
public function help() {
$this->hr();
$this->out('redis script shell usage:');
$this->out('');
$this->out('./cake.sh redis check // consistency check for entire objects db');
$this->out('./cake.sh redis clearByPrefix <prefix> // remove cache keys starting with <prefix> string');
$this->out('./cake.sh redis countByPrefix <prefix> // count cache keys starting with <prefix> string');
$this->out('./cake.sh redis countById [-v (verbose mode|default off) -limit <n> (default 10) -log <logFile>] // count keys occurences by ids');
$this->out('./cake.sh redis ls // list keys group by type');
$this->out('./cake.sh redis show -id <objectId> // show keys for specified id');
$this->out('');
}
private function deleteKey($redis, $key) {
$redis->delete($key);
$this->out("Key $key deleted");
}
private function getRedisConn() {
$settings = Cache::settings('objects');
try {
$redis = new Redis();
$persistentId = $settings['port'] . $settings['timeout'] . $settings['database'];
$res = $redis->pconnect($settings['server'], $settings['port'], $settings['timeout'], $persistentId);
} catch (Exception $e) {
$this->out('Error connecting to redis: ' . $e->getMessage());
return -1;
}
if (!$res) {
$this->out('Error connecting to redis');
return -1;
}
if ($settings['password'] && ! $redis->auth($settings['password'])) {
return -1;
}
$redis->select($settings['database']);
return $redis;
}
private function startsWith($haystack, $needle) {
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
private function endsWith($haystack, $needle) {
$length = strlen($needle);
if ($length == 0) {
return true;
}
return (substr($haystack, -$length) === $needle);
}
private function skipKey($key) {
foreach ($this->skipKeysPrefix as $prefix) {
if ($this->startsWith($key, $prefix)) {
return true;
}
}
return false;
}
private function isObjectKey($key) {
return $this->startsWith($key, 'objects_');
}
private function isIndexKey($key) {
return $this->endsWith($key, '_index');
}
private function isTypeKey($key) {
return $this->endsWith($key, '-type');
}
private function isPermsKey($key) {
return $this->endsWith($key, '-perms');
}
private function isChildrenKey($key) {
return (strpos($key, '-children-') !== false);
}
private function isNicknameKey($key) {
return (strpos($key, '_nickname-') !== false);
}
private function isPathKey($key) {
return (strpos($key, '_path-') !== false);
}
private function isParentKey($key) {
return (strpos($key, '-parent-') !== false);
}
private function isHash($key) {
if ($this->isObjectKey($key)) {
$idx = strrpos($key, '-') + 1;
$hash = substr($key,$idx);
return (strlen($hash) === 32);
}
return false;
}
private function getIdFromKey($key) {
$start = 8;
if (stripos($key,'objects_path') === 0) {
$start = 13;
}
$tmp = substr($key,$start);
$len = 0;
$len1 = (stripos($tmp,'_')) ? stripos($tmp,'_') : null;
$len2 = (stripos($tmp,'-')) ? stripos($tmp,'-') : null;
if ($len1 != null && $len2 != null) {
$len = min(array($len1,$len2));
} else if ($len1 !=null) {
$len = $len1;
} else {
$len = $len2;
}
return substr($key,$start,$len);
}
private function managedKey($key) {
if ($this->isIndexKey($key)) {
return 'index';
} else if ($this->isTypeKey($key)) {
return 'type';
} else if ($this->isPathKey($key)) {
return 'path';
} else if ($this->isPermsKey($key)) {
return 'perms';
} else if ($this->isParentKey($key)) {
return 'parent';
} else if ($this->isChildrenKey($key)) {
return 'children';
} else if ($this->isNicknameKey($key)) {
return 'nickname';
} else if ($this->isHash($key)) {
return 'hash';
}
return null;
}
}