Skip to content

feature/hash-cache-keys #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jan 15, 2018
9 changes: 7 additions & 2 deletions src/CacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ public function __construct(array $eagerLoad, Model $model, Builder $query)
$this->query = $query;
}

public function make(array $columns = ['*'], $idColumn = null) : string
{
public function make(
array $columns = ['*'],
$idColumn = null,
string $keyDifferentiator = ''
) : string {
$key = $this->getModelSlug();
$key .= $this->getIdColumn($idColumn ?: '');
$key .= $this->getQueryColumns($columns);
Expand All @@ -27,6 +30,8 @@ public function make(array $columns = ['*'], $idColumn = null) : string
$key .= $this->getOrderByClauses();
$key .= $this->getOffsetClause();
$key .= $this->getLimitClause();
$key .= $keyDifferentiator;
$key = sha1($key);

return $key;
}
Expand Down
97 changes: 62 additions & 35 deletions src/CachedBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ public function avg($column)
}

return $this->cache($this->makeCacheTags())
->rememberForever($this->makeCacheKey() . "-avg_{$column}", function () use ($column) {
return parent::avg($column);
});
->rememberForever(
$this->makeCacheKey(['*'], null, "-avg_{$column}"),
function () use ($column) {
return parent::avg($column);
}
);
}

public function count($columns = ['*'])
Expand All @@ -29,9 +32,12 @@ public function count($columns = ['*'])
}

return $this->cache($this->makeCacheTags())
->rememberForever($this->makeCacheKey() . "-count", function () use ($columns) {
return parent::count($columns);
});
->rememberForever(
$this->makeCacheKey(['*'], null, "-count"),
function () use ($columns) {
return parent::count($columns);
}
);
}

public function cursor()
Expand All @@ -41,9 +47,12 @@ public function cursor()
}

return $this->cache($this->makeCacheTags())
->rememberForever($this->makeCacheKey() . "-cursor", function () {
return collect(parent::cursor());
});
->rememberForever(
$this->makeCacheKey(['*'], null, "-cursor"),
function () {
return collect(parent::cursor());
}
);
}

public function delete()
Expand All @@ -64,9 +73,12 @@ public function find($id, $columns = ['*'])
}

return $this->cache($this->makeCacheTags())
->rememberForever($this->makeCacheKey($columns, $id), function () use ($id, $columns) {
return parent::find($id, $columns);
});
->rememberForever(
$this->makeCacheKey($columns, $id),
function () use ($id, $columns) {
return parent::find($id, $columns);
}
);
}

public function first($columns = ['*'])
Expand All @@ -76,9 +88,12 @@ public function first($columns = ['*'])
}

return $this->cache($this->makeCacheTags())
->rememberForever($this->makeCacheKey($columns) . '-first', function () use ($columns) {
return parent::first($columns);
});
->rememberForever(
$this->makeCacheKey($columns, null, '-first'),
function () use ($columns) {
return parent::first($columns);
}
);
}

public function get($columns = ['*'])
Expand All @@ -88,9 +103,12 @@ public function get($columns = ['*'])
}

return $this->cache($this->makeCacheTags())
->rememberForever($this->makeCacheKey($columns), function () use ($columns) {
return parent::get($columns);
});
->rememberForever(
$this->makeCacheKey($columns),
function () use ($columns) {
return parent::get($columns);
}
);
}

public function max($column)
Expand All @@ -100,9 +118,12 @@ public function max($column)
}

return $this->cache($this->makeCacheTags())
->rememberForever($this->makeCacheKey() . "-max_{$column}", function () use ($column) {
return parent::max($column);
});
->rememberForever(
$this->makeCacheKey(['*'], null, "-max_{$column}"),
function () use ($column) {
return parent::max($column);
}
);
}

public function min($column)
Expand All @@ -112,9 +133,12 @@ public function min($column)
}

