123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- namespace Flowdat\Stats\App\Service\Redis;
- use Predis\Client as Redis;
- class RedisService implements CollectorInterface
- {
- /**
- * @var Redis
- */
- private $redis;
- /**
- * RedisService constructor.
- * @param Redis $redis
- */
- public function __construct()
- {
- $this->redis = new Redis(array("tcp://redis:6379"));
- }
- /**
- * @param string $key
- * @return array
- */
- public function get($key, $output = false)
- {
- $start = microtime(true);
- $_data = $this->redis->get($key);
- $end = microtime(true);
- $t = $end - $start;
- if ($output) {
- print_r("REDIS GET key '{$key}': {$t} segundos" . PHP_EOL);
- }
- $start = microtime(true);
- $data_cached = json_decode($_data, true);
- $end = microtime(true);
- $t = $end - $start;
- if ($output) {
- print_r("DECODE key '{$key}': {$t} segundos" . PHP_EOL);
- }
- $this->redis->disconnect();
- $this->redis->quit();
- if (is_null($data_cached)) $data_cached = array();
- return $data_cached;
- }
- /**
- * @param string $key
- * @param array $data
- */
- public function set($key, $data, $output = false)
- {
- $start = microtime(true);
- $_save = json_encode($data);
- $end = microtime(true);
- $t = $end - $start;
- if ($output) {
- print_r("ENCODE key '{$key}': {$t} segundos" . PHP_EOL);
- }
- $start = microtime(true);
- $this->redis->setex($key, 7200, $_save);
- $end = microtime(true);
- $t = $end - $start;
- if ($output) {
- print_r("SETEX key '{$key}': {$t} segundos" . PHP_EOL);
- }
- }
- /**
- * @param string $key
- * @return string
- */
- public function getString($key, $output = false)
- {
- $start = microtime(true);
- $_data = $this->redis->get($key);
- $end = microtime(true);
- $t = $end - $start;
- if ($output) {
- print_r("REDIS GET key '{$key}': {$t} segundos" . PHP_EOL);
- }
- $this->redis->disconnect();
- $this->redis->quit();
- return $_data;
- }
- /**
- * @param string $key
- * @param string $data
- */
- public function setString($key, $data, $output = false)
- {
- $_save = $data;
- $start = microtime(true);
- $this->redis->setex($key, 7200, $_save);
- $end = microtime(true);
- $t = $end - $start;
- if ($output) {
- print_r("SETEX key '{$key}': {$t} segundos" . PHP_EOL);
- }
- }
- /**
- * @param string|array $key
- */
- public function del($key)
- {
- if(is_array($key)) {
- return $this->redis->del($key);
- } else {
- return $this->redis->del(array($key));
- }
- }
- /**
- * @param string $key
- * @param string $field
- * @param array $data
- */
- public function hset($key, $field, $data, $output = false)
- {
- $start = microtime(true);
- $_save = json_encode($data);
- $end = microtime(true);
- $t = $end - $start;
- if ($output) {
- print_r("ENCODE key '{$key}.{$field}': {$t} segundos" . PHP_EOL);
- }
- $start = microtime(true);
- $this->redis->hset($key, $field, $_save);
- $end = microtime(true);
- $t = $end - $start;
- if ($output) {
- print_r("SETEX key '{$key}.{$field}': {$t} segundos" . PHP_EOL);
- }
- }
- /**
- * @param string $key
- * @param string $field
- */
- public function hget($key, $field, $output = false)
- {
- $start = microtime(true);
- $_data = $this->redis->hget($key, $field);
- $end = microtime(true);
- $t = $end - $start;
- if ($output) {
- print_r("HGET '{$key}.{$field}': {$t} segundos" . PHP_EOL);
- }
- if($_data)
- return json_decode($_data,true);
- return array();
- }
- /**
- * @param string $key
- */
- public function hgetall($key, $output = false)
- {
- $start = microtime(true);
- $_data = $this->redis->hgetall($key);
- $end = microtime(true);
- $t = $end - $start;
- if ($output) {
- print_r("HGET '{$key}': {$t} segundos" . PHP_EOL);
- }
- if($_data)
- return $_data;
- return array();
- }
- }
|