Skip to content

dont cache if model::$isCachable == false #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/QueryOrModelCaller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace GeneaLabs\LaravelModelCaching;

use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;

class QueryOrModelCaller
{
/**
* @var CachedBuilder
*/
protected $query;
/**
* @var Model
*/
protected $model;
/**
* @var bool
*/
protected $disableInConfig;

/**
* QueryOrModelCaller constructor.
*
* @param EloquentBuilder $query
* @param Model $model
* @param bool $disableInConfig
*/
public function __construct(EloquentBuilder $query, Model $model, bool $disableInConfig)
{
$this->query = $query;
$this->model = $model;
$this->disableInConfig = $disableInConfig;
}

/**
* @param $name
* @param $arguments
*
* @return mixed
*/
public function __call($name, $arguments)
{
if ($name == 'all')
$result = call_user_func_array([$this->model, 'all'], $arguments);
else
$result = call_user_func_array([$this->query, $name], $arguments);

config()->set('laravel-model-caching.disabled', $this->disableInConfig);

return $result;
}
}
15 changes: 10 additions & 5 deletions src/Traits/ModelCaching.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
<?php namespace GeneaLabs\LaravelModelCaching\Traits;

use GeneaLabs\LaravelModelCaching\CachedBuilder;
use GeneaLabs\LaravelModelCaching\QueryOrModelCaller;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;

trait ModelCaching
{
public static function all($columns = ['*'])
{
if (config('laravel-model-caching.disabled')) {
$class = get_called_class();
$instance = new $class;

if (config('laravel-model-caching.disabled') || !$instance->isCachable) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that isCachable can never be set for the all() method, since setting isCachable is set through a query scope, which returns a Builder object, which doesn't have an all method. Consider that the following code throws an exception:

$model->disableCache()->all();

Unfortunately the only way to disable caching on all() queries is to turn it off in config at this point, or change to get(). Perhaps you have some thoughts on how we can disable the cache on the model? In the past, I've had a method on the model, instead of a scope, and that had more problems than it solved, so I moved to scope.

return parent::all($columns);
}

$class = get_called_class();
$instance = new $class;
$tags = [str_slug(get_called_class())];
$key = $instance->makeCacheKey();

Expand Down Expand Up @@ -52,13 +54,16 @@ public function newEloquentBuilder($query)
return new CachedBuilder($query);
}

public function scopeDisableCache(EloquentBuilder $query) : EloquentBuilder
public function scopeDisableCache(EloquentBuilder $query) : QueryOrModelCaller
{
$disabledInConfig = config('laravel-model-caching.disabled');

if ($this->isCachable()) {
config()->set('laravel-model-caching.disabled', true);
$query = $query->disableModelCaching();
}

return $query;
return new QueryOrModelCaller($query, $this, $disabledInConfig);
}

public function scopeWithCacheCooldownSeconds(
Expand Down