Skip to content

Move the logic into a trait, so it is accessible without extending the provided base model (eg for projects which already have a base model) #65

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
merged 6 commits into from
Feb 17, 2018
Merged
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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,19 @@ mean that the entire cache is cleared each time a model is created, saved,
updated, or deleted.

For ease of maintenance, I would recommend adding a `BaseModel` model that
extends `CachedModel`, from which all your other models are extended. If you
uses `Cachable`, from which all your other models are extended. If you
don't want to do that, simply extend your models directly from `CachedModel`.

Here's an example `BaseModel` class:

```php
<?php namespace App;

use GeneaLabs\LaravelModelCaching\CachedModel;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;

abstract class BaseModel extends CachedModel
abstract class BaseModel
{
use Cachable;
//
}
```
Expand Down
56 changes: 0 additions & 56 deletions src/CachedModel.php
Original file line number Diff line number Diff line change
@@ -1,65 +1,9 @@
<?php namespace GeneaLabs\LaravelModelCaching;

use GeneaLabs\LaravelModelCaching\CachedBuilder as Builder;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Cache\CacheManager;
use Illuminate\Cache\TaggableStore;
use Illuminate\Cache\TaggedCache;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Collection;
use LogicException;

abstract class CachedModel extends Model
{
use Cachable;

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

return new EloquentBuilder($query);
}

return new Builder($query);
}

public static function boot()
{
parent::boot();

$class = get_called_class();
$instance = new $class;

static::created(function () use ($instance) {
$instance->flushCache();
});

static::deleted(function () use ($instance) {
$instance->flushCache();
});

static::saved(function () use ($instance) {
$instance->flushCache();
});

static::updated(function () use ($instance) {
$instance->flushCache();
});
}

public static function all($columns = ['*'])
{
$class = get_called_class();
$instance = new $class;
$tags = [str_slug(get_called_class())];
$key = $instance->makeCacheKey();

return $instance->cache($tags)
->rememberForever($key, function () use ($columns) {
return parent::all($columns);
});
}
}
33 changes: 33 additions & 0 deletions src/Traits/Cachable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use GeneaLabs\LaravelModelCaching\CachedModel;
use Illuminate\Cache\TaggableStore;
use Illuminate\Database\Query\Builder;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use GeneaLabs\LaravelModelCaching\CachedBuilder;

trait Cachable
{
Expand Down Expand Up @@ -60,4 +62,35 @@ protected function makeCacheTags() : array
return (new CacheTags($this->eagerLoad, $this->model))
->make();
}

public static function bootCachable()
{
static::saved(function ($instance) {
$instance->flushCache();
});
}

public static function all($columns = ['*'])
{
$class = get_called_class();
$instance = new $class;
$tags = [str_slug(get_called_class())];
$key = $instance->makeCacheKey();

return $instance->cache($tags)
->rememberForever($key, function () use ($columns) {
return parent::all($columns);
});
}

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

return new EloquentBuilder($query);
}

return new CachedBuilder($query);
}
}