首先我的網站目前使用了這些元件
1 2 3 4 5 6 7 8 | "require" : {
"symfony/http-foundation" : "^3.1" ,
"symfony/routing" : "^3.1" ,
"symfony/http-kernel" : "^3.1" ,
"symfony/event-dispatcher" : "^3.1" ,
"pimple/pimple" : "~3.0" ,
"illuminate/database" : "^5.3"
},
|
登入後複製
登入後複製
因為我用的是symfony的event-dispatcher元件,而沒有用laravel的events元件,
所以Eloquent ORM服務初始化的時候這個自帶的設定事件監聽的功能並不能用
1 2 3 | use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$capsule ->setEventDispatcher( new Dispatcher( new Container));
|
登入後複製
登入後複製
而我又不想使用兩個功能重複的元件,所以沒法監聽到Eloquent ORM的事件然後做快取。
我想實現的主要是用memcache緩存Eloquent ORM查詢事件的數據,這一步該怎麼做呢...
回复內容:
首先我的網站目前使用了這些組件
1 2 3 4 5 6 7 8 | "require" : {
"symfony/http-foundation" : "^3.1" ,
"symfony/routing" : "^3.1" ,
"symfony/http-kernel" : "^3.1" ,
"symfony/event-dispatcher" : "^3.1" ,
"pimple/pimple" : "~3.0" ,
"illuminate/database" : "^5.3"
},
|
登入後複製
登入後複製
因為我用的是symfony的event-dispatcher元件,而沒有用laravel的events元件,
所以Eloquent ORM服務初始化的時候這個自帶的設定事件監聽的功能並不能用
1 2 3 | use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$capsule ->setEventDispatcher( new Dispatcher( new Container));
|
登入後複製
登入後複製
而我又不想使用兩個功能重複的元件,所以沒法監聽到Eloquent ORM的事件然後做快取。
我想實現的主要是用memcache快取Eloquent ORM查詢事件的數據,這一步該怎麼做呢...
快取查詢的數據建議使用 remember
方法。
1 2 3 | $value = Cache::remember( 'users' , $minutes , function () {
return DB::table( 'users' )->get();
});
|
登入後複製
有快取就直接傳回快取裡的數據,否則從資料庫查詢並設定快取後返回資料。
至於你說的替換了event dispatcher,怎麼監聽model事件,可以在model或者基類model裡寫, 例如:
1 2 3 4 5 6 7 8 | protected static function boot()
{
parent::boot();
static ::created( function ( $model ) {
});
}
|
登入後複製
每個事件都有其對應的靜態方法:saving
atingsaved
upupd
updated
creating
created
deleting
deleted
del 或.cn)!