|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Anorgan\LaravelCache; |
|
4
|
|
|
|
|
5
|
|
|
use AlternativeLaravelCache\Core\AlternativeCacheStore; |
|
6
|
|
|
use AlternativeLaravelCache\Provider\AlternativeCacheStoresServiceProvider; |
|
7
|
|
|
use Illuminate\Cache\CacheManager; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
9
|
|
|
use Illuminate\Support\Facades\Event; |
|
10
|
|
|
use Illuminate\Support\ServiceProvider; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class LaravelCacheServiceProvider |
|
14
|
|
|
* @package Anorgan\LaravelCache |
|
15
|
|
|
*/ |
|
16
|
|
|
class LaravelCacheServiceProvider extends ServiceProvider |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Bootstrap the application events. |
|
20
|
|
|
*/ |
|
21
|
|
|
public function boot() |
|
22
|
|
|
{ |
|
23
|
|
|
$this->publishes([ |
|
24
|
|
|
__DIR__.'/../config/laravel-cache.php' => config_path('laravel-cache.php'), |
|
25
|
|
|
], 'config'); |
|
26
|
|
|
|
|
27
|
|
|
$this->app->bind(AlternativeCacheStore::class, function () { |
|
28
|
|
|
return $this->app->make(CacheManager::class)->store()->getStore(); |
|
29
|
|
|
}); |
|
30
|
|
|
|
|
31
|
|
|
Event::listen(['eloquent.saved: *', 'eloquent.deleted: *', 'eloquent.restored: *'], function (Model $model) { |
|
32
|
|
|
/** @var CacheInvalidator $cacheInvalidator */ |
|
33
|
|
|
$cacheInvalidator = $this->app->make(CacheInvalidator::class); |
|
34
|
|
|
$cacheInvalidator->invalidateByModel($model); |
|
35
|
|
|
|
|
36
|
|
|
$invalidateRules = config('laravel-cache.invalidate'); |
|
37
|
|
|
if (isset($invalidateRules[get_class($model)])) { |
|
38
|
|
|
$cacheInvalidator->invalidateByKeys($invalidateRules[get_class($model)]); |
|
39
|
|
|
} |
|
40
|
|
|
}); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function register() |
|
44
|
|
|
{ |
|
45
|
|
|
$this->app->register(AlternativeCacheStoresServiceProvider::class); |
|
46
|
|
|
$this->mergeConfigFrom(__DIR__.'/../config/laravel-cache.php', 'laravel-cache'); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|