Skip to content

Commit

Permalink
Merge pull request #4 from rennokki/increase-code-coverage
Browse files Browse the repository at this point in the history
Increase the code coverage
  • Loading branch information
rennokki authored Nov 23, 2019
2 parents 39f003d + 02ec858 commit 8606561
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 22 deletions.
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions database/factories/KidFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/

use Illuminate\Support\Str;

$factory->define(\Rennokki\QueryCache\Test\Models\Kid::class, function () {
return [
'name' => 'Kid'.Str::random(5),
];
});
22 changes: 8 additions & 14 deletions src/Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
};
}
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -206,25 +201,24 @@ 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;
}

/**
* 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);
}

/**
Expand All @@ -234,7 +228,7 @@ public function cacheForever($key = null)
*/
public function dontCache()
{
$this->cacheTime = $this->cacheTags = null;
$this->avoidCache = true;

return $this;
}
Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/Traits/QueryCacheable.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function newBaseQueryBuilder()
}

if ($this->cachePrefix) {
$builder->prefix($this->cachePrefix);
$builder->cachePrefix($this->cachePrefix);
}

if ($this->cacheDriver) {
Expand Down
81 changes: 81 additions & 0 deletions tests/MethodsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Rennokki\QueryCache\Test;

use Cache;
use Rennokki\QueryCache\Test\Models\Kid;
use Rennokki\QueryCache\Test\Models\Post;

class MethodsTest extends TestCase
{
public function test_do_not_cache()
{
$post = factory(Post::class)->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);
}
}
15 changes: 15 additions & 0 deletions tests/Models/Kid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rennokki\QueryCache\Test\Models;

use Illuminate\Database\Eloquent\Model;
use Rennokki\QueryCache\Traits\QueryCacheable;

class Kid extends Model
{
use QueryCacheable;

protected $fillable = [
'name',
];
}
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function getEnvironmentSetUp($app)
]);
$app['config']->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');
}

Expand Down
32 changes: 32 additions & 0 deletions tests/database/migrations/2018_07_14_183253_kids.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class Kids extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('kids', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('kids');
}
}

0 comments on commit 8606561

Please sign in to comment.