Guest User

Untitled

a guest
Oct 21st, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.10 KB | None | 0 0
  1. <?php
  2.  
  3. function sendMemcacheCommand($command){
  4.  
  5. $server = "localhost";
  6. $port = 11211;
  7.  
  8. $s = @fsockopen($server,$port);
  9. if (!$s){
  10. die("Cant connect to:".$server.':'.$port);
  11. }
  12.  
  13. fwrite($s, $command."\r\n");
  14.  
  15. $buf='';
  16. while ((!feof($s))) {
  17. $buf .= fgets($s, 256);
  18. if (strpos($buf,"END\r\n")!==false) {
  19. break;
  20. }
  21. if (strpos($buf,"DELETED\r\n")!==false || strpos($buf,"NOT_FOUND\r\n")!==false) {
  22. break;
  23. }
  24. if (strpos($buf,"OK\r\n")!==false) {
  25. break;
  26. }
  27. }
  28. fclose($s);
  29.  
  30. return ($buf);
  31. }
  32.  
  33. function listKeys() {
  34.  
  35. $res = array();
  36.  
  37. $string = sendMemcacheCommand("stats items");
  38.  
  39. $lines = explode("\r\n", $string);
  40.  
  41. $slabs = array();
  42.  
  43. foreach($lines as $line) {
  44. if (preg_match("/STAT items:([\d]):/", $line, $matches) == 1) {
  45.  
  46. if (isset($matches[1])) {
  47. if (!in_array($matches[1], $slabs)) {
  48. $slabs[] = $matches[1];
  49.  
  50. $string = sendMemcacheCommand("stats cachedump " . $matches[1] . " 100");
  51.  
  52. preg_match_all("/ITEM (.*?) /", $string, $matches);
  53. $res = array_merge($res, $matches[1]);
  54. }
  55. }
  56. }
  57. }
  58.  
  59. return $res;
  60. }
  61.  
  62. var_dump(listKeys());
  63.  
  64. ?>
Add Comment
Please, Sign In to add comment