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
124124rate, and you don't want every comment submission to invalidate the cache. While
125125I don't necessarily recommend this, you might experiment it's effectiveness.
126126
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) :
128128``` php
129129class MyModel extends Model
130130{
131131 use Cachable;
132132
133- protected $useCacheCooldown = true;
133+ protected $cacheCooldownSeconds = 300; // 5 minutes
134134
135135 // ...
136136}
@@ -139,7 +139,14 @@ class MyModel extends Model
139139After that it can be implemented in queries:
140140``` php
141141(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
143150 ->get();
144151```
145152
Original file line number Diff line number Diff line change @@ -93,7 +93,7 @@ protected function makeCacheTags() : array
9393
9494 public function getModelCacheCooldown (Model $ instance ) : array
9595 {
96- if (! $ instance ->useCacheCooldown ) {
96+ if (! $ instance ->cacheCooldownSeconds ) {
9797 return [null , null , null ];
9898 }
9999
Original file line number Diff line number Diff line change 66
77trait ModelCaching
88{
9- protected $ useCacheCooldown = false ;
9+ protected $ cacheCooldownSeconds = 0 ;
1010
1111 public static function all ($ columns = ['* ' ])
1212 {
@@ -88,8 +88,12 @@ public function scopeDisableCache(EloquentBuilder $query) : EloquentBuilder
8888
8989 public function scopeWithCacheCooldownSeconds (
9090 EloquentBuilder $ query ,
91- int $ seconds
91+ int $ seconds = null
9292 ) : EloquentBuilder {
93+ if (! $ seconds ) {
94+ $ seconds = $ this ->cacheCooldownSeconds ;
95+ }
96+
9397 $ cachePrefix = $ this ->getCachePrefix ();
9498 $ modelClassName = get_class ($ this );
9599 $ cacheKey = "{$ cachePrefix }: {$ modelClassName }-cooldown:seconds " ;
@@ -108,8 +112,8 @@ public function scopeWithCacheCooldownSeconds(
108112 return $ query ;
109113 }
110114
111- public function getUseCacheCooldownAttribute () : bool
115+ public function getcacheCooldownSecondsAttribute () : bool
112116 {
113- return $ this ->useCacheCooldown ;
117+ return $ this ->cacheCooldownSeconds ;
114118 }
115119}
Original file line number Diff line number Diff line change 88class AuthorWithCooldown extends Author
99{
1010 protected $ table = "authors " ;
11- protected $ useCacheCooldown = true ;
11+ protected $ cacheCooldownSeconds = 1 ;
1212}
You can’t perform that action at this time.
0 commit comments