Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Mar 31, 2018
2 parents 1f6e4ef + 8273f57 commit e8f6012
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 61 deletions.
64 changes: 7 additions & 57 deletions src/extension/CacheTrait.php
Original file line number Diff line number Diff line change
@@ -1,74 +1,24 @@
<?php
/**
* Created by PhpStorm.
* User: arthurkushman
* Date: 04.03.18
* Time: 11:07
*/

namespace rjapi\extension;


use Predis\Client;
use rjapi\helpers\ConfigHelper;
use rjapi\types\ConfigInterface;
use Illuminate\Support\Facades\Redis;
use rjapi\types\PhpInterface;

/**
* Trait CacheTrait
* @package rjapi\extension
*/
trait CacheTrait
{
/** @var Client $cache */
private $cache;
private $host;
private $port;

public function cacheConn() : void
{
$this->setHostAndPort();
$this->cache = new Client([
'scheme' => 'tcp',
'host' => $this->host,
'port' => $this->port,
]);
// select db
$db = ConfigHelper::getNestedParam(ConfigInterface::CACHE, ConfigInterface::DATABASE);
if ($db !== null) {
$this->cache->select($db);
}
// enter password
$password = env('REDIS_PASSWORD');
if ($password !== null) {
$this->cache->auth($password);
}
}

private function setHostAndPort() : void
{
$cacheEntity = ConfigHelper::getNestedParam(ConfigInterface::CACHE, $this->entity);
$this->host = empty($cacheEntity[ConfigInterface::HOST]) ? null : $cacheEntity[ConfigInterface::HOST];
if ($this->host === null) {
$this->host = env('REDIS_HOST');
if ($this->host === null) {
$this->host = ConfigInterface::DEFAULT_REDIS_HOST;
}
}

$this->port = empty($cacheEntity[ConfigInterface::PORT]) ? null : $cacheEntity[ConfigInterface::PORT];
if ($this->port === null) {
$this->port = env('REDIS_PORT');
if ($this->port === null) {
$this->port = ConfigInterface::DEFAULT_REDIS_PORT;
}
}
}

/**
* @param string $key
* @param \League\Fractal\Resource\Collection | \League\Fractal\Resource\Item $val
* @return mixed
*/
private function set(string $key, $val)
{
return $this->cache->set($key, $this->ser($val));
return Redis::set($key, $this->ser($val));
}

/**
Expand All @@ -77,7 +27,7 @@ private function set(string $key, $val)
*/
private function get(string $key)
{
$data = $this->cache->get($key);
$data = Redis::get($key);
if ($data === null) {
return null;
}
Expand Down
4 changes: 0 additions & 4 deletions src/extension/OptionsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,6 @@ private function setConfigOptions(string $calledMethod) : void
$this->configOptions->setIsCached($entityCache[ConfigInterface::ENABLED]);
}
}

if ($this->configOptions->isCached()) {
$this->cacheConn();
}
}

/**
Expand Down

0 comments on commit e8f6012

Please sign in to comment.