1
- ** DO NOT INSTALL - STILL IN EXPERIMENTAL PHASE**
2
-
3
1
![ pexels-photo-325229] ( https://user-images.githubusercontent.com/1791050/30768358-0df9d0f2-9fbb-11e7-9f10-ad40b83bbf59.jpg )
4
2
5
3
# Model Caching for Laravel
@@ -20,10 +18,9 @@ relationships. This package is the attempt to address those requirements.
20
18
21
19
## Features
22
20
- automatic, self-invalidating relationship caching.
21
+ - automatic, self-invalidating model query caching.
23
22
- automatic use of cache flags for cache providers that support them (will
24
23
flush entire cache for providers that don't).
25
- - provides simple caching methods for use in query methods for models that
26
- take advantage of the automatic cache management features mentioned.
27
24
28
25
## Requirements
29
26
- PHP >= 7.0.0
@@ -35,14 +32,11 @@ memcached). While this is optional, using a non-taggable cache provider will
35
32
mean that the entire cache is cleared each time a model is created, saved,
36
33
updated, or deleted.
37
34
38
- For best implementation results , I would recommend adding a ` BaseModel ` model
39
- from which all your other models are extended. The BaseModel should extend from
40
- ` CachedModel ` .
35
+ For ease of maintenance , I would recommend adding a ` BaseModel ` model that
36
+ extends ` CachedModel ` , from which all your other models are extended. If you
37
+ don't want to do that, simply extend your models directly from ` CachedModel ` .
41
38
42
- ### Automatic Relationship Caching
43
- When writing custom queries in your models, use ` $this->cache() ` instead of
44
- ` cache() ` to automatically tag and cache the queries. These will also be auto-
45
- invalidated.
39
+ Here's an example ` BaseModel ` class:
46
40
47
41
``` php
48
42
<?php namespace App;
@@ -55,61 +49,12 @@ abstract class BaseModel extends CachedModel
55
49
}
56
50
```
57
51
58
- ``` php
59
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
60
- use Illuminate\Support\Collection;
61
-
62
- class Venue extends BaseModel
63
- {
64
- protected $fillable = [
65
- 'name',
66
- ];
67
-
68
- public function address() : BelongsTo
69
- {
70
- return $this->belongsTo(Address::class);
71
- }
72
-
73
- public function getAll() : Collection
74
- {
75
- return $this->cache()
76
- ->rememberForever("venues-getAll", function () {
77
- return $this->orderBy('name')
78
- ->get();
79
- });
80
- }
81
- }
82
- ```
83
-
84
- ### Custom Query Caching
85
- ** ` $this->cache(array $keys) ` **
86
- This method is available in any model that extends ` CachedModel ` , as well
87
- as automatically invalidate them. Pass in respective additional models that are
88
- represented in the query being cached. This is most often necessary when eager-
89
- loading relationships.
90
-
91
- When you create the cache key, be sure to build the key in such a way that it
92
- uniquely represents the query and does not overwrite keys of other queries. The
93
- best way to achieve this is to build the key as follows: `<model slug >-<model
94
- method>-<unique key >` . The ` unique key` portion is only necessary if you pass in
95
- parameters for your query where clauses.
52
+ ** That's all you need to do. All model queries and relationships are now
53
+ cached!**
96
54
97
- ``` php
98
- public function getByTypes(array $types) : Collection
99
- {
100
- $key = implode('-', $types);
101
-
102
- return $this->cache([ContactType::class])
103
- ->rememberForever("contacts-getByTypes-{$key}", function () use ($types) {
104
- return $this
105
- ->with(['contactTypes' => function ($query) use ($types) {
106
- $query->whereIn('title', $types);
107
- }])
108
- ->orderBy('name')
109
- ->get();
110
- });
111
- }
112
- ```
55
+ In testing this has optimized performance on some pages up to 900%! Most often
56
+ you should see somewhere around 100% performance increase. (I will show some
57
+ concrete examples here soon, still working on optimizing things first.)
113
58
114
59
## Commitment to Quality
115
60
During package development I try as best as possible to embrace good design and
0 commit comments