|
9 | 9 |
|
10 | 10 | 
|
11 | 11 |
|
| 12 | +Documentation has moved to https://genealabs.com/docs/laravel-model-caching/. |
| 13 | + |
12 | 14 | ## Supporting This Package
|
13 | 15 | This is an MIT-licensed open source project with its ongoing development made possible by the support of the community. If you'd like to support this, and our other packages, please consider [becoming a backer or sponsor on Patreon](https://www.patreon.com/mikebronner).
|
14 | 16 |
|
15 |
| -## Impetus |
16 |
| -I created this package in response to a client project that had complex, nested |
17 |
| -forms with many `<select>`'s that resulted in over 700 database queries on one |
18 |
| -page. I needed a package that abstracted the caching process out of the model |
19 |
| -for me, and one that would let me cache custom queries, as well as cache model |
20 |
| -relationships. This package is an attempt to address those requirements. |
21 |
| - |
22 |
| -## Features |
23 |
| -- automatic, self-invalidating relationship (only eager-loading) caching. |
24 |
| -- automatic, self-invalidating model query caching. |
25 |
| -- automatic use of cache tags for cache providers that support them (will |
26 |
| - flush entire cache for providers that don't). |
27 |
| - |
28 |
| -## Requirements |
29 |
| -- PHP >= 7.1.3 |
30 |
| -- Laravel 5.8 |
31 |
| - ```diff |
32 |
| - - Please note that prior Laravel versions are not supported and the package |
33 |
| - - versions that are compatible with prior versions of Laravel contain bugs. |
34 |
| - - Please only use with the versions of Laravel noted above to be compatible. |
35 |
| - ``` |
36 |
| - |
37 |
| -### Possible Conflicting Packages |
38 |
| -Any packages that also override `newEloquentModel()` from the `Model` class will |
39 |
| -likely conflict with this package. So far these may include the following: |
40 |
| -- [grimzy/laravel-mysql-spatial](https://github.com/grimzy/laravel-mysql-spatial) |
41 |
| -- [fico7489/laravel-pivot](https://github.com/fico7489/laravel-pivot) |
42 |
| - This package conflicts with Laravel Telescope. We have forked our own solution |
43 |
| - for pivot events that replaces this package. If you previously had this package |
44 |
| - installed, we recommend uninstalling it to avoid conflicts. |
45 |
| -- [chelout/laravel-relationship-events](https://github.com/chelout/laravel-relationship-events) |
46 |
| - |
47 |
| -### Things That Don't Work Currently |
48 |
| -The following items currently do no work with this package: |
49 |
| -```diff |
50 |
| -- caching of lazy-loaded relationships, see #127. Currently lazy-loaded belongs-to relationships are cached. Caching of other relationships is in the works. |
51 |
| -- using select() clauses in Eloquent queries, see #238 (work-around discussed in the issue) |
52 |
| -``` |
53 |
| - |
54 |
| -[](https://vimeo.com/256318402) |
55 |
| - |
56 |
| -## Installation |
57 |
| -Be sure to not require a specific version of this package when requiring it: |
58 |
| -``` |
59 |
| -composer require genealabs/laravel-model-caching:* |
60 |
| -``` |
61 |
| - |
62 |
| -## Upgrade Notes |
63 |
| -### 0.5.0 |
64 |
| -The following implementations have changed (see the respective sections below): |
65 |
| -- model-specific cache prefix |
66 |
| - |
67 |
| -## Configuration |
68 |
| -### Recommended (Optional) Custom Cache Store |
69 |
| -If you would like to use a different cache store than the default one used by |
70 |
| -your Laravel application, you may do so by setting the `MODEL_CACHE_STORE` |
71 |
| -environment variable in your `.env` file to the name of a cache store configured |
72 |
| -in `config/cache.php` (you can define any custom cache store based on your |
73 |
| -specific needs there). For example: |
74 |
| -``` |
75 |
| -MODEL_CACHE_STORE=redis2 |
76 |
| -``` |
77 |
| - |
78 |
| -## Usage |
79 |
| -For best performance a taggable cache provider is recommended (redis, |
80 |
| -memcached). While this is optional, using a non-taggable cache provider will |
81 |
| -mean that the entire cache is cleared each time a model is created, saved, |
82 |
| -updated, or deleted. |
83 |
| - |
84 |
| -For ease of maintenance, I would recommend adding a `BaseModel` model that |
85 |
| -uses `Cachable`, from which all your other models are extended. If you |
86 |
| -don't want to do that, simply extend your models directly from `CachedModel`. |
87 |
| - |
88 |
| -Here's an example `BaseModel` class: |
89 |
| - |
90 |
| -```php |
91 |
| -<?php namespace App; |
92 |
| - |
93 |
| -use GeneaLabs\LaravelModelCaching\Traits\Cachable; |
94 |
| - |
95 |
| -abstract class BaseModel |
96 |
| -{ |
97 |
| - use Cachable; |
98 |
| - // |
99 |
| -} |
100 |
| -``` |
101 |
| - |
102 |
| -### Multiple Database Connections |
103 |
| -__Thanks to @dtvmedia for suggestion this feature. This is actually a more robust |
104 |
| -solution than cache-prefixes.__ |
105 |
| - |
106 |
| -Keeping keys separate for multiple database connections is automatically handled. |
107 |
| -This is especially important for multi-tenant applications, and of course any |
108 |
| -application using multiple database connections. |
109 |
| - |
110 |
| -### Optional Cache Key Prefix |
111 |
| -Thanks to @lucian-dragomir for suggesting this feature! You can use cache key |
112 |
| -prefixing to keep cache entries separate for multi-tenant applications. For this |
113 |
| -it is recommended to add the Cachable trait to a base model, then set the cache |
114 |
| -key prefix config value there. |
115 |
| - |
116 |
| -Here's is an example: |
117 |
| -```php |
118 |
| -<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures; |
119 |
| - |
120 |
| -use GeneaLabs\LaravelModelCaching\Traits\Cachable; |
121 |
| -use Illuminate\Database\Eloquent\Model; |
122 |
| -use Illuminate\Database\Eloquent\Relations\BelongsTo; |
123 |
| -use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
124 |
| - |
125 |
| -class BaseModel extends Model |
126 |
| -{ |
127 |
| - use Cachable; |
128 |
| - |
129 |
| - protected $cachePrefix = "test-prefix"; |
130 |
| -} |
131 |
| -``` |
132 |
| - |
133 |
| -The cache prefix can also be set in the configuration to prefix all cached |
134 |
| -models across the board: |
135 |
| -```php |
136 |
| - 'cache-prefix' => 'test-prefix', |
137 |
| -``` |
138 |
| - |
139 |
| -### Exception: User Model |
140 |
| -I would not recommend caching the user model, as it is a special case, since it |
141 |
| -extends `Illuminate\Foundation\Auth\User`. Overriding that would break functionality. |
142 |
| -Not only that, but it probably isn't a good idea to cache the user model anyway, |
143 |
| -since you always want to pull the most up-to-date info on it. |
144 |
| - |
145 |
| -### Experimental: Cache Cool-down In Specific Models |
146 |
| -In some instances, you may want to add a cache invalidation cool-down period. |
147 |
| -For example you might have a busy site where comments are submitted at a high |
148 |
| -rate, and you don't want every comment submission to invalidate the cache. While |
149 |
| -I don't necessarily recommend this, you might experiment it's effectiveness. |
150 |
| - |
151 |
| -To use it, it must be enabled in the model (or base model if you want to use it on multiple or all models): |
152 |
| -```php |
153 |
| -class MyModel extends Model |
154 |
| -{ |
155 |
| - use Cachable; |
156 |
| - |
157 |
| - protected $cacheCooldownSeconds = 300; // 5 minutes |
158 |
| - |
159 |
| - // ... |
160 |
| -} |
161 |
| -``` |
162 |
| - |
163 |
| -After that it can be implemented in queries: |
164 |
| -```php |
165 |
| -(new Comment) |
166 |
| - ->withCacheCooldownSeconds(30) // override default cooldown seconds in model |
167 |
| - ->get(); |
168 |
| -``` |
169 |
| - |
170 |
| -or: |
171 |
| -```php |
172 |
| -(new Comment) |
173 |
| - ->withCacheCooldownSeconds() // use default cooldown seconds in model |
174 |
| - ->get(); |
175 |
| -``` |
176 |
| - |
177 |
| -### Disabling Caching of Queries |
178 |
| -There are two methods by which model-caching can be disabled: |
179 |
| -1. Use `->disableCache()` in a query-by-query instance. |
180 |
| -2. Set `MODEL_CACHE_DISABLED=TRUE` in your `.env` file. |
181 |
| -3. If you only need to disable the cache for a block of code, or for non- |
182 |
| - eloquent queries, this is probably the better option: |
183 |
| - ```php |
184 |
| - $result = app("model-cache")->runDisabled(function () { |
185 |
| - return (new MyModel)->get(); // or any other stuff you need to run with model-caching disabled |
186 |
| - }); |
187 |
| - ``` |
188 |
| - |
189 |
| -**Recommendation: use option #1 in all your seeder queries to avoid pulling in |
190 |
| -cached information when reseeding multiple times.** |
191 |
| -You can disable a given query by using `disableCache()` anywhere in the query chain. For example: |
192 |
| -```php |
193 |
| -$results = $myModel->disableCache()->where('field', $value)->get(); |
194 |
| -``` |
195 |
| - |
196 |
| -### Manual Flushing of Specific Model |
197 |
| -You can flush the cache of a specific model using the following artisan command: |
198 |
| -```sh |
199 |
| -php artisan modelCache:clear --model=App\Model |
200 |
| -``` |
201 |
| - |
202 |
| -This comes in handy when manually making updates to the database. You could also |
203 |
| -trigger this after making updates to the database from sources outside your |
204 |
| -Laravel app. |
205 |
| - |
206 |
| -## Summary |
207 |
| -**That's all you need to do. All model queries and relationships are now |
208 |
| -cached!** |
209 |
| - |
210 |
| -In testing this has optimized performance on some pages up to 900%! Most often |
211 |
| -you should see somewhere around 100% performance increase. |
212 |
| - |
213 |
| -## Commitment to Quality |
214 |
| -During package development I try as best as possible to embrace good design and development practices, to help ensure that this package is as good as it can |
215 |
| -be. My checklist for package development includes: |
216 |
| - |
217 |
| -- ✅ Achieve as close to 100% code coverage as possible using unit tests. |
218 |
| -- ✅ Eliminate any issues identified by SensioLabs Insight and Scrutinizer. |
219 |
| -- ✅ Be fully PSR1, PSR2, and PSR4 compliant. |
220 |
| -- ✅ Include comprehensive documentation in README.md. |
221 |
| -- ✅ Provide an up-to-date CHANGELOG.md which adheres to the format outlined |
222 |
| - at <http://keepachangelog.com>. |
223 |
| -- ✅ Have no PHPMD or PHPCS warnings throughout all code. |
224 |
| - |
225 |
| -## Contributing |
226 |
| -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>. |
227 |
| - |
228 |
| -### Reporting Issues |
229 |
| -When reporting issues, please fill out the included template as completely as |
230 |
| -possible. Incomplete issues may be ignored or closed if there is not enough |
231 |
| -information included to be actionable. |
232 |
| - |
233 |
| -### Submitting Pull Requests |
234 |
| -Please review the Contribution Guidelines <https://github.com/GeneaLabs/laravel-model-caching/blob/master/CONTRIBUTING.md>. |
235 |
| -Only PRs that meet all criterium will be accepted. |
236 |
| - |
237 | 17 | ## If you ❤️ open-source software, give the repos you use a ⭐️.
|
238 | 18 | We have included the awesome `symfony/thanks` composer package as a dev
|
239 | 19 | dependency. Let your OS package maintainers know you appreciate them by starring
|
|
0 commit comments