Skip to content

Commit

Permalink
Merge pull request #56 from renoki-co/fix/string-call
Browse files Browse the repository at this point in the history
[fix] Bugs with columns passed as string
  • Loading branch information
rennokki authored Feb 4, 2021
2 parents 1440be4 + fb1367d commit 6c0f022
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 16 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ CustomModel::cacheFor(30)->customGetMethod();
This is how the default key generation function looks like:

```php
public function generatePlainCacheKey(string $method = 'get', $id = null, $appends = null): string
public function generatePlainCacheKey(string $method = 'get', string $id = null, string $appends = null): string
{
$name = $this->connection->getName();

Expand All @@ -359,7 +359,7 @@ class MyCustomBuilder implements QueryCacheModuleInterface
{
use QueryCacheModule;

public function generatePlainCacheKey(string $method = 'get', $id = null, $appends = null): string
public function generatePlainCacheKey(string $method = 'get', string $id = null, string $appends = null): string
{
$name = $this->connection->getName();

Expand All @@ -374,12 +374,14 @@ class MyCustomBuilder implements QueryCacheModuleInterface
Since all of the Laravel Eloquent functions are based on it, the builder that comes with this package replaces only the `get()` one:

```php
use Illuminate\Support\Arr;

class Builder
{
public function get($columns = ['*'])
{
if (! $this->shouldAvoidCache()) {
return $this->getFromQueryCache('get', $columns);
return $this->getFromQueryCache('get', Arr::wrap($columns));
}

return parent::get($columns);
Expand Down Expand Up @@ -413,9 +415,11 @@ The default behaviour of the package doesn't use it, since the query builder use
However, if your builder replaces functions like `find()`, `$id` is needed and you will also have to replace the `getQueryCacheCallback()` like so:

```php
use Illuminate\Support\Arr;

class MyCustomBuilder
{
public function getQueryCacheCallback(string $method = 'get', $columns = ['*'], $id = null)
public function getQueryCacheCallback(string $method = 'get', $columns = ['*'], string $id = null)
{
return function () use ($method, $columns, $id) {
$this->avoidCache = true;
Expand All @@ -434,7 +438,7 @@ class MyCustomBuilder
{
// implementing the same logic
if (! $this->shouldAvoidCache()) {
return $this->getFromQueryCache('find', $columns, $id);
return $this->getFromQueryCache('find', Arr::wrap($columns), $id);
}

return parent::find($id, $columns);
Expand Down
6 changes: 3 additions & 3 deletions src/Contracts/QueryCacheModuleInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ interface QueryCacheModuleInterface
* @param string|null $appends
* @return string
*/
public function generatePlainCacheKey(string $method = 'get', $id = null, $appends = null): string;
public function generatePlainCacheKey(string $method = 'get', string $id = null, string $appends = null): string;

/**
* Get the query cache callback.
*
* @param string $method
* @param array $columns
* @param array|string $columns
* @param string|null $id
* @return \Closure
*/
public function getQueryCacheCallback(string $method = 'get', $columns = ['*'], $id = null);
public function getQueryCacheCallback(string $method = 'get', $columns = ['*'], string $id = null);
}
5 changes: 3 additions & 2 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Rennokki\QueryCache\Query;

use Illuminate\Database\Query\Builder as BaseBuilder;
use Illuminate\Support\Arr;
use Rennokki\QueryCache\Contracts\QueryCacheModuleInterface;
use Rennokki\QueryCache\Traits\QueryCacheModule;

Expand All @@ -17,7 +18,7 @@ public function get($columns = ['*'])
{
return $this->shouldAvoidCache()
? parent::get($columns)
: $this->getFromQueryCache('get', $columns);
: $this->getFromQueryCache('get', Arr::wrap($columns));
}

/**
Expand Down Expand Up @@ -45,7 +46,7 @@ public function useWritePdo()
*/
public function selectSub($query, $as)
{
if (get_class($query) == self::class) {
if (! is_string($query) && get_class($query) == self::class) {
$this->appendCacheTags($query->getCacheTags() ?? []);
}

Expand Down
13 changes: 7 additions & 6 deletions src/Traits/QueryCacheModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ trait QueryCacheModule
/**
* Get the cache from the current query.
*
* @param string $method
* @param array $columns
* @param string|null $id
* @return array
*/
public function getFromQueryCache(string $method = 'get', $columns = ['*'], $id = null)
public function getFromQueryCache(string $method = 'get', array $columns = ['*'], string $id = null)
{
if (is_null($this->columns)) {
$this->columns = $columns;
Expand All @@ -90,11 +91,11 @@ public function getFromQueryCache(string $method = 'get', $columns = ['*'], $id
* Get the query cache callback.
*
* @param string $method
* @param array $columns
* @param array|string $columns
* @param string|null $id
* @return \Closure
*/
public function getQueryCacheCallback(string $method = 'get', $columns = ['*'], $id = null)
public function getQueryCacheCallback(string $method = 'get', $columns = ['*'], string $id = null)
{
return function () use ($method, $columns) {
$this->avoidCache = true;
Expand All @@ -111,7 +112,7 @@ public function getQueryCacheCallback(string $method = 'get', $columns = ['*'],
* @param string|null $appends
* @return string
*/
public function getCacheKey(string $method = 'get', $id = null, $appends = null): string
public function getCacheKey(string $method = 'get', string $id = null, string $appends = null): string
{
$key = $this->generateCacheKey($method, $id, $appends);
$prefix = $this->getCachePrefix();
Expand All @@ -127,7 +128,7 @@ public function getCacheKey(string $method = 'get', $id = null, $appends = null)
* @param string|null $appends
* @return string
*/
public function generateCacheKey(string $method = 'get', $id = null, $appends = null): string
public function generateCacheKey(string $method = 'get', string $id = null, string $appends = null): string
{
$key = $this->generatePlainCacheKey($method, $id, $appends);

Expand All @@ -146,7 +147,7 @@ public function generateCacheKey(string $method = 'get', $id = null, $appends =
* @param string|null $appends
* @return string
*/
public function generatePlainCacheKey(string $method = 'get', $id = null, $appends = null): string
public function generatePlainCacheKey(string $method = 'get', string $id = null, string $appends = null): string
{
$name = $this->connection->getName();

Expand Down
19 changes: 19 additions & 0 deletions tests/GetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,23 @@ public function test_get_with_columns()
$post->name
);
}

public function test_get_with_string_columns()
{
$post = factory(Post::class)->create();
$storedPosts = Post::cacheFor(now()->addHours(1))->get('name');
$cache = Cache::get('leqc:sqlitegetselect "name" from "posts"a:0:{}');

$this->assertNotNull($cache);

$this->assertEquals(
$cache->first()->name,
$storedPosts->first()->name
);

$this->assertEquals(
$cache->first()->name,
$post->name
);
}
}

0 comments on commit 6c0f022

Please sign in to comment.