Skip to content

Commit 3d59b36

Browse files
committed
Merge branch 'release/0.2.38'
2 parents a1b5a13 + 2c8a183 commit 3d59b36

File tree

7 files changed

+305
-132
lines changed

7 files changed

+305
-132
lines changed

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [0.2.38] - 24 Feb 2018
8+
### Added
9+
- cache-invalidation-cool-down functionality.
10+
11+
## [0.2.37] - 23 Feb 2018
12+
### Added
13+
- disabling of `->all()` method caching via config flag.
14+
715
## [0.2.36] - 23 Feb 2018
816
### Added
917
- config setting to allow disabling of model-caching.
@@ -15,6 +23,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1523
### Added
1624
- caching for `paginate()`;
1725

26+
## [0.2.34] - 21 Feb 2018
27+
### Added
28+
- implementation tests using redis.
29+
- additional tests for some edge case scenarios.
30+
31+
### Fixed
32+
- cache key prefix functionality.
33+
34+
### Updated
35+
- tests through refactoring and cleaning up.
36+
1837
## [0.2.33] - 19 Feb 2018
1938
### Added
2039
- unit test to make sure `Model::all()` returns a collection when only only

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ environment variable in your `.env` file to the name of a cache store configured
4141
in `config/cache.php` (you can define any custom cache store based on your
4242
specific needs there). For example:
4343
```
44-
MODEL_CACHE_STORE=redis
44+
MODEL_CACHE_STORE=redis2
4545
```
4646

4747
## Usage
@@ -105,7 +105,20 @@ extends `Illuminate\Foundation\Auth\User`. Overriding that would break functiona
105105
Not only that, but it probably isn't a good idea to cache the user model anyway,
106106
since you always want to pull the most up-to-date info on it.
107107

108-
### Optional Disabling Caching of Queries
108+
### Experimental: Cache Cool-down In Specific Models
109+
In some instances, you may want to add a cache invalidation cool-down period.
110+
For example you might have a busy site where comments are submitted at a high
111+
rate, and you don't want every comment submission to invalidate the cache. While
112+
I don't necessarily recommend this, you might experiment it's effectiveness.
113+
114+
It can be implemented like so:
115+
```php
116+
(new Comment)
117+
->withCacheCooldownSeconds(30)
118+
->get();
119+
```
120+
121+
### Disabling Caching of Queries
109122
There are two methods by which model-caching can be disabled:
110123
1. Use `->disableCache()` in a query-by-query instance.
111124
2. Set `MODEL_CACHE_DISABLED=TRUE` in your `.env` file.

