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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor
composer.lock
.*.sw?
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is probably best handled in your global git configuration. It's been considered "best practice" to leave out environment-specific ignores from the project's git-ignore.

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 $suffix = ''
) : 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 .= $suffix;
$key = sha1($key);

return $key;
}
Expand Down
92 changes: 61 additions & 31 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,7 +147,7 @@ public function pluck($column, $key = null)
return parent::pluck($column, $key);
}

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

if ($key) {
$cacheKey .= "_{$key}";
Expand All @@ -142,9 +166,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 +181,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 = sha1('genealabslaravelmodelcachingtestsfixturesauthor');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call makeCacheKey() here, this was bad code on my part.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work in a static function.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused as to why the function is marked static in the first place. It is called like an object method.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this?

$key = $instance->makeCacheKey();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for instance in tests/Unit/CachedBuilderTest.php line 44.


return $instance->cache($tags)
->rememberForever($key, function () use ($columns) {
Expand Down
9 changes: 6 additions & 3 deletions src/Traits/Cachable.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ public function flushCache(array $tags = [])
$this->cache($tags)->flush();
}

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

protected function makeCacheTags() : array
Expand Down
Loading