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 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. 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 6f22c56..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); @@ -101,10 +100,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 +165,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(); @@ -206,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; } @@ -219,12 +214,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 +228,7 @@ public function cacheForever($key = null) */ public function dontCache() { - $this->cacheTime = $this->cacheTags = null; + $this->avoidCache = true; return $this; } @@ -255,7 +249,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..9a8d4a3 --- /dev/null +++ b/tests/MethodsTest.php @@ -0,0 +1,81 @@ +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); + } + + 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'); + } +}