Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache + Promise #3

Closed
meandus opened this issue Jan 31, 2020 · 1 comment
Closed

Cache + Promise #3

meandus opened this issue Jan 31, 2020 · 1 comment

Comments

@meandus
Copy link

meandus commented Jan 31, 2020

Hi,

I'm trying to do async get for concurrent requests. Do you know the correct implementation for cache with Promise

/**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
        $this->store = app('cache')->store('redis'); // Laravel
        $this->handler = new Middleware($this->store, config('api-app.cache'), false);
        $this->stack = HandlerStack::create();
        $this->stack->push($this->handler);
        $this->client = new Client([
            'handler' => $this->stack,
            'base_uri' => config('api-app.url'),
            'headers' => [config('api-app.header_token') => config('api-app.token')]
        ]);
    }

/**
     * Show the application dashboard.
     *
     * @return \Illuminate\View\View
     */
    public function index()
    {
        try {

            $promises = [
                'status_api' => $this->client->getAsync('/api/v1/heartbeat/hello', ['cache_ttl' => 60, 'cache_key' => 'status_api',]),
            ];
            $responses = Promise\unwrap($promises);
            $responses = Promise\settle($promises)->wait();

            $status_api = $responses['status_api']->getBody(); **<== FAILURE**



thanks in advance,

@pecuchet
Copy link
Member

pecuchet commented Apr 10, 2020

Hello meandus,
Sorry for the very late response! You probably already figured it out by now, but the problem is that your are using unwrap and settle at the same time. Both do the same (except unwrap will stop if any of the requests fail).

So you have two possibilities, depending on your needs:

  1. with unwrap:
$responses = Promise\unwrap($promises);
$status_api = $responses['status_api']->getBody();
  1. or with settle (Guzzle's doc is not very precise here):
$responses = Promise\settle($promises)->wait();
$status_api = $responses['status_api']['value']->getBody();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants