![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/ledger.corals.io/Corals/core/Foundation/Traits/Cache/ |
<?php namespace Corals\Foundation\Traits\Cache; use Corals\Foundation\Classes\Cache\CachedBuilder; use Illuminate\Database\Eloquent\Builder as EloquentBuilder; trait ModelCaching { public static function all($columns = ['*']) { if (config('laravel-model-caching.disabled')) { return parent::all($columns); } $class = get_called_class(); $instance = new $class; $tags = $instance->makeCacheTags(); $key = $instance->makeCacheKey(); return $instance->cache($tags) ->rememberForever($key, function () use ($columns) { return parent::all($columns); }); } public static function bootCachable() { static::deleted(function ($instance) { $instance->checkCooldownAndFlushAfterPersiting($instance); }); static::saved(function ($instance) { $instance->checkCooldownAndFlushAfterPersiting($instance); }); static::pivotAttached(function ($instance) { $instance->checkCooldownAndFlushAfterPersiting($instance); }); static::pivotDetached(function ($instance) { $instance->checkCooldownAndFlushAfterPersiting($instance); }); static::pivotUpdated(function ($instance) { $instance->checkCooldownAndFlushAfterPersiting($instance); }); } public static function destroy($ids) { $class = get_called_class(); $instance = new $class; $instance->flushCache(); return parent::destroy($ids); } public function newEloquentBuilder($query) { if (! $this->isCachable()) { $this->isCachable = true; return new EloquentBuilder($query); } return new CachedBuilder($query); } public function scopeDisableCache(EloquentBuilder $query) : EloquentBuilder { if ($this->isCachable()) { $query = $query->disableModelCaching(); } return $query; } public function scopeWithCacheCooldownSeconds( EloquentBuilder $query, int $seconds ) : EloquentBuilder { $cachePrefix = $this->getCachePrefix(); $modelClassName = get_class($this); $cacheKey = "{$cachePrefix}:{$modelClassName}-cooldown:seconds"; $this->cache() ->rememberForever($cacheKey, function () use ($seconds) { return $seconds; }); $cacheKey = "{$cachePrefix}:{$modelClassName}-cooldown:invalidated-at"; $this->cache() ->rememberForever($cacheKey, function () { return now(); }); return $query; } }