Skip to content

Commit 2563824

Browse files
committed
wip.
1 parent b31eb03 commit 2563824

File tree

3 files changed

+77
-11
lines changed

3 files changed

+77
-11
lines changed

src/Query/Builder.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,6 @@ protected function getQueryCacheCallback(string $method = 'get', $columns = ['*'
101101
return function () use ($method, $columns) {
102102
$this->avoidCache = true;
103103

104-
if ($method === 'get') {
105-
return $this->get($columns);
106-
}
107-
108104
return $this->{$method}($columns);
109105
};
110106
}
@@ -170,7 +166,7 @@ public function generatePlainCacheKey(string $method = 'get', $id = null, $appen
170166
* @param array $tags
171167
* @return bool
172168
*/
173-
public function flushQueryCache(array $tags = []): bool
169+
public function flushQueryCache(array $tags = ['leqc']): bool
174170
{
175171
$cache = $this->getCacheDriver();
176172

@@ -219,12 +215,11 @@ public function cacheFor($seconds)
219215
/**
220216
* Indicate that the query results should be cached forever.
221217
*
222-
* @param string|null $key
223218
* @return \Illuminate\Database\Query\Builder|static
224219
*/
225-
public function cacheForever($key = null)
220+
public function cacheForever()
226221
{
227-
return $this->cacheFor(-1, $key);
222+
return $this->cacheFor(-1);
228223
}
229224

230225
/**
@@ -234,7 +229,7 @@ public function cacheForever($key = null)
234229
*/
235230
public function dontCache()
236231
{
237-
$this->cacheTime = $this->cacheTags = null;
232+
$this->avoidCache = true;
238233

239234
return $this;
240235
}
@@ -255,7 +250,7 @@ public function doNotCache()
255250
* @param string $prefix
256251
* @return \Rennokki\QueryCache\Query\Builder
257252
*/
258-
public function prefix(string $prefix)
253+
public function cachePrefix(string $prefix)
259254
{
260255
$this->cachePrefix = $prefix;
261256

src/Traits/QueryCacheable.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected function newBaseQueryBuilder()
2828
}
2929

3030
if ($this->cachePrefix) {
31-
$builder->prefix($this->cachePrefix);
31+
$builder->cachePrefix($this->cachePrefix);
3232
}
3333

3434
if ($this->cacheDriver) {

tests/MethodsTest.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Rennokki\QueryCache\Test;
4+
5+
use Cache;
6+
use Rennokki\QueryCache\Test\Models\Post;
7+
8+
class MethodsTest extends TestCase
9+
{
10+
public function test_do_not_cache()
11+
{
12+
$post = factory(Post::class)->create();
13+
14+
$storedPost = Post::cacheFor(now()->addHours(1))->doNotCache()->first();
15+
$cache = Cache::get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
16+
$this->assertNull($cache);
17+
18+
$storedPost = Post::cacheFor(now()->addHours(1))->dontCache()->first();
19+
$cache = Cache::get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
20+
$this->assertNull($cache);
21+
}
22+
23+
public function test_cache_prefix()
24+
{
25+
$post = factory(Post::class)->create();
26+
$storedPost = Post::cacheFor(now()->addHours(1))->cachePrefix('test')->first();
27+
$cache = Cache::get('test:sqlitegetselect * from "posts" limit 1a:0:{}');
28+
29+
$this->assertNotNull($cache);
30+
}
31+
32+
public function test_cache_tags()
33+
{
34+
$post = factory(Post::class)->create();
35+
$storedPost = Post::cacheFor(now()->addHours(1))->cacheTags(['test'])->first();
36+
37+
$cache = Cache::get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
38+
$this->assertNull($cache);
39+
40+
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
41+
$this->assertNotNull($cache);
42+
}
43+
44+
public function test_cache_flush_with_the_right_tag()
45+
{
46+
$post = factory(Post::class)->create();
47+
$storedPost = Post::cacheFor(now()->addHours(1))->cacheTags(['test'])->first();
48+
49+
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
50+
$this->assertNotNull($cache);
51+
52+
Post::flushQueryCache(['test']);
53+
54+
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
55+
$this->assertNull($cache);
56+
}
57+
58+
public function test_cache_flush_without_the_right_tag()
59+
{
60+
$post = factory(Post::class)->create();
61+
$storedPost = Post::cacheFor(now()->addHours(1))->cacheTags(['test'])->first();
62+
63+
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
64+
$this->assertNotNull($cache);
65+
66+
Post::flushQueryCache(['test2']);
67+
68+
$cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
69+
$this->assertNotNull($cache);
70+
}
71+
}

0 commit comments

Comments
 (0)