src/CacheKey.php

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class CacheKey
1313
protected function getCachePrefix() : string
1414
{
1515
return "genealabs:laravel-model-caching:"
16-
. (config('laravel-model-caching.cache-prefix')
17-
? config('laravel-model-caching.cache-prefix', '') . ":"
16+
. (config("laravel-model-caching.cache-prefix")
17+
? config("laravel-model-caching.cache-prefix", "") . ":"
1818
: "");
1919
}
2020

@@ -29,13 +29,13 @@ public function __construct(
2929
}
3030

3131
public function make(
32-
array $columns = ['*'],
32+
array $columns = ["*"],
3333
$idColumn = null,
34-
string $keyDifferentiator = ''
34+
string $keyDifferentiator = ""
3535
) : string {
3636
$key = $this->getCachePrefix();
3737
$key .= $this->getModelSlug();
38-
$key .= $this->getIdColumn($idColumn ?: '');
38+
$key .= $this->getIdColumn($idColumn ?: "");
3939
$key .= $this->getQueryColumns($columns);
4040
$key .= $this->getWhereClauses();
4141
$key .= $this->getWithModels();
@@ -49,13 +49,13 @@ public function make(
4949

5050
protected function getIdColumn(string $idColumn) : string
5151
{
52-
return $idColumn ? "_{$idColumn}" : '';
52+
return $idColumn ? "_{$idColumn}" : "";
5353
}
5454

5555
protected function getLimitClause() : string
5656
{
5757
if (! $this->query->limit) {
58-
return '';
58+
return "";
5959
}
6060

6161
return "-limit_{$this->query->limit}";
@@ -69,7 +69,7 @@ protected function getModelSlug() : string
6969
protected function getOffsetClause() : string
7070
{
7171
if (! $this->query->offset) {
72-
return '';
72+
return "";
7373
}
7474

7575
return "-offset_{$this->query->offset}";
@@ -81,48 +81,48 @@ protected function getOrderByClauses() : string
8181

8282
return $orders
8383
->reduce(function ($carry, $order) {
84-
if (($order['type'] ?? '') === 'Raw') {
85-
return $carry . '_orderByRaw_' . str_slug($order['sql']);
84+
if (($order["type"] ?? "") === "Raw") {
85+
return $carry . "_orderByRaw_" . str_slug($order["sql"]);
8686
}
8787

88-
return $carry . '_orderBy_' . $order['column'] . '_' . $order['direction'];
88+
return $carry . "_orderBy_" . $order["column"] . "_" . $order["direction"];
8989
})
90-
?: '';
90+
?: "";
9191
}
9292

9393
protected function getQueryColumns(array $columns) : string
9494
{
95-
if ($columns === ['*'] || $columns === []) {
96-
return '';
95+
if ($columns === ["*"] || $columns === []) {
96+
return "";
9797
}
9898

99-
return '_' . implode('_', $columns);
99+
return "_" . implode("_", $columns);
100100
}
101101

102102
protected function getTypeClause($where) : string
103103
{
104-
$type =in_array($where['type'], ['In', 'NotIn', 'Null', 'NotNull', 'between'])
105-
? strtolower($where['type'])
106-
: strtolower($where['operator']);
104+
$type =in_array($where["type"], ["In", "NotIn", "Null", "NotNull", "between"])
105+
? strtolower($where["type"])
106+
: strtolower($where["operator"]);
107107

108-
return str_replace(' ', '_', $type);
108+
return str_replace(" ", "_", $type);
109109
}
110110

111111
protected function getValuesClause(array $where = null) : string
112112
{
113-
if (in_array($where['type'], ['NotNull'])) {
114-
return '';
113+
if (in_array($where["type"], ["NotNull"])) {
114+
return "";
115115
}
116116

117-
$values = is_array(array_get($where, 'values'))
118-
? implode('_', $where['values'])
119-
: '';
117+
$values = is_array(array_get($where, "values"))
118+
? implode("_", $where["values"])
119+
: "";
120120

121-
if (! $values && $this->query->bindings['where'] ?? false) {
122-
$values = implode('_', $this->query->bindings['where']);
121+
if (! $values && $this->query->bindings["where"] ?? false) {
122+
$values = implode("_", $this->query->bindings["where"]);
123123
}
124124

125-
return '_' . $values;
125+
return "_" . $values;
126126
}
127127

128128
protected function getWhereClauses(array $wheres = []) : string
@@ -136,46 +136,46 @@ protected function getWhereClauses(array $wheres = []) : string
136136

137137
return $value;
138138
})
139-
. '';
139+
. "";
140140
}
141141

142142
protected function getNestedClauses(array $where) : string
143143
{
144-
if (! in_array($where['type'], ['Exists', 'Nested', 'NotExists'])) {
145-
return '';
144+
if (! in_array($where["type"], ["Exists", "Nested", "NotExists"])) {
145+
return "";
146146
}
147147

148-
return '_' . strtolower($where['type']) . $this->getWhereClauses($where['query']->wheres);
148+
return "_" . strtolower($where["type"]) . $this->getWhereClauses($where["query"]->wheres);
149149
}
150150

151151
protected function getColumnClauses(array $where) : string
152152
{
153-
if ($where['type'] !== 'Column') {
154-
return '';
153+
if ($where["type"] !== "Column") {
154+
return "";
155155
}
156156

157-
return "_{$where['boolean']}_{$where['first']}_{$where['operator']}_{$where['second']}";
157+
return "_{$where["boolean"]}_{$where["first"]}_{$where["operator"]}_{$where["second"]}";
158158
}
159159

160160
protected function getRawClauses(array $where) : string
161161
{
162-
if ($where['type'] !== 'raw') {
163-
return '';
162+
if ($where["type"] !== "raw") {
163+
return "";
164164
}
165165

166-
return "_{$where['boolean']}_" . str_slug($where['sql']);
166+
return "_{$where["boolean"]}_" . str_slug($where["sql"]);
167167
}
168168

169169
protected function getOtherClauses(array $where, string $carry = null) : string
170170
{
171-
if (in_array($where['type'], ['Exists', 'Nested', 'NotExists', 'raw', 'Column'])) {
172-
return '';
171+
if (in_array($where["type"], ["Exists", "Nested", "NotExists", "raw", "Column"])) {
172+
return "";
173173
}
174174

175175
$value = $this->getTypeClause($where);
176176
$value .= $this->getValuesClause($where);
177177

178-
return "{$carry}-{$where['column']}_{$value}";
178+
return "{$carry}-{$where["column"]}_{$value}";
179179
}
180180

181181
protected function getWheres(array $wheres) : Collection
@@ -194,9 +194,9 @@ protected function getWithModels() : string
194194
$eagerLoads = collect($this->eagerLoad);
195195

196196
if ($eagerLoads->isEmpty()) {
197-
return '';
197+
return "";
198198
}
199199

200-
return '-' . implode('-', $eagerLoads->keys()->toArray());
200+
return "-" . implode("-", $eagerLoads->keys()->toArray());
201201
}
202202
}

0 commit comments

Comments
 (0)