|
@@ -0,0 +1,90 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace RedisBundle\Services;
|
|
|
+
|
|
|
+use Predis\Client as Redis;
|
|
|
+
|
|
|
+class RedisService extends Redis implements CollectorInterface
|
|
|
+{
|
|
|
+ public function setOptions($options)
|
|
|
+ {
|
|
|
+ $this->options = $this->createOptions($options ?: array());
|
|
|
+ $this->profile = $this->options->profile;
|
|
|
+ }
|
|
|
+
|
|
|
+ public function setConnection($parameters)
|
|
|
+ {
|
|
|
+ $this->connection = $this->createConnection($parameters ?: array());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param string $key
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function get($key, $output = false)
|
|
|
+ {
|
|
|
+ $start = microtime(true);
|
|
|
+ $_data = parent::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);
|
|
|
+ }
|
|
|
+
|
|
|
+ parent::disconnect();
|
|
|
+ parent::quit();
|
|
|
+
|
|
|
+ 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);
|
|
|
+ parent::setex($key, 3600, $_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 = parent::get($key);
|
|
|
+ $end = microtime(true);
|
|
|
+ $t = $end - $start;
|
|
|
+ if ($output) {
|
|
|
+ print_r("REDIS GET key '{$key}': {$t} segundos" . PHP_EOL);
|
|
|
+ }
|
|
|
+
|
|
|
+ parent::disconnect();
|
|
|
+ parent::quit();
|
|
|
+
|
|
|
+ return $_data;
|
|
|
+ }
|
|
|
+}
|