|
1 |
| -** DO NOT INSTALL -- STILL IN EXPERIMENTAL STAGE** |
2 |
| - |
3 | 1 | # Model Caching for Laravel
|
4 | 2 | [](https://travis-ci.org/GeneaLabs/laravel-model-caching)
|
5 | 3 | [](https://insight.sensiolabs.com/projects/fde269ac-c382-4d17-a647-c69ad6b9dd85)
|
|
8 | 6 | [](https://github.com/GeneaLabs/laravel-model-caching)
|
9 | 7 | [](https://packagist.org/packages/genealabs/laravel-model-caching)
|
10 | 8 |
|
| 9 | +## Impetus |
| 10 | +I created this package in response to a client project that had complex, nested |
| 11 | +forms with many `<select>`'s that resulted in over 700 database queries on one |
| 12 | +page. I needed a package that abstracted the caching process out of the model |
| 13 | +for me, and one that would let me cache custom queries, as well as cache model |
| 14 | +relationships. This package is the attempt to address those requirements. |
| 15 | + |
11 | 16 | ## Features
|
12 |
| -- automatic relationship caching. |
13 |
| -- automatic cache flushing when models are changed. |
14 |
| -- automatic use of cache flags for cache providers that support them (will flush |
15 |
| - entire cache for providers that don't). |
16 |
| -- provides simple caching methods for use in query methods for models that take |
17 |
| - advantage of the automatic cache management features mentioned. |
| 17 | +- automatic, self-invalidating relationship caching. |
| 18 | +- automatic use of cache flags for cache providers that support them (will |
| 19 | + flush entire cache for providers that don't). |
| 20 | +- provides simple caching methods for use in query methods for models that |
| 21 | + take advantage of the automatic cache management features mentioned. |
| 22 | + |
| 23 | +## Requirements |
| 24 | +- PHP >= 7.0.0 |
| 25 | +- Laravel 5.5 |
| 26 | + |
| 27 | +## Usage |
| 28 | +For best performance a taggable cache provider is recommended (redis, |
| 29 | +memcached). While this is optional, using a non-taggable cache provider will |
| 30 | +mean that the entire cache is cleared each time a model is created, saved, |
| 31 | +updated, or deleted. |
| 32 | + |
| 33 | +For best implementation results, I would recommend adding a `BaseModel` model |
| 34 | +from which all your other models are extended. The BaseModel should extend from |
| 35 | +`CachedModel`. |
| 36 | + |
| 37 | +### Automatic Relationship Caching |
| 38 | +When writing custom queries in your models, use `$this->cache()` instead of |
| 39 | +`cache()` to automatically tag and cache the queries. These will also be auto- |
| 40 | +invalidated. |
| 41 | + |
| 42 | +```php |
| 43 | +<?php namespace App; |
| 44 | + |
| 45 | +use GeneaLabs\LaravelModelCaching\CachedModel; |
| 46 | + |
| 47 | +abstract class BaseModel extends CachedModel |
| 48 | +{ |
| 49 | + // |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +```php |
| 54 | +use Illuminate\Database\Eloquent\Relations\BelongsTo; |
| 55 | +use Illuminate\Support\Collection; |
| 56 | + |
| 57 | +class Venue extends BaseModel |
| 58 | +{ |
| 59 | + protected $fillable = [ |
| 60 | + 'name', |
| 61 | + ]; |
| 62 | + |
| 63 | + public function address() : BelongsTo |
| 64 | + { |
| 65 | + return $this->belongsTo(Address::class); |
| 66 | + } |
| 67 | + |
| 68 | + public function getAll() : Collection |
| 69 | + { |
| 70 | + return $this->cache() |
| 71 | + ->rememberForever("venues-getAll", function () { |
| 72 | + return $this->orderBy('name') |
| 73 | + ->get(); |
| 74 | + }); |
| 75 | + } |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +### Custom Query Caching |
| 80 | +**`$this->cache(array $keys)`** |
| 81 | +This method is available in any model that extends `CachedModel`, as well |
| 82 | +as automatically invalidate them. Pass in respective additional models that are |
| 83 | +represented in the query being cached. This is most often necessary when eager- |
| 84 | +loading relationships. |
| 85 | + |
| 86 | +When you create the cache key, be sure to build the key in such a way that it |
| 87 | +uniquely represents the query and does not overwrite keys of other queries. The |
| 88 | +best way to achieve this is to build the key as follows: `<model slug>-<model |
| 89 | +method>-<unique key>`. The `unique key` portion is only necessary if you pass in |
| 90 | +parameters for your query where clauses. |
| 91 | + |
| 92 | +```php |
| 93 | +public function getByTypes(array $types) : Collection |
| 94 | +{ |
| 95 | + $key = implode('-', $types); |
| 96 | + |
| 97 | + return $this->cache([ContactType::class]) |
| 98 | + ->rememberForever("contacts-getByTypes-{$key}", function () use ($types) { |
| 99 | + return $this |
| 100 | + ->with(['contactTypes' => function ($query) use ($types) { |
| 101 | + $query->whereIn('title', $types); |
| 102 | + }]) |
| 103 | + ->orderBy('name') |
| 104 | + ->get(); |
| 105 | + }); |
| 106 | +} |
| 107 | +``` |
| 108 | + |
| 109 | +## Dedication to Quality |
| 110 | +During package development I try as best as possible to embrace good design and |
| 111 | +development practices to try to ensure that this package is as good as it can |
| 112 | +be. My checklist for package development includes: |
| 113 | + |
| 114 | +- ✅ Achieve as close to 100% code coverage as possible using unit tests. |
| 115 | +- ✅ Eliminate any issues identified by SensioLabs Insight and Scrutinizer. |
| 116 | +- ✅ Be fully PSR1, PSR2, and PSR4 compliant. |
| 117 | +- ✅ Include comprehensive documentation in README.md. |
| 118 | +- ✅ Provide an up-to-date CHANGELOG.md which adheres to the format outlined |
| 119 | + at <http://keepachangelog.com>. |
| 120 | +- ✅ Have no PHPMD or PHPCS warnings throughout all code. |
| 121 | + |
| 122 | +## Contributing |
| 123 | +Please observe and respect all aspects of the included Code of Conduct <https://github.com/GeneaLabs/laravel-model-caching/blob/master/CODE_OF_CONDUCT.md>. |
| 124 | + |
| 125 | +### Reporting Issues |
| 126 | +When reporting issues, please fill out the included template as completely as |
| 127 | +possible. Incomplete issues may be ignore or closed if there is not enough |
| 128 | +information included to be actionable. |
| 129 | + |
| 130 | +### Submitting Pull Requests |
| 131 | +Please review the Contribution Guidelines <https://github.com/GeneaLabs/laravel-model-caching/blob/master/CONTRIBUTING.md>. |
| 132 | +Only PRs that meet all criterium will be accepted. |
0 commit comments