Skip to content

Commit 4f9eae9

Browse files
authored
Merge pull request #38 from betafashion/append-cache
Append cache tags
2 parents 460fd39 + 0883a47 commit 4f9eae9

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed

src/Query/Builder.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,22 @@ public function useWritePdo()
3333

3434
return $this;
3535
}
36+
37+
/**
38+
* Add a subselect expression to the query.
39+
*
40+
* @param \Closure|$this|string $query
41+
* @param string $as
42+
* @return $this
43+
*
44+
* @throws \InvalidArgumentException
45+
*/
46+
public function selectSub($query, $as)
47+
{
48+
if (get_class($query) == self::class) {
49+
$this->appendCacheTags($query->getCacheTags() ?? []);
50+
}
51+
52+
return parent::selectSub($query, $as);
53+
}
3654
}

src/Traits/QueryCacheModule.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,19 @@ public function cacheTags(array $cacheTags = [])
272272
return $this;
273273
}
274274

275+
/**
276+
* Append tags to the cache.
277+
*
278+
* @param array $cacheTags
279+
* @return \Rennokki\QueryCache\Query\Builder
280+
*/
281+
public function appendCacheTags(array $cacheTags = [])
282+
{
283+
$this->cacheTags = array_unique(array_merge($this->cacheTags ?? [], $cacheTags));
284+
285+
return $this;
286+
}
287+
275288
/**
276289
* Use a specific cache driver.
277290
*

tests/MethodsTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Rennokki\QueryCache\Test\Models\Book;
77
use Rennokki\QueryCache\Test\Models\Kid;
88
use Rennokki\QueryCache\Test\Models\Post;
9+
use Rennokki\QueryCache\Test\Models\User;
910

1011
class MethodsTest extends TestCase
1112
{
@@ -123,4 +124,50 @@ public function test_hashed_key()
123124

124125
$this->assertNotNull($cache);
125126
}
127+
128+
public function test_append_cache_tags()
129+
{
130+
$post = factory(Post::class)->create();
131+
$storedPost = Post::cacheFor(now()->addHours(1))->appendCacheTags(['test'])->first();
132+
133+
$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');
134+
135+
// The caches that do not support tagging should
136+
// cache the query either way.
137+
$this->driverSupportsTags()
138+
? $this->assertNull($cache)
139+
: $this->assertNotNull($cache);
140+
141+
$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "posts" limit 1a:0:{}', ['test']);
142+
$this->assertNotNull($cache);
143+
}
144+
145+
public function test_multiple_append_cache_tags()
146+
{
147+
$post = factory(Post::class)->create();
148+
$storedPostQuery = Post::cacheFor(now()->addHours(1))->appendCacheTags(['test'])->appendCacheTags(['test2']);
149+
150+
$this->assertEquals($storedPostQuery->getQuery()->getCacheTags(), ['test', 'test2']);
151+
}
152+
153+
public function test_append_cache_tags_with_sub_query()
154+
{
155+
$user = factory(User::class)->create();
156+
factory(Post::class)->createMany([
157+
['user_id' => $user->id, 'name' => 'Post 1 on topic 1'],
158+
['user_id' => $user->id, 'name' => 'Post 2 on topic 1'],
159+
['user_id' => $user->id, 'name' => 'Post 3 on topic 2'],
160+
]);
161+
162+
$userAndPosts = User::cacheFor(now()->addHours(1))
163+
->withCount([
164+
'posts' => function ($query) {
165+
$query->appendCacheTags(['posts'])
166+
->where('name', 'like', '%topic 1%');
167+
},
168+
])
169+
->appendCacheTags(['user']);
170+
171+
$this->assertEquals($userAndPosts->getQuery()->getCacheTags(), ['posts', 'user']);
172+
}
126173
}

tests/Models/User.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
namespace Rennokki\QueryCache\Test\Models;
44

55
use Illuminate\Foundation\Auth\User as Authenticatable;
6+
use Rennokki\QueryCache\Traits\QueryCacheable;
67

78
class User extends Authenticatable
89
{
10+
use QueryCacheable;
11+
912
protected $fillable = [
1013
'name', 'email', 'password',
1114
];
@@ -20,4 +23,9 @@ protected function getCacheBaseTags(): array
2023
//
2124
];
2225
}
26+
27+
public function posts()
28+
{
29+
return $this->hasMany(Post::class);
30+
}
2331
}

tests/database/migrations/2018_07_14_183253_posts.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public function up()
1515
{
1616
Schema::create('posts', function (Blueprint $table) {
1717
$table->increments('id');
18+
$table->unsignedBigInteger('user_id')->nullable();
1819
$table->string('name');
1920
$table->timestamps();
2021
});

0 commit comments

Comments
 (0)