Skip to content

Commit 65046c3

Browse files
committed
Add support for cache parameter on model based tags
1 parent 8d9d624 commit 65046c3

File tree

3 files changed

+67
-16
lines changed

3 files changed

+67
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Enhanced support for many template tags including `exp:channel:form, exp:member:custom_profile_data, exp:member:edit_avatar, exp:member:edit_profile, exp:member:forgot_password_form, exp:member:forgot_username_form, exp:member:login_form, exp:member:logout_form, exp:member:memberlist, exp:member:member_search, exp:member:registration_form`
1212
- Template Generator support for Twig and Blade template engines
1313
- Twig extension to simplify using Vite in a template, e.g. `{{ vite('ee::assets/style.scss') | raw }}`
14+
- Support for `cache` parameter on model based tags (`exp.channel.entries`, `exp.channel.categories`)
1415

1516
### Fixed
1617

src/View/ModelTag.php

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Expressionengine\Coilpack\View;
44

5+
use Expressionengine\Coilpack\Support\Arguments\Argument;
56
use Expressionengine\Coilpack\Support\Arguments\ListArgument;
67
use Expressionengine\Coilpack\Support\Parameter;
78

@@ -42,6 +43,12 @@ public function defineParameters(): array
4243
'description' => 'A pipe separated list of relationships to eager load',
4344
'defaultValue' => null,
4445
]),
46+
new Parameter([
47+
'name' => 'cache',
48+
'type' => 'integer',
49+
'description' => 'Number of seconds to cache results',
50+
'defaultValue' => null,
51+
]),
4552
];
4653
}
4754

@@ -52,12 +59,28 @@ public function getWithArgument($value)
5259

5360
public function run()
5461
{
62+
$cacheKey = null;
63+
64+
if ($this->hasArgument('cache')) {
65+
$cacheKey = $this->getCacheKey();
66+
67+
if (ee()->cache->get($cacheKey) !== false) {
68+
return ee()->cache->get($cacheKey);
69+
}
70+
}
71+
72+
if ($this->hasArgument('with')) {
73+
$this->query->with($this->getArgument('with')->terms->map->value->toArray());
74+
}
75+
5576
if ($this->hasArgument('page') || $this->hasArgument('per_page')) {
56-
return $this->query->paginate(
57-
$this->hasArgument('limit') ? $this->getArgument('limit')->value : $this->getArgument('per_page')->value,
58-
['*'],
59-
'page',
60-
$this->hasArgument('page') ? $this->getArgument('page')->value : null
77+
return $this->cache($cacheKey,
78+
$this->query->paginate(
79+
$this->hasArgument('limit') ? $this->getArgument('limit')->value : $this->getArgument('per_page')->value,
80+
['*'],
81+
'page',
82+
$this->hasArgument('page') ? $this->getArgument('page')->value : null
83+
)
6184
);
6285
}
6386

@@ -69,11 +92,7 @@ public function run()
6992
$this->query->take($this->getArgument('limit')->value);
7093
}
7194

72-
if ($this->hasArgument('with')) {
73-
$this->query->with($this->getArgument('with')->terms->map->value->toArray());
74-
}
75-
76-
return $this->query->get();
95+
return $this->cache($cacheKey, $this->query->get());
7796
}
7897

7998
public function __call($method, $arguments)
@@ -86,4 +105,35 @@ public function __call($method, $arguments)
86105

87106
return $result;
88107
}
108+
109+
protected function cache($key, $result)
110+
{
111+
if (! is_null($key)) {
112+
ee()->cache->save($key, $result, (int) $this->getArgument('cache')->value);
113+
}
114+
115+
return $result;
116+
}
117+
118+
protected function getCacheKey()
119+
{
120+
$class = implode('.', array_slice(explode('\\', static::class), -2, 2));
121+
$prefix = $this->hasArgument('cache_prefix') ? $this->getArgument('cache_prefix')->value : null;
122+
123+
$arguments = $this->getArguments();
124+
unset($arguments['cache'], $arguments['cache_prefix']);
125+
126+
foreach ($arguments as $key => $value) {
127+
if ($value instanceof Argument) {
128+
$arguments[$key] = $value->value;
129+
}
130+
}
131+
132+
return implode(':', array_filter([
133+
'coilpack',
134+
strtolower($class),
135+
$prefix,
136+
md5(json_encode($arguments)),
137+
]));
138+
}
89139
}

src/View/Tags/Channel/Entries.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,19 +281,19 @@ public function run()
281281
$year = $this->getArgument('year')->value ?: date('Y');
282282
$start = [
283283
'month' => $this->hasArgument('month') ? $this->getArgument('month')->value : 1,
284-
'day' => $this->hasArgument('day') ? $this->getArgument('day')->value : 1
284+
'day' => $this->hasArgument('day') ? $this->getArgument('day')->value : 1,
285285
];
286286
$end = [
287287
'month' => $this->hasArgument('month') ? $this->getArgument('month')->value : 12,
288-
'day' => $this->hasArgument('day') ? $this->getArgument('day')->value : null
288+
'day' => $this->hasArgument('day') ? $this->getArgument('day')->value : null,
289289
];
290-
if(is_null($end['day'])) {
290+
if (is_null($end['day'])) {
291291
ee()->load->helper('date');
292292
$end['day'] = \days_in_month($end['month'], $year);
293293
}
294294
$query->whereBetween('entry_date', [
295295
ee()->localize->string_to_timestamp("{$year}-{$start['month']}-{$start['day']} 00:00"),
296-
ee()->localize->string_to_timestamp("{$year}-{$end['month']}-{$end['day']} 23:59")
296+
ee()->localize->string_to_timestamp("{$year}-{$end['month']}-{$end['day']} 23:59"),
297297
]);
298298
});
299299

@@ -347,8 +347,8 @@ public function run()
347347

348348
// Sticky
349349
if (! $this->hasArgument('sticky') || $this->getArgument('sticky')->value == 'yes') {
350-
$this->setArgument('orderby', 'sticky|'.($this->arguments['orderby'] ?? ''));
351-
$this->setArgument('sort', 'desc|'.($this->arguments['sort'] ?? ''));
350+
$this->setArgument('orderby', 'sticky|'.($this->getArgument('orderby')->terms->map->value->implode('|') ?? ''));
351+
$this->setArgument('sort', 'desc|'.($this->getArgument('sort')->terms->map->value->implode('|') ?? ''));
352352
}
353353

354354
if ($this->hasArgument('sticky')) {

0 commit comments

Comments
 (0)