return $this->cache($this->makeCacheTags())
->rememberForever($this->makeCacheKey() . "-min_{$column}", function () use ($column) {
return parent::min($column);
});
->rememberForever(
$this->makeCacheKey(['*'], null, "-min_{$column}"),
function () use ($column) {
return parent::min($column);
}
);
}

public function pluck($column, $key = null)
Expand All @@ -123,11 +147,8 @@ public function pluck($column, $key = null)
return parent::pluck($column, $key);
}

$cacheKey = $this->makeCacheKey([$column]) . "-pluck_{$column}";

if ($key) {
$cacheKey .= "_{$key}";
}
$keyDifferentiator = "-pluck_{$column}" . ($key ? "_{$key}" : "");
$cacheKey = $this->makeCacheKey([$column], null, $keyDifferentiator);

return $this->cache($this->makeCacheTags())
->rememberForever($cacheKey, function () use ($column, $key) {
Expand All @@ -142,9 +163,12 @@ public function sum($column)
}

return $this->cache($this->makeCacheTags())
->rememberForever($this->makeCacheKey() . "-sum_{$column}", function () use ($column) {
return parent::sum($column);
});
->rememberForever(
$this->makeCacheKey(['*'], null, "-sum_{$column}"),
function () use ($column) {
return parent::sum($column);
}
);
}

public function value($column)
Expand All @@ -154,8 +178,11 @@ public function value($column)
}

return $this->cache($this->makeCacheTags())
->rememberForever($this->makeCacheKey() . "-value_{$column}", function () use ($column) {
return parent::value($column);
});
->rememberForever(
$this->makeCacheKey(['*'], null, "-value_{$column}"),
function () use ($column) {
return parent::value($column);
}
);
}
}
2 changes: 1 addition & 1 deletion src/CachedModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public static function all($columns = ['*'])
$class = get_called_class();
$instance = new $class;
$tags = [str_slug(get_called_class())];
$key = 'genealabslaravelmodelcachingtestsfixturesauthor';
$key = $instance->makeCacheKey();

return $instance->cache($tags)
->rememberForever($key, function () use ($columns) {
Expand Down
16 changes: 12 additions & 4 deletions src/Traits/Cachable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use GeneaLabs\LaravelModelCaching\CacheTags;
use GeneaLabs\LaravelModelCaching\CachedModel;
use Illuminate\Cache\TaggableStore;
use Illuminate\Database\Query\Builder;

trait Cachable
{
Expand Down Expand Up @@ -41,10 +42,17 @@ public function flushCache(array $tags = [])
$this->cache($tags)->flush();
}

protected function makeCacheKey(array $columns = ['*'], $idColumn = null) : string
{
return (new CacheKey($this->eagerLoad, $this->model, $this->query))
->make($columns, $idColumn);
protected function makeCacheKey(
array $columns = ['*'],
$idColumn = null,
string $keyDifferentiator = ''
) : string {
$eagerLoad = $this->eagerLoad ?? [];
$model = $this->model ?? $this;
$query = $this->query ?? app(Builder::class);

return (new CacheKey($eagerLoad, $model, $query))
->make($columns, $idColumn, $keyDifferentiator);
}

protected function makeCacheTags() : array
Expand Down
23 changes: 23 additions & 0 deletions tests/Unit/CacheKeyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Unit;

use GeneaLabs\LaravelModelCaching\CachedBuilder;
use GeneaLabs\LaravelModelCaching\Tests\Fixtures\Author;
use GeneaLabs\LaravelModelCaching\Tests\UnitTestCase;
use ReflectionMethod;

class CacheKeyTest extends UnitTestCase
{
public function testKeyIsSHA1()
{
$makeCacheKey = new ReflectionMethod(
CachedBuilder::class,
'makeCacheKey'
);
$makeCacheKey->setAccessible(true);

$builder = (new Author)->startsWithA();
$key = $makeCacheKey->invoke($builder);

$this->assertTrue(strlen($key) === 40 && ctype_xdigit($key));
}
}
Loading