Skip to content

Commit

Permalink
Merge pull request #76 from andrewmclagan/feature/without-session-driver
Browse files Browse the repository at this point in the history
Removing session driver dependancy, using cache
  • Loading branch information
mikebronner committed Feb 18, 2018
2 parents 5629be0 + 32d5fed commit 46643a0
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/Traits/Cachable.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use GeneaLabs\LaravelModelCaching\CacheKey;
use GeneaLabs\LaravelModelCaching\CacheTags;
use GeneaLabs\LaravelModelCaching\CachedModel;
use Illuminate\Support\Facades\Cache;
use Illuminate\Cache\TaggableStore;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
Expand All @@ -12,6 +13,8 @@ trait Cachable
{
protected $isCachable = true;

protected static $isCachableKey = 'genealabs:model-caching:is-disabled';

protected function cache(array $tags = [])
{
$cache = cache();
Expand All @@ -33,7 +36,8 @@ protected function cache(array $tags = [])

public function disableCache()
{
session(['genealabs-laravel-model-caching-is-disabled' => true]);
Cache::forever(static::$isCachableKey, true);

$this->isCachable = false;

return $this;
Expand Down Expand Up @@ -78,9 +82,9 @@ public static function bootCachable()

public static function all($columns = ['*'])
{
if (session('genealabs-laravel-model-caching-is-disabled')) {
if (Cache::get(static::$isCachableKey)) {
return parent::all($columns);
}
}

$class = get_called_class();
$instance = new $class;
Expand All @@ -95,8 +99,8 @@ public static function all($columns = ['*'])

public function newEloquentBuilder($query)
{
if (session('genealabs-laravel-model-caching-is-disabled')) {
session()->forget('genealabs-laravel-model-caching-is-disabled');
if (Cache::get(static::$isCachableKey)) {
Cache::forget(static::$isCachableKey);

return new EloquentBuilder($query);
}
Expand Down

0 comments on commit 46643a0

Please sign in to comment.