In Laravel, how can I add items to array in contro

2019-09-11 04:08发布

问题:

I am creating an analytics storage process (using elastic search) where I need to add the items to be stored in elastic from my controller. But then I want to wait until after the response has been sent to the user to actually do the processing. I don't want the user to have to wait for this process to complete before getting the server response.

I am planning on running the processing part using:

App::finish(function(){
   // DO THE PROCESSING
})

I know that this will run after the response has been sent. However, I am not sure how to get the data which has been compiled in a controller (or any class) to be referenced in the App::finish() closure method.

I have tried using App::singleton() but there are many instances where I need to be able to continually 'set' the data, I can't just set it once. I guess I am essentially looking for a global variable that I can manipulate but I know that doesn't exist in Laravel.

Another option is to use Session::push() and Session::get() but right now I have my Session storage using memcached for this app and I would rather not do additional I/O on memcached when I could just be storing the data needed to be saved in temporary memory.

Seems like I just need a simple container to write to and read from which is saved only in memory but I cannot find that in the Laravel docs.

回答1:

You might be able to use the __destruct() magic method on whichever controllers you need to do the processing on.

You could also potentially implement it in BaseController as well if it should run for all controllers.

Another option would be to use sqlite in memory. Simply create a new connection

    'sqlite_memory' => array(
        'driver'   => 'sqlite',
        'database' => ':memory:',
        'prefix'   => '',
    ),

Then you can use DB::connection('sqlite_memory') to use that connection and store/save whatever you need using the query builder.



回答2:

You can pass data to the closure using "use".

In PHP 5.3.0, what is the function "use" identifier?

I ended up using something like this for storing data in the cache after returning the data to the user.

App::finish(function($request, $response) use ($id, $json){
    Cache::put('key_'.$id, $json, 1440);
});