From b8012fab298731a6526cd4701531df184db9cc02 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Sat, 23 Nov 2019 08:28:42 +0200 Subject: [PATCH 1/5] Caching composer. --- .travis.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.travis.yml b/.travis.yml index 7c9445a..92b3a77 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,6 +8,10 @@ env: matrix: - COMPOSER_FLAGS="" +cache: + directories: + - $HOME/.composer/cache + before_script: - travis_retry composer self-update - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source From b31eb03be37dc6bcdbff2d8947bceeb03638c9a0 Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Sat, 23 Nov 2019 08:28:49 +0200 Subject: [PATCH 2/5] Fixed readme. --- README.md | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/README.md b/README.md index a41117e..c1f3705 100644 --- a/README.md +++ b/README.md @@ -87,12 +87,6 @@ $alice = Kid::whereName('Alice')->cacheFor(60)->cacheTags(['kids'])->first(); $bob = Kid::whereName('Bob')->cacheFor(60)->cacheTags(['kids'])->first(); ``` -In case you want to invalidate all the cache, don't specify an argument for the `flushQueryCache()` method: - -```php -Problem::flushQueryCache(); // bye-bye problems! -``` - ## Relationship Caching Relationships are just another queries. They can be intercepted and modified before the database is hit with the query. The following example needs the `Order` model (or the model associated with the `orders` relationship) to include the `QueryCacheable` trait. @@ -109,7 +103,7 @@ $orders = $user->orders; The package automatically generate the keys needed to store the data in the cache store. However, prefixing them might be useful if the cache store is used by other applications and/or models and you want to manage the keys better to avoid collisions. ```php -$bob = Kid::whereName('Bob')->cacheFor(60)->prefix('kids_')->first(); +$bob = Kid::whereName('Bob')->cacheFor(60)->cachePrefix('kids_')->first(); ``` If no prefix is specified, the string `leqc` is going to be used. From 25638245188c5302497982f177dbc520f1dec72d Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Sat, 23 Nov 2019 08:29:00 +0200 Subject: [PATCH 3/5] wip. --- src/Query/Builder.php | 15 +++----- src/Traits/QueryCacheable.php | 2 +- tests/MethodsTest.php | 71 +++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 11 deletions(-) create mode 100644 tests/MethodsTest.php diff --git a/src/Query/Builder.php b/src/Query/Builder.php index 6f22c56..cda817d 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -101,10 +101,6 @@ protected function getQueryCacheCallback(string $method = 'get', $columns = ['*' return function () use ($method, $columns) { $this->avoidCache = true; - if ($method === 'get') { - return $this->get($columns); - } - return $this->{$method}($columns); }; } @@ -170,7 +166,7 @@ public function generatePlainCacheKey(string $method = 'get', $id = null, $appen * @param array $tags * @return bool */ - public function flushQueryCache(array $tags = []): bool + public function flushQueryCache(array $tags = ['leqc']): bool { $cache = $this->getCacheDriver(); @@ -219,12 +215,11 @@ public function cacheFor($seconds) /** * Indicate that the query results should be cached forever. * - * @param string|null $key * @return \Illuminate\Database\Query\Builder|static */ - public function cacheForever($key = null) + public function cacheForever() { - return $this->cacheFor(-1, $key); + return $this->cacheFor(-1); } /** @@ -234,7 +229,7 @@ public function cacheForever($key = null) */ public function dontCache() { - $this->cacheTime = $this->cacheTags = null; + $this->avoidCache = true; return $this; } @@ -255,7 +250,7 @@ public function doNotCache() * @param string $prefix * @return \Rennokki\QueryCache\Query\Builder */ - public function prefix(string $prefix) + public function cachePrefix(string $prefix) { $this->cachePrefix = $prefix; diff --git a/src/Traits/QueryCacheable.php b/src/Traits/QueryCacheable.php index faa8a1e..f37b5be 100644 --- a/src/Traits/QueryCacheable.php +++ b/src/Traits/QueryCacheable.php @@ -28,7 +28,7 @@ protected function newBaseQueryBuilder() } if ($this->cachePrefix) { - $builder->prefix($this->cachePrefix); + $builder->cachePrefix($this->cachePrefix); } if ($this->cacheDriver) { diff --git a/tests/MethodsTest.php b/tests/MethodsTest.php new file mode 100644 index 0000000..12cc514 --- /dev/null +++ b/tests/MethodsTest.php @@ -0,0 +1,71 @@ +create(); + + $storedPost = Post::cacheFor(now()->addHours(1))->doNotCache()->first(); + $cache = Cache::get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}'); + $this->assertNull($cache); + + $storedPost = Post::cacheFor(now()->addHours(1))->dontCache()->first(); + $cache = Cache::get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}'); + $this->assertNull($cache); + } + + public function test_cache_prefix() + { + $post = factory(Post::class)->create(); + $storedPost = Post::cacheFor(now()->addHours(1))->cachePrefix('test')->first(); + $cache = Cache::get('test:sqlitegetselect * from "posts" limit 1a:0:{}'); + + $this->assertNotNull($cache); + } + + public function test_cache_tags() + { + $post = factory(Post::class)->create(); + $storedPost = Post::cacheFor(now()->addHours(1))->cacheTags(['test'])->first(); + + $cache = Cache::get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}'); + $this->assertNull($cache); + + $cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}'); + $this->assertNotNull($cache); + } + + public function test_cache_flush_with_the_right_tag() + { + $post = factory(Post::class)->create(); + $storedPost = Post::cacheFor(now()->addHours(1))->cacheTags(['test'])->first(); + + $cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}'); + $this->assertNotNull($cache); + + Post::flushQueryCache(['test']); + + $cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}'); + $this->assertNull($cache); + } + + public function test_cache_flush_without_the_right_tag() + { + $post = factory(Post::class)->create(); + $storedPost = Post::cacheFor(now()->addHours(1))->cacheTags(['test'])->first(); + + $cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}'); + $this->assertNotNull($cache); + + Post::flushQueryCache(['test2']); + + $cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}'); + $this->assertNotNull($cache); + } +} From 2a8f7057f13462d1176f7b82be5d036d9357d4ac Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Sat, 23 Nov 2019 08:58:10 +0200 Subject: [PATCH 4/5] wip. --- database/factories/KidFactory.php | 19 +++++++++++ src/Query/Builder.php | 7 ++-- tests/MethodsTest.php | 11 +++++++ tests/Models/Kid.php | 15 +++++++++ tests/TestCase.php | 1 + .../migrations/2018_07_14_183253_kids.php | 32 +++++++++++++++++++ 6 files changed, 81 insertions(+), 4 deletions(-) create mode 100644 database/factories/KidFactory.php create mode 100644 tests/Models/Kid.php create mode 100644 tests/database/migrations/2018_07_14_183253_kids.php diff --git a/database/factories/KidFactory.php b/database/factories/KidFactory.php new file mode 100644 index 0000000..40c3815 --- /dev/null +++ b/database/factories/KidFactory.php @@ -0,0 +1,19 @@ +define(\Rennokki\QueryCache\Test\Models\Kid::class, function () { + return [ + 'name' => 'Kid'.Str::random(5), + ]; +}); diff --git a/src/Query/Builder.php b/src/Query/Builder.php index cda817d..1ad3497 100644 --- a/src/Query/Builder.php +++ b/src/Query/Builder.php @@ -78,7 +78,6 @@ protected function getFromQueryCache(string $method = 'get', $columns = ['*']) } $key = $this->getCacheKey('get'); - $seconds = $this->cacheTime; $cache = $this->getCache(); $callback = $this->getQueryCacheCallback($method, $columns); @@ -202,12 +201,12 @@ protected function getCache() /** * Indicate that the query results should be cached. * - * @param \DateTime|int $seconds + * @param \DateTime|int $time * @return \Rennokki\QueryCache\Query\Builder */ - public function cacheFor($seconds) + public function cacheFor($time) { - $this->cacheTime = $seconds; + $this->cacheTime = $time; return $this; } diff --git a/tests/MethodsTest.php b/tests/MethodsTest.php index 12cc514..a6a6efd 100644 --- a/tests/MethodsTest.php +++ b/tests/MethodsTest.php @@ -2,7 +2,9 @@ namespace Rennokki\QueryCache\Test; +use DB; use Cache; +use Rennokki\QueryCache\Test\Models\Kid; use Rennokki\QueryCache\Test\Models\Post; class MethodsTest extends TestCase @@ -68,4 +70,13 @@ public function test_cache_flush_without_the_right_tag() $cache = Cache::tags(['test'])->get('leqc:sqlitegetselect * from "posts" limit 1a:0:{}'); $this->assertNotNull($cache); } + + public function test_hashed_key() + { + $kid = factory(Kid::class)->create(); + $storedKid = Kid::cacheFor(now()->addHours(1))->first(); + $cache = Cache::get('leqc:156667fa9bcb7fb8abb01018568648406f251ef65736e89e6fd27d08bc48b5bb'); + + $this->assertNotNull($cache); + } } diff --git a/tests/Models/Kid.php b/tests/Models/Kid.php new file mode 100644 index 0000000..453e09f --- /dev/null +++ b/tests/Models/Kid.php @@ -0,0 +1,15 @@ +set('auth.providers.users.model', User::class); $app['config']->set('auth.providers.posts.model', Post::class); + $app['config']->set('auth.providers.kids.model', Kid::class); $app['config']->set('app.key', 'wslxrEFGWY6GfGhvN9L3wH3KSRJQQpBD'); } diff --git a/tests/database/migrations/2018_07_14_183253_kids.php b/tests/database/migrations/2018_07_14_183253_kids.php new file mode 100644 index 0000000..dcc6085 --- /dev/null +++ b/tests/database/migrations/2018_07_14_183253_kids.php @@ -0,0 +1,32 @@ +increments('id'); + $table->string('name'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('kids'); + } +} From 7fb7d1ce413246d4e641af0145e10bbe4215e2dd Mon Sep 17 00:00:00 2001 From: rennokki Date: Sat, 23 Nov 2019 07:04:01 +0000 Subject: [PATCH 5/5] Apply fixes from StyleCI --- tests/MethodsTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/MethodsTest.php b/tests/MethodsTest.php index a6a6efd..9a8d4a3 100644 --- a/tests/MethodsTest.php +++ b/tests/MethodsTest.php @@ -2,7 +2,6 @@ namespace Rennokki\QueryCache\Test; -use DB; use Cache; use Rennokki\QueryCache\Test\Models\Kid; use Rennokki\QueryCache\Test\Models\Post;