RedisService.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. namespace Flowdat\Stats\App\Service\Redis;
  3. use Predis\Client as Redis;
  4. class RedisService implements CollectorInterface
  5. {
  6. /**
  7. * @var Redis
  8. */
  9. private $redis;
  10. /**
  11. * RedisService constructor.
  12. * @param Redis $redis
  13. */
  14. public function __construct()
  15. {
  16. $this->redis = new Redis(array("tcp://redis:6379"));
  17. }
  18. /**
  19. * @param string $key
  20. * @return array
  21. */
  22. public function get($key, $output = false)
  23. {
  24. $start = microtime(true);
  25. $_data = $this->redis->get($key);
  26. $end = microtime(true);
  27. $t = $end - $start;
  28. if ($output) {
  29. print_r("REDIS GET key '{$key}': {$t} segundos" . PHP_EOL);
  30. }
  31. $start = microtime(true);
  32. $data_cached = json_decode($_data, true);
  33. $end = microtime(true);
  34. $t = $end - $start;
  35. if ($output) {
  36. print_r("DECODE key '{$key}': {$t} segundos" . PHP_EOL);
  37. }
  38. $this->redis->disconnect();
  39. $this->redis->quit();
  40. if (is_null($data_cached)) $data_cached = array();
  41. return $data_cached;
  42. }
  43. /**
  44. * @param string $key
  45. * @param array $data
  46. */
  47. public function set($key, $data, $output = false)
  48. {
  49. $start = microtime(true);
  50. $_save = json_encode($data);
  51. $end = microtime(true);
  52. $t = $end - $start;
  53. if ($output) {
  54. print_r("ENCODE key '{$key}': {$t} segundos" . PHP_EOL);
  55. }
  56. $start = microtime(true);
  57. $this->redis->setex($key, 7200, $_save);
  58. $end = microtime(true);
  59. $t = $end - $start;
  60. if ($output) {
  61. print_r("SETEX key '{$key}': {$t} segundos" . PHP_EOL);
  62. }
  63. }
  64. /**
  65. * @param string $key
  66. * @return string
  67. */
  68. public function getString($key, $output = false)
  69. {
  70. $start = microtime(true);
  71. $_data = $this->redis->get($key);
  72. $end = microtime(true);
  73. $t = $end - $start;
  74. if ($output) {
  75. print_r("REDIS GET key '{$key}': {$t} segundos" . PHP_EOL);
  76. }
  77. $this->redis->disconnect();
  78. $this->redis->quit();
  79. return $_data;
  80. }
  81. /**
  82. * @param string $key
  83. * @param string $data
  84. */
  85. public function setString($key, $data, $output = false)
  86. {
  87. $_save = $data;
  88. $start = microtime(true);
  89. $this->redis->setex($key, 7200, $_save);
  90. $end = microtime(true);
  91. $t = $end - $start;
  92. if ($output) {
  93. print_r("SETEX key '{$key}': {$t} segundos" . PHP_EOL);
  94. }
  95. }
  96. /**
  97. * @param string|array $key
  98. */
  99. public function del($key)
  100. {
  101. if(is_array($key)) {
  102. return $this->redis->del($key);
  103. } else {
  104. return $this->redis->del(array($key));
  105. }
  106. }
  107. /**
  108. * @param string $key
  109. * @param string $field
  110. * @param array $data
  111. */
  112. public function hset($key, $field, $data, $output = false)
  113. {
  114. $start = microtime(true);
  115. $_save = json_encode($data);
  116. $end = microtime(true);
  117. $t = $end - $start;
  118. if ($output) {
  119. print_r("ENCODE key '{$key}.{$field}': {$t} segundos" . PHP_EOL);
  120. }
  121. $start = microtime(true);
  122. $this->redis->hset($key, $field, $_save);
  123. $end = microtime(true);
  124. $t = $end - $start;
  125. if ($output) {
  126. print_r("SETEX key '{$key}.{$field}': {$t} segundos" . PHP_EOL);
  127. }
  128. }
  129. /**
  130. * @param string $key
  131. * @param string $field
  132. */
  133. public function hget($key, $field, $output = false)
  134. {
  135. $start = microtime(true);
  136. $_data = $this->redis->hget($key, $field);
  137. $end = microtime(true);
  138. $t = $end - $start;
  139. if ($output) {
  140. print_r("HGET '{$key}.{$field}': {$t} segundos" . PHP_EOL);
  141. }
  142. if($_data)
  143. return json_decode($_data,true);
  144. return array();
  145. }
  146. /**
  147. * @param string $key
  148. */
  149. public function hgetall($key, $output = false)
  150. {
  151. $start = microtime(true);
  152. $_data = $this->redis->hgetall($key);
  153. $end = microtime(true);
  154. $t = $end - $start;
  155. if ($output) {
  156. print_r("HGET '{$key}': {$t} segundos" . PHP_EOL);
  157. }
  158. if($_data)
  159. return $_data;
  160. return array();
  161. }
  162. }