How does Eloquent ORM listen to its events and then use memcache to cache the data?

PHP中文网
Release: 2016-09-03 00:14:12
Original
1222 people have browsed it


First of all, my website currently uses these components

"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"
    },
Copy after login
Copy after login

Because I use symfony's event-dispatcher component instead of laravel's events component,
so this comes with the setting event when the Eloquent ORM service is initialized The monitoring function does not work

use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$capsule->setEventDispatcher(new Dispatcher(new Container));
Copy after login
Copy after login

and I don’t want to use two components with duplicate functions, so I cannot monitor the Eloquent ORM events and cache them.
What I want to achieve is mainly to use memcache to cache the data of Eloquent ORM query events. How to do this step...

Reply content:

First of all, my website currently uses these components

"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"
    },
Copy after login
Copy after login

Because I use Symfony's event-dispatcher component does not use Laravel's events component. So when the Eloquent ORM service is initialized, the built-in event monitoring function cannot be used.

use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
$capsule->setEventDispatcher(new Dispatcher(new Container));
Copy after login
Copy after login
And I don't want to use two components with duplicate functions. So there is no way to listen to Eloquent ORM events and cache them.

What I want to achieve is to use memcache to cache the data of Eloquent ORM query events. How to do this step...


It is recommended to use the

remember method to cache query data.

$value = Cache::remember('users', $minutes, function() {
    return DB::table('users')->get();
});
Copy after login
If there is a cache, the data in the cache will be returned directly. Otherwise, the data will be returned after querying the database and setting the cache.

As for what you said about replacing the event dispatcher, how to monitor model events can be written in the model or base class model, for example:

protected static function boot()
{
    parent::boot();
    
    static::created(function ($model) {
        // cache model
    });
}
Copy after login
Each event has its corresponding static method:

saving saved updating updated creating created deleting deleted

The above is how Eloquent ORM listens to its events and then uses memcache to cache the data. For more related content, please pay attention to the PHP Chinese website (www.php .cn)!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!