Skip to content

Removing session driver dependancy, using cache #76

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

Merged
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
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