RedisService.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace RedisBundle\Services;
  3. use Predis\Client as Redis;
  4. class RedisService extends Redis implements CollectorInterface
  5. {
  6. public function setOptions($options)
  7. {
  8. $this->options = $this->createOptions($options ?: array());
  9. $this->profile = $this->options->profile;
  10. }
  11. public function setConnection($parameters)
  12. {
  13. $this->connection = $this->createConnection($parameters ?: array());
  14. }
  15. /**
  16. * @param string $key
  17. * @return array
  18. */
  19. public function get($key, $output = false)
  20. {
  21. $start = microtime(true);
  22. $_data = parent::get($key);
  23. $end = microtime(true);
  24. $t = $end - $start;
  25. if ($output) {
  26. print_r("REDIS GET key '{$key}': {$t} segundos" . PHP_EOL);
  27. }
  28. $start = microtime(true);
  29. $data_cached = json_decode($_data, true);
  30. $end = microtime(true);
  31. $t = $end - $start;
  32. if ($output) {
  33. print_r("DECODE key '{$key}': {$t} segundos" . PHP_EOL);
  34. }
  35. parent::disconnect();
  36. parent::quit();
  37. return $data_cached;
  38. }
  39. /**
  40. * @param string $key
  41. * @param array $data
  42. */
  43. public function set($key, $data, $output = false)
  44. {
  45. $start = microtime(true);
  46. $_save = json_encode($data);
  47. $end = microtime(true);
  48. $t = $end - $start;
  49. if ($output) {
  50. print_r("ENCODE key '{$key}': {$t} segundos" . PHP_EOL);
  51. }
  52. $start = microtime(true);
  53. parent::setex($key, 3600, $_save);
  54. $end = microtime(true);
  55. $t = $end - $start;
  56. if ($output) {
  57. print_r("SETEX key '{$key}': {$t} segundos" . PHP_EOL);
  58. }
  59. }
  60. /**
  61. * @param string $key
  62. * @return string
  63. */
  64. public function getString($key, $output = false)
  65. {
  66. $start = microtime(true);
  67. $_data = parent::get($key);
  68. $end = microtime(true);
  69. $t = $end - $start;
  70. if ($output) {
  71. print_r("REDIS GET key '{$key}': {$t} segundos" . PHP_EOL);
  72. }
  73. parent::disconnect();
  74. parent::quit();
  75. return $_data;
  76. }
  77. }