File tree Expand file tree Collapse file tree 4 files changed +20
-9
lines changed Expand file tree Collapse file tree 4 files changed +20
-9
lines changed Original file line number Diff line number Diff line change @@ -124,13 +124,13 @@ For example you might have a busy site where comments are submitted at a high
124
124
rate, and you don't want every comment submission to invalidate the cache. While
125
125
I don't necessarily recommend this, you might experiment it's effectiveness.
126
126
127
- To ise it, it must be enabled in the model:
127
+ To use it, it must be enabled in the model (or base model if you want to use it on multiple or all models) :
128
128
``` php
129
129
class MyModel extends Model
130
130
{
131
131
use Cachable;
132
132
133
- protected $useCacheCooldown = true;
133
+ protected $cacheCooldownSeconds = 300; // 5 minutes
134
134
135
135
// ...
136
136
}
@@ -139,7 +139,14 @@ class MyModel extends Model
139
139
After that it can be implemented in queries:
140
140
``` php
141
141
(new Comment)
142
- ->withCacheCooldownSeconds(30)
142
+ ->withCacheCooldownSeconds(30) // override default cooldown seconds in model
143
+ ->get();
144
+ ```
145
+
146
+ or:
147
+ ``` php
148
+ (new Comment)
149
+ ->withCacheCooldownSeconds() // use default cooldown seconds in model
143
150
->get();
144
151
```
145
152
Original file line number Diff line number Diff line change @@ -93,7 +93,7 @@ protected function makeCacheTags() : array
93
93
94
94
public function getModelCacheCooldown (Model $ instance ) : array
95
95
{
96
- if (! $ instance ->useCacheCooldown ) {
96
+ if (! $ instance ->cacheCooldownSeconds ) {
97
97
return [null , null , null ];
98
98
}
99
99
Original file line number Diff line number Diff line change 6
6
7
7
trait ModelCaching
8
8
{
9
- protected $ useCacheCooldown = false ;
9
+ protected $ cacheCooldownSeconds = 0 ;
10
10
11
11
public static function all ($ columns = ['* ' ])
12
12
{
@@ -88,8 +88,12 @@ public function scopeDisableCache(EloquentBuilder $query) : EloquentBuilder
88
88
89
89
public function scopeWithCacheCooldownSeconds (
90
90
EloquentBuilder $ query ,
91
- int $ seconds
91
+ int $ seconds = null
92
92
) : EloquentBuilder {
93
+ if (! $ seconds ) {
94
+ $ seconds = $ this ->cacheCooldownSeconds ;
95
+ }
96
+
93
97
$ cachePrefix = $ this ->getCachePrefix ();
94
98
$ modelClassName = get_class ($ this );
95
99
$ cacheKey = "{$ cachePrefix }: {$ modelClassName }-cooldown:seconds " ;
@@ -108,8 +112,8 @@ public function scopeWithCacheCooldownSeconds(
108
112
return $ query ;
109
113
}
110
114
111
- public function getUseCacheCooldownAttribute () : bool
115
+ public function getcacheCooldownSecondsAttribute () : bool
112
116
{
113
- return $ this ->useCacheCooldown ;
117
+ return $ this ->cacheCooldownSeconds ;
114
118
}
115
119
}
Original file line number Diff line number Diff line change 8
8
class AuthorWithCooldown extends Author
9
9
{
10
10
protected $ table = "authors " ;
11
- protected $ useCacheCooldown = true ;
11
+ protected $ cacheCooldownSeconds = 1 ;
12
12
}
You can’t perform that action at this time.
0 commit comments