Skip to content

Commit eeb78e0

Browse files
committed
Change useCacheCooldown to cacheCooldownSeconds
1 parent ad40e49 commit eeb78e0

File tree

4 files changed

+20
-9
lines changed

4 files changed

+20
-9
lines changed

README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ For example you might have a busy site where comments are submitted at a high
124124
rate, and you don't want every comment submission to invalidate the cache. While
125125
I 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
129129
class 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
139139
After 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

src/Traits/Caching.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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

src/Traits/ModelCaching.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
trait 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
}

tests/Fixtures/AuthorWithCooldown.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
class AuthorWithCooldown extends Author
99
{
1010
protected $table = "authors";
11-
protected $useCacheCooldown = true;
11+
protected $cacheCooldownSeconds = 1;
1212
}

0 commit comments

Comments
 (0)