Skip to content

Commit

Permalink
Merge pull request #38 from betafashion/append-cache
Browse files Browse the repository at this point in the history
Append cache tags
  • Loading branch information
rennokki authored Aug 29, 2020
2 parents 460fd39 + 0883a47 commit 4f9eae9
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,22 @@ public function useWritePdo()

return $this;
}

/**
* Add a subselect expression to the query.
*
* @param \Closure|$this|string $query
* @param string $as
* @return $this
*
* @throws \InvalidArgumentException
*/
public function selectSub($query, $as)
{
if (get_class($query) == self::class) {
$this->appendCacheTags($query->getCacheTags() ?? []);
}

return parent::selectSub($query, $as);
}
}
13 changes: 13 additions & 0 deletions src/Traits/QueryCacheModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,19 @@ public function cacheTags(array $cacheTags = [])
return $this;
}

/**
* Append tags to the cache.
*
* @param array $cacheTags
* @return \Rennokki\QueryCache\Query\Builder
*/
public function appendCacheTags(array $cacheTags = [])
{
$this->cacheTags = array_unique(array_merge($this->cacheTags ?? [], $cacheTags));

return $this;
}

/**
* Use a specific cache driver.
*
Expand Down
47 changes: 47 additions & 0 deletions tests/MethodsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Rennokki\QueryCache\Test\Models\Book;
use Rennokki\QueryCache\Test\Models\Kid;
use Rennokki\QueryCache\Test\Models\Post;
use Rennokki\QueryCache\Test\Models\User;

class MethodsTest extends TestCase
{
Expand Down Expand Up @@ -123,4 +124,50 @@ public function test_hashed_key()

$this->assertNotNull($cache);
}

public function test_append_cache_tags()
{
$post = factory(Post::class)->create();
$storedPost = Post::cacheFor(now()->addHours(1))->appendCacheTags(['test'])->first();

$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "posts" limit 1a:0:{}');

// The caches that do not support tagging should
// cache the query either way.
$this->driverSupportsTags()
? $this->assertNull($cache)
: $this->assertNotNull($cache);

$cache = $this->getCacheWithTags('leqc:sqlitegetselect * from "posts" limit 1a:0:{}', ['test']);
$this->assertNotNull($cache);
}

public function test_multiple_append_cache_tags()
{
$post = factory(Post::class)->create();
$storedPostQuery = Post::cacheFor(now()->addHours(1))->appendCacheTags(['test'])->appendCacheTags(['test2']);

$this->assertEquals($storedPostQuery->getQuery()->getCacheTags(), ['test', 'test2']);
}

public function test_append_cache_tags_with_sub_query()
{
$user = factory(User::class)->create();
factory(Post::class)->createMany([
['user_id' => $user->id, 'name' => 'Post 1 on topic 1'],
['user_id' => $user->id, 'name' => 'Post 2 on topic 1'],
['user_id' => $user->id, 'name' => 'Post 3 on topic 2'],
]);

$userAndPosts = User::cacheFor(now()->addHours(1))
->withCount([
'posts' => function ($query) {
$query->appendCacheTags(['posts'])
->where('name', 'like', '%topic 1%');
},
])
->appendCacheTags(['user']);

$this->assertEquals($userAndPosts->getQuery()->getCacheTags(), ['posts', 'user']);
}
}
8 changes: 8 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace Rennokki\QueryCache\Test\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Rennokki\QueryCache\Traits\QueryCacheable;

class User extends Authenticatable
{
use QueryCacheable;

protected $fillable = [
'name', 'email', 'password',
];
Expand All @@ -20,4 +23,9 @@ protected function getCacheBaseTags(): array
//
];
}

public function posts()
{
return $this->hasMany(Post::class);
}
}
1 change: 1 addition & 0 deletions tests/database/migrations/2018_07_14_183253_posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->unsignedBigInteger('user_id')->nullable();
$table->string('name');
$table->timestamps();
});
Expand Down

0 comments on commit 4f9eae9

Please sign in to comment.