Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
Changed PHPDoc classes order.
Browse files Browse the repository at this point in the history
Reworded some README.md passages.
  • Loading branch information
DarkGhostHunter committed Dec 30, 2019
1 parent e33d651 commit fa2aa97
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .phpunit.result.cache
@@ -1 +1 @@
C:37:"PHPUnit\Runner\DefaultTestResultCache":1130:{a:2:{s:7:"defects";a:6:{s:61:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testTrue";i:4;s:94:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testMacroReturnsRememberableQueryInstance";i:4;s:70:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testQueriesCached";i:3;s:78:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testEloquentBuilderCached";i:4;s:75:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testQueryBuilderCached";i:4;s:80:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testReceivesOtherRepository";i:5;}s:5:"times";a:6:{s:61:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testTrue";d:13.407;s:94:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testMacroReturnsRememberableQueryInstance";d:17.604;s:70:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testQueriesCached";d:0.961;s:78:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testEloquentBuilderCached";d:26.47;s:75:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testQueryBuilderCached";d:26.28;s:80:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testReceivesOtherRepository";d:0.714;}}}
C:37:"PHPUnit\Runner\DefaultTestResultCache":1616:{a:2:{s:7:"defects";a:8:{s:61:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testTrue";i:4;s:94:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testMacroReturnsRememberableQueryInstance";i:4;s:70:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testQueriesCached";i:3;s:78:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testEloquentBuilderCached";i:4;s:75:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testQueryBuilderCached";i:4;s:80:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testReceivesOtherRepository";i:5;s:66:"Tests\RememberableTest::testDifferentBuilderOrderGivesDifferentKey";i:4;s:45:"Tests\RememberableTest::testChangesCacheStore";i:4;}s:5:"times";a:11:{s:61:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testTrue";d:13.407;s:94:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testMacroReturnsRememberableQueryInstance";d:17.604;s:70:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testQueriesCached";d:0.961;s:78:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testEloquentBuilderCached";d:26.47;s:75:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testQueryBuilderCached";d:26.28;s:80:"DarkGhostHunter\RememberableQuery\Tests\ExampleTest::testReceivesOtherRepository";d:0.714;s:66:"Tests\RememberableTest::testDifferentBuilderOrderGivesDifferentKey";d:0.785;s:45:"Tests\RememberableTest::testChangesCacheStore";d:0.896;s:65:"Tests\RememberableTest::testMacroReturnsRememberableQueryInstance";d:0.85;s:49:"Tests\RememberableTest::testEloquentBuilderCached";d:0.212;s:46:"Tests\RememberableTest::testQueryBuilderCached";d:0.229;}}}
58 changes: 34 additions & 24 deletions README.md
Expand Up @@ -14,8 +14,8 @@ Remember your Query results using only one method. Yes, only one.
## Requirements

* PHP 7.2 or latest
* Laravel 5.8|6.0
* A functioning brain
* Laravel 5.8|6.x
* A working brain

## Installation

Expand All @@ -33,48 +33,31 @@ Just use the `remember()` method to remember a Query result. That's it.
use Illuminate\Support\Facades\DB;
use Illuminate\Foundation\Auth\User;

$userA = DB::table('users')->remember()->where('name', 'Joe')->first();
$query = DB::table('users')->remember()->where('name', 'Joe')->first();

$userB = User::where('name', 'Joe')->remember()->first();
$eloquent = User::where('name', 'Joe')->remember()->first();
```

The next time you call the **same** query, the result will be retrieved from the cache instead of connecting to Database.
The next time you call the **same** query, the result will be retrieved from the cache instead of running the SQL statement in the database.

> If the result is `null` or `false`, it won't be remembered, which mimics the Cache behaviour on these values.
### Time-to-live

By default, queries are remembered by 60 seconds, but you're free to use any length, or Datetime or Carbon instance.
By default, queries are remembered by 60 seconds, but you're free to use any length, Datetime, DateInterval or Carbon instance.

```php
User::where('name', 'Joe')->remember(today()->addHour())->first();
```

### Custom Cache Key

By default, the cache key is an MD5 hash of the SQL query and bindings, which avoids any collision with other queries. You can use any string, but is recommended to append `query|myCustomKey` to avoid conflicts with other cache keys.
By default, the cache key is an MD5 hash of the SQL query and bindings, which avoids any collision with other queries. You can use any string, but is recommended to append `query|{key}` to avoid conflicts with other cache keys in your application.

```php
User::where('name', 'Joe')->remember(30, 'query|find_joe')->first();
```


### When two Builders are not the same

Consider that altering the Builder methods order may change the automatic cache key generation, even if they are practically the same. For example:

```php
<?php

DB::table('users')->remember()->whereName('Joe')->whereAge(20)->first();
// "query|fecc2c1bb6396e485d94eede60532937"

DB::table('users')->remember()->whereAge(20)->whereName('Joe')->first();
// "query|3ac5eba7cd0ef6151481bdfe46f6c22f"
```

If you plan to _remember_ the same query on different parts of your application, it's recommended to set manually the same Cache Key to avoid using different cache keys.

### Custom Cache

In some scenarios, using the default cache of your application may be detrimental compared to the database performance. You can use any other Cache by telling the Service Container to pass it to the `RememberableQuery` class (preferably) in your `AppServiceProvider`.
Expand All @@ -93,6 +76,33 @@ public function boot()
}
```

## Mind the gap

There are two things you should be warned about.

### Operations are **NOT** commutative

Altering the Builder methods order may change the automatic cache key generation. Even if they are *practically* the same, the order of statements makes them different. For example:

```php
<?php

DB::table('users')->remember()->whereName('Joe')->whereAge(20)->first();
// "query|fecc2c1bb6396e485d94eede60532937"

DB::table('users')->remember()->whereAge(20)->whereName('Joe')->first();
// "query|3ac5eba7cd0ef6151481bdfe46f6c22f"
```

If you plan to _remember_ the same query on different parts of your application, it's recommended to set manually the same Cache Key to ensure hitting the cached results.

### Only works for SELECT statements

The nature of remembering a Query is to cache the result automatically.

Caching the result for `UPDATE`, `DELETE` and `INSERT` operations will cache the result and subsequents operations won't be executed, returning unexpected results.

Don't use `remember()` on anything that is not a `SELECT` statement.

## License

Expand Down
2 changes: 1 addition & 1 deletion src/RememberableQuery.php
Expand Up @@ -21,7 +21,7 @@ class RememberableQuery
/**
* Query Builder instance
*
* @var \Illuminate\Database\Query\Builder|\Illuminate\Database\Eloquent\Builder
* @var \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
*/
protected $builder;

Expand Down

0 comments on commit fa2aa97

Please sign in to comment.