Skip to content

Commit

Permalink
Merge pull request #4 from ARCANEDEV/feature-events
Browse files Browse the repository at this point in the history
Adding caching
  • Loading branch information
arcanedev-maroc authored Mar 5, 2017
2 parents 8468424 + 7809f1d commit c3c9456
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
7 changes: 5 additions & 2 deletions config/laravel-seo.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
| Tables
| -----------------------------------------------------------------
*/
'metas' => [
'metas' => [
'table' => 'metas',
'model' => \Arcanedev\LaravelSeo\Models\Meta::class,
],
Expand Down Expand Up @@ -51,7 +51,10 @@
'class' => \Arcanedev\LaravelSeo\Redirectors\EloquentRedirector::class,

'options' => [
//
'cache' => [
'key' => 'seo-redirects',
'duration' => 30, // minutes
],
],
],
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public function up()
$table->text('new_url');
$table->integer('status');
$table->timestamps();

$table->unique('old_url');
});
}
}
30 changes: 30 additions & 0 deletions src/Models/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ public function __construct(array $attributes = [])
$this->setTable(Seo::getConfig('redirects.table', 'redirects'));
}

/**
* The "booting" method of the model.
*/
public static function boot()
{
parent::boot();

static::saved(function() {
static::clearCache();
});

static::deleted(function() {
static::clearCache();
});
}

/* -----------------------------------------------------------------
| Getters & Setters
| -----------------------------------------------------------------
Expand Down Expand Up @@ -95,4 +111,18 @@ public static function createOne($oldUrl, $newUrl, $status = Response::HTTP_MOVE

return $redirect;
}

/* -----------------------------------------------------------------
| Other Methods
| -----------------------------------------------------------------
*/
/**
* Clear the cached data.
*/
protected static function clearCache()
{
$key = Seo::getConfig('redirector.drivers.eloquent.options.cache.key', 'seo-redirects');

cache()->forget($key);
}
}
18 changes: 14 additions & 4 deletions src/Redirectors/EloquentRedirector.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,26 @@ class EloquentRedirector extends AbstractRedirector implements Redirector
*/
public function getRedirectedUrls()
{
/** @var \Illuminate\Database\Eloquent\Collection $redirects */
$redirects = $this->getRedirectModel()->get();

return $redirects->keyBy('old_url')
return $this->getCachedRedirects()
->keyBy('old_url')
->transform(function (Redirect $item) {
return [$item->new_url, $item->status];
})
->toArray();
}

/**
* Get the cached redirection urls.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
protected function getCachedRedirects()
{
return cache()->remember($this->getOption('cache.key'), $this->getOption('cache.duration'), function () {
return $this->getRedirectModel()->get();
});
}

/**
* Get the redirect model.
*
Expand Down

0 comments on commit c3c9456

Please sign in to comment.