Skip to content

Commit

Permalink
Merge branch 'release/0.2.29'
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebronner committed Feb 18, 2018
2 parents 403dc41 + ad59342 commit 9782a4c
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 143 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [0.2.29] - 18 Feb 2018
### Added
- hash collision detection and prevetion.

## [0.2.28] - 18 Feb 2018
### Changed
- disabling of cache from using session to use cache-key instead.
Expand Down
1 change: 0 additions & 1 deletion src/CacheKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public function make(
$key .= $this->getOffsetClause();
$key .= $this->getLimitClause();
$key .= $keyDifferentiator;
$key = sha1($key);

return $key;
}
Expand Down
168 changes: 97 additions & 71 deletions src/CachedBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@ public function avg($column)
return parent::avg($column);
}

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

return $this->cachedValue($arguments, $cacheKey, $method);
}

public function count($columns = ['*'])
Expand All @@ -31,13 +29,11 @@ public function count($columns = ['*'])
return parent::count($columns);
}

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

return $this->cachedValue($arguments, $cacheKey, $method);
}

public function cursor()
Expand All @@ -46,13 +42,11 @@ public function cursor()
return collect(parent::cursor());
}

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

return $this->cachedValue($arguments, $cacheKey, $method);
}

public function delete()
Expand All @@ -72,13 +66,11 @@ public function find($id, $columns = ['*'])
return parent::find($id, $columns);
}

return $this->cache($this->makeCacheTags())
->rememberForever(
$this->makeCacheKey($columns, $id),
function () use ($id, $columns) {
return parent::find($id, $columns);
}
);
$arguments = func_get_args();
$cacheKey = $this->makeCacheKey($columns);
$method = 'find';

return $this->cachedValue($arguments, $cacheKey, $method);
}

public function first($columns = ['*'])
Expand All @@ -87,13 +79,11 @@ public function first($columns = ['*'])
return parent::first($columns);
}

return $this->cache($this->makeCacheTags())
->rememberForever(
$this->makeCacheKey($columns, null, '-first'),
function () use ($columns) {
return parent::first($columns);
}
);
$arguments = func_get_args();
$cacheKey = $this->makeCacheKey($columns);
$method = 'first';

return $this->cachedValue($arguments, $cacheKey, $method);
}

public function get($columns = ['*'])
Expand All @@ -102,13 +92,11 @@ public function get($columns = ['*'])
return parent::get($columns);
}

return $this->cache($this->makeCacheTags())
->rememberForever(
$this->makeCacheKey($columns),
function () use ($columns) {
return parent::get($columns);
}
);
$arguments = func_get_args();
$cacheKey = $this->makeCacheKey($columns);
$method = 'get';

return $this->cachedValue($arguments, $cacheKey, $method);
}

public function max($column)
Expand All @@ -117,13 +105,11 @@ public function max($column)
return parent::max($column);
}

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

return $this->cachedValue($arguments, $cacheKey, $method);
}

public function min($column)
Expand All @@ -132,13 +118,11 @@ public function min($column)
return parent::min($column);
}

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

return $this->cachedValue($arguments, $cacheKey, $method);
}

public function pluck($column, $key = null)
Expand All @@ -148,12 +132,11 @@ public function pluck($column, $key = null)
}

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

return $this->cache($this->makeCacheTags())
->rememberForever($cacheKey, function () use ($column, $key) {
return parent::pluck($column, $key);
});
return $this->cachedValue($arguments, $cacheKey, $method);
}

public function sum($column)
Expand All @@ -162,13 +145,11 @@ public function sum($column)
return parent::sum($column);
}

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

return $this->cachedValue($arguments, $cacheKey, $method);
}

public function value($column)
Expand All @@ -177,11 +158,56 @@ public function value($column)
return parent::value($column);
}

return $this->cache($this->makeCacheTags())
$arguments = func_get_args();
$cacheKey = $this->makeCacheKey(['*'], null, "-value_{$column}");
$method = 'value';

return $this->cachedValue($arguments, $cacheKey, $method);
}

public function cachedValue(array $arguments, string $cacheKey, string $method)
{
$cacheTags = $this->makeCacheTags();
$hashedCacheKey = sha1($cacheKey);

$result = $this->retrieveCachedValue(
$arguments,
$cacheKey,
$cacheTags,
$hashedCacheKey,
$method
);

if ($result['key'] !== $cacheKey) {
cache()->tags($cacheTags)->forget($hashedCacheKey);
}

$result = $this->retrieveCachedValue(
$arguments,
$cacheKey,
$cacheTags,
$hashedCacheKey,
$method
);

return $result['value'];
}

protected function retrieveCachedValue(
array $arguments,
string $cacheKey,
array $cacheTags,
string $hashedCacheKey,
string $method
) {
return $this->cache($cacheTags)
->rememberForever(
$this->makeCacheKey(['*'], null, "-value_{$column}"),
function () use ($column) {
return parent::value($column);
$hashedCacheKey,
function () use ($arguments, $cacheKey, $method) {
return [
'key' => $cacheKey,
'value' => parent::{$method}(...$arguments),
];
}
);
}
Expand Down
23 changes: 0 additions & 23 deletions tests/Unit/CacheKeyTest.php

This file was deleted.

Loading

0 comments on commit 9782a4c

Please sign in to comment.