Skip to content

Commit 6c0f022

Browse files
authored
Merge pull request #56 from renoki-co/fix/string-call
[fix] Bugs with columns passed as string
2 parents 1440be4 + fb1367d commit 6c0f022

File tree

5 files changed

+41
-16
lines changed

5 files changed

+41
-16
lines changed

README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ CustomModel::cacheFor(30)->customGetMethod();
336336
This is how the default key generation function looks like:
337337

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

@@ -359,7 +359,7 @@ class MyCustomBuilder implements QueryCacheModuleInterface
359359
{
360360
use QueryCacheModule;
361361

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

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

376376
```php
377+
use Illuminate\Support\Arr;
378+
377379
class Builder
378380
{
379381
public function get($columns = ['*'])
380382
{
381383
if (! $this->shouldAvoidCache()) {
382-
return $this->getFromQueryCache('get', $columns);
384+
return $this->getFromQueryCache('get', Arr::wrap($columns));
383385
}
384386

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

415417
```php
418+
use Illuminate\Support\Arr;
419+
416420
class MyCustomBuilder
417421
{
418-
public function getQueryCacheCallback(string $method = 'get', $columns = ['*'], $id = null)
422+
public function getQueryCacheCallback(string $method = 'get', $columns = ['*'], string $id = null)
419423
{
420424
return function () use ($method, $columns, $id) {
421425
$this->avoidCache = true;
@@ -434,7 +438,7 @@ class MyCustomBuilder
434438
{
435439
// implementing the same logic
436440
if (! $this->shouldAvoidCache()) {
437-
return $this->getFromQueryCache('find', $columns, $id);
441+
return $this->getFromQueryCache('find', Arr::wrap($columns), $id);
438442
}
439443

440444
return parent::find($id, $columns);

src/Contracts/QueryCacheModuleInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ interface QueryCacheModuleInterface
1212
* @param string|null $appends
1313
* @return string
1414
*/
15-
public function generatePlainCacheKey(string $method = 'get', $id = null, $appends = null): string;
15+
public function generatePlainCacheKey(string $method = 'get', string $id = null, string $appends = null): string;
1616

1717
/**
1818
* Get the query cache callback.
1919
*
2020
* @param string $method
21-
* @param array $columns
21+
* @param array|string $columns
2222
* @param string|null $id
2323
* @return \Closure
2424
*/
25-
public function getQueryCacheCallback(string $method = 'get', $columns = ['*'], $id = null);
25+
public function getQueryCacheCallback(string $method = 'get', $columns = ['*'], string $id = null);
2626
}

src/Query/Builder.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Rennokki\QueryCache\Query;
44

55
use Illuminate\Database\Query\Builder as BaseBuilder;
6+
use Illuminate\Support\Arr;
67
use Rennokki\QueryCache\Contracts\QueryCacheModuleInterface;
78
use Rennokki\QueryCache\Traits\QueryCacheModule;
89

@@ -17,7 +18,7 @@ public function get($columns = ['*'])
1718
{
1819
return $this->shouldAvoidCache()
1920
? parent::get($columns)
20-
: $this->getFromQueryCache('get', $columns);
21+
: $this->getFromQueryCache('get', Arr::wrap($columns));
2122
}
2223

2324
/**
@@ -45,7 +46,7 @@ public function useWritePdo()
4546
*/
4647
public function selectSub($query, $as)
4748
{
48-
if (get_class($query) == self::class) {
49+
if (! is_string($query) && get_class($query) == self::class) {
4950
$this->appendCacheTags($query->getCacheTags() ?? []);
5051
}
5152

src/Traits/QueryCacheModule.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,12 @@ trait QueryCacheModule
6464
/**
6565
* Get the cache from the current query.
6666
*
67+
* @param string $method
6768
* @param array $columns
6869
* @param string|null $id
6970
* @return array
7071
*/
71-
public function getFromQueryCache(string $method = 'get', $columns = ['*'], $id = null)
72+
public function getFromQueryCache(string $method = 'get', array $columns = ['*'], string $id = null)
7273
{
7374
if (is_null($this->columns)) {
7475
$this->columns = $columns;
@@ -90,11 +91,11 @@ public function getFromQueryCache(string $method = 'get', $columns = ['*'], $id
9091
* Get the query cache callback.
9192
*
9293
* @param string $method
93-
* @param array $columns
94+
* @param array|string $columns
9495
* @param string|null $id
9596
* @return \Closure
9697
*/
97-
public function getQueryCacheCallback(string $method = 'get', $columns = ['*'], $id = null)
98+
public function getQueryCacheCallback(string $method = 'get', $columns = ['*'], string $id = null)
9899
{
99100
return function () use ($method, $columns) {
100101
$this->avoidCache = true;
@@ -111,7 +112,7 @@ public function getQueryCacheCallback(string $method = 'get', $columns = ['*'],
111112
* @param string|null $appends
112113
* @return string
113114
*/
114-
public function getCacheKey(string $method = 'get', $id = null, $appends = null): string
115+
public function getCacheKey(string $method = 'get', string $id = null, string $appends = null): string
115116
{
116117
$key = $this->generateCacheKey($method, $id, $appends);
117118
$prefix = $this->getCachePrefix();
@@ -127,7 +128,7 @@ public function getCacheKey(string $method = 'get', $id = null, $appends = null)
127128
* @param string|null $appends
128129
* @return string
129130
*/
130-
public function generateCacheKey(string $method = 'get', $id = null, $appends = null): string
131+
public function generateCacheKey(string $method = 'get', string $id = null, string $appends = null): string
131132
{
132133
$key = $this->generatePlainCacheKey($method, $id, $appends);
133134

@@ -146,7 +147,7 @@ public function generateCacheKey(string $method = 'get', $id = null, $appends =
146147
* @param string|null $appends
147148
* @return string
148149
*/
149-
public function generatePlainCacheKey(string $method = 'get', $id = null, $appends = null): string
150+
public function generatePlainCacheKey(string $method = 'get', string $id = null, string $appends = null): string
150151
{
151152
$name = $this->connection->getName();
152153

tests/GetTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,23 @@ public function test_get_with_columns()
4444
$post->name
4545
);
4646
}
47+
48+
public function test_get_with_string_columns()
49+
{
50+
$post = factory(Post::class)->create();
51+
$storedPosts = Post::cacheFor(now()->addHours(1))->get('name');
52+
$cache = Cache::get('leqc:sqlitegetselect "name" from "posts"a:0:{}');
53+
54+
$this->assertNotNull($cache);
55+
56+
$this->assertEquals(
57+
$cache->first()->name,
58+
$storedPosts->first()->name
59+
);
60+
61+
$this->assertEquals(
62+
$cache->first()->name,
63+
$post->name
64+
);
65+
}
4766
}

0 commit comments

Comments
 (0)