Skip to content

Commit

Permalink
Merge pull request #5 from sitkoru/cacheops
Browse files Browse the repository at this point in the history
Cacheops
  • Loading branch information
SonicGD authored Apr 5, 2017
2 parents 707b9a7 + 8e2969c commit b7fb574
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion ActiveQueryCacheHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected static function getModelsToDelete($className, $condition, $params)
$params
);
try {
$results = $query->createCommand()->queryAll();
$results = $query->createCommand($className::getDb())->queryAll();
} catch (Exception $ex) {
$results = [];
}
Expand Down
11 changes: 9 additions & 2 deletions CacheActiveQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class CacheActiveQuery extends ActiveQuery
private $dropConditions = [];
private $disableCache = false;

private function isCacheEnabled()
{
if ($this->disableCache || (defined('DISABLE_AR_CACHE') && DISABLE_AR_CACHE)) {
return false;
}
return true;
}

/**
* @inheritdoc
Expand All @@ -26,7 +33,7 @@ public function all($db = null)
{
ActiveQueryCacheHelper::initialize();

if (!$this->disableCache) {
if ($this->isCacheEnabled()) {
$command = $this->createCommand($db);
$key = $this->generateCacheKey($command->rawSql, 'all');

Expand Down Expand Up @@ -70,7 +77,7 @@ public function all($db = null)
public function one($db = null)
{
ActiveQueryCacheHelper::initialize();
if (!$this->disableCache) {
if ($this->isCacheEnabled()) {
$command = $this->createCommand($db);
$key = $this->generateCacheKey($command->rawSql, 'one');
/**
Expand Down
16 changes: 12 additions & 4 deletions CacheHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,21 @@ public static function scriptExists($sha)

public static function evalSHA($sha, $args, $numKeys)
{
return static::getRedis()->executeCommand('EVALSHA', [$sha, $args, $numKeys]);
return static::getRedis()->evalsha($sha, $numKeys, ...$args);
}

public static function get($key)
{
$res = static::getRedis()->executeCommand('get', [$key]);
$res = static::getRedis()->get($key);

return $res !== false ? unserialize(zlib_decode($res)) : $res;
if ($res !== false) {
try {
return unserialize(zlib_decode($res));
} catch (\Throwable $ex) {

}
return null;
}
return $res;
}
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@
"sitkoru\\cache\\ar\\": ""
}
}
}
}

0 comments on commit b7fb574

Please sign in to comment.