Closed
Description
Hi Mike,
First, thank you for the great package, really appreciate your work!
I came across this issue when attempting to hook into the model's retrieved event.
The retrieved event only fires if the result hasn't been cached. Once the result is in cache, it no longer fires.
To work around the issue I had to do the following:
- Replaced newEloquentBuilder in base model to return extended GeneaLabs\LaravelModelCaching\CachedBuilder if model is cachable.
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use App\Database\Eloquent\Builder\OverrideCachedBuilder;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
abstract class BaseModel extends Model
{
use Cachable;
public function newEloquentBuilder($query)
{
if (! $this->isCachable()) {
$this->isCachable = false;
return new EloquentBuilder($query);
}
return new OverrideCachedBuilder($query); // call custom extended CachedBuilder
}
}
- Override CachedBuilder cachedValue method with a modified version that fires the retrieved model event.
namespace App\Database\Eloquent\Builder;
use GeneaLabs\LaravelModelCaching\CachedBuilder;
class OverrideCachedBuilder extends CachedBuilder
{
public function cachedValue(array $arguments, string $cacheKey)
{
$method = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['function'];
$cacheTags = $this->makeCacheTags();
$hashedCacheKey = sha1($cacheKey);
$result = $this->retrieveCachedValue(
$arguments,
$cacheKey,
$cacheTags,
$hashedCacheKey,
$method
);
// start - my hack to fire model retrieved event
if ($resultValue = data_get($result, 'value')) {
try {
if (is_subclass_of($resultValue, 'Illuminate\Database\Eloquent\Model')) {
$resultValue->fireModelEvent('retrieved', false);
}
} catch (\Throwable $th) {
//throw $th;
}
}
// end - my hack to fire model retrieved event
return $this->preventHashCollision(
$result,
$arguments,
$cacheKey,
$cacheTags,
$hashedCacheKey,
$method
);
}
}
My solution is not pretty, but works. I had troubles doing it other ways because of the debug_backtrace in the cacheValue event.
If there's a way to improve this solution, and/or get it into core, that would be amazing.
Kind Regards,
Paul
Originally posted by @paularmstrong80 in #171 (comment)