Skip to content

Commit

Permalink
Add support for cache parameter on model based tags
Browse files Browse the repository at this point in the history
  • Loading branch information
bryannielsen committed Dec 3, 2024
1 parent 8d9d624 commit 65046c3
Showing 3 changed files with 67 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@
- 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`
- Template Generator support for Twig and Blade template engines
- Twig extension to simplify using Vite in a template, e.g. `{{ vite('ee::assets/style.scss') | raw }}`
- Support for `cache` parameter on model based tags (`exp.channel.entries`, `exp.channel.categories`)

### Fixed

70 changes: 60 additions & 10 deletions src/View/ModelTag.php
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

namespace Expressionengine\Coilpack\View;

use Expressionengine\Coilpack\Support\Arguments\Argument;
use Expressionengine\Coilpack\Support\Arguments\ListArgument;
use Expressionengine\Coilpack\Support\Parameter;

@@ -42,6 +43,12 @@ public function defineParameters(): array
'description' => 'A pipe separated list of relationships to eager load',
'defaultValue' => null,
]),
new Parameter([
'name' => 'cache',
'type' => 'integer',
'description' => 'Number of seconds to cache results',
'defaultValue' => null,
]),
];
}

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

public function run()
{
$cacheKey = null;

if ($this->hasArgument('cache')) {
$cacheKey = $this->getCacheKey();

if (ee()->cache->get($cacheKey) !== false) {
return ee()->cache->get($cacheKey);
}
}

if ($this->hasArgument('with')) {
$this->query->with($this->getArgument('with')->terms->map->value->toArray());
}

if ($this->hasArgument('page') || $this->hasArgument('per_page')) {
return $this->query->paginate(
$this->hasArgument('limit') ? $this->getArgument('limit')->value : $this->getArgument('per_page')->value,
['*'],
'page',
$this->hasArgument('page') ? $this->getArgument('page')->value : null
return $this->cache($cacheKey,
$this->query->paginate(
$this->hasArgument('limit') ? $this->getArgument('limit')->value : $this->getArgument('per_page')->value,
['*'],
'page',
$this->hasArgument('page') ? $this->getArgument('page')->value : null
)
);
}

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

if ($this->hasArgument('with')) {
$this->query->with($this->getArgument('with')->terms->map->value->toArray());
}

return $this->query->get();
return $this->cache($cacheKey, $this->query->get());
}

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

return $result;
}

protected function cache($key, $result)
{
if (! is_null($key)) {
ee()->cache->save($key, $result, (int) $this->getArgument('cache')->value);
}

return $result;
}

protected function getCacheKey()
{
$class = implode('.', array_slice(explode('\\', static::class), -2, 2));
$prefix = $this->hasArgument('cache_prefix') ? $this->getArgument('cache_prefix')->value : null;

$arguments = $this->getArguments();
unset($arguments['cache'], $arguments['cache_prefix']);

foreach ($arguments as $key => $value) {
if ($value instanceof Argument) {
$arguments[$key] = $value->value;
}
}

return implode(':', array_filter([
'coilpack',
strtolower($class),
$prefix,
md5(json_encode($arguments)),
]));
}
}
12 changes: 6 additions & 6 deletions src/View/Tags/Channel/Entries.php
Original file line number Diff line number Diff line change
@@ -281,19 +281,19 @@ public function run()
$year = $this->getArgument('year')->value ?: date('Y');
$start = [
'month' => $this->hasArgument('month') ? $this->getArgument('month')->value : 1,
'day' => $this->hasArgument('day') ? $this->getArgument('day')->value : 1
'day' => $this->hasArgument('day') ? $this->getArgument('day')->value : 1,
];
$end = [
'month' => $this->hasArgument('month') ? $this->getArgument('month')->value : 12,
'day' => $this->hasArgument('day') ? $this->getArgument('day')->value : null
'day' => $this->hasArgument('day') ? $this->getArgument('day')->value : null,
];
if(is_null($end['day'])) {
if (is_null($end['day'])) {
ee()->load->helper('date');
$end['day'] = \days_in_month($end['month'], $year);
}
$query->whereBetween('entry_date', [
ee()->localize->string_to_timestamp("{$year}-{$start['month']}-{$start['day']} 00:00"),
ee()->localize->string_to_timestamp("{$year}-{$end['month']}-{$end['day']} 23:59")
ee()->localize->string_to_timestamp("{$year}-{$end['month']}-{$end['day']} 23:59"),
]);
});

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

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

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

0 comments on commit 65046c3

Please sign in to comment.