user6216601
user6216601

Reputation:

Optimize API call in Symfony

How optimize an API call in symfony?

I call with Guzzle bundle, but the time in some situations is very long.

In client application call a function from the server. In server application extract the objects from the database and send back to the client. In client creat the new object with properties from server respons.

Upvotes: 3

Views: 2133

Answers (2)

Kévin Dunglas
Kévin Dunglas

Reputation: 3024

If you control the server

You should put a cache reverse proxy like Varnish on top of your PHP server. The PHP app must send HTTP cache headers to tell to the proxy how many time it must cache the request. Alternatively, you can use a library like FOSHttpCache to setup a cache invalidation strategy (the PHP server will purge the cache from the proxy when an update of the data occurs - it's a more advanced and complex scenario). The PHP server will not even be called if the requested resource is in the reverse proxy cache.

You should also use a profiler like Blackfire.io or xhprof to find why some parts of your PHP code (or your SQL queries) take so many time to be executed, then optimize.

If you control the client

You can use this HTTP cache middleware for Guzzle to cache every API result according to HTTP headers sent by the API.

Upvotes: 0

pavlovich
pavlovich

Reputation: 1942

One of the ways to improve your API calls is to use caching. In Symfony there are many different ways to achieve this. I can show you one of them (PhpFileCache example):

In services.yml create cache service:

your_app.cache_provider:
    class: Doctrine\Common\Cache\PhpFileCache
    arguments: ["%kernel.cache_dir%/path_to/your_cache_dir", ".your.cached_file_name.php"]

(Remember, you need Doctrine extension in your app to work)

Then pass your caching service your_app.cache_provider to any service where you need caching:

Again in your services.yml:

some_service_of_yours:
    class: AppBundle\Services\YourService
    arguments: ['@your_app.cache_provider']

Finally, in your service (where you want to perform API caching):

use Doctrine\Common\Cache\CacheProvider;

class YourService
{
    private $cache;

    public function __construct(CacheProvider $cache)
    {
        $this->cache = $cache;
    }

    public function makeApiRequest()
    {
        $key = 'some_unique_identifier_of_your_cache_record';

        if(!$data = $this->cache->fetch($key))
        {
            $data = $provider->makeActualApiCallHere('http://some_url');
            $this->cache->save($key, serialize($data), 10800); //10800 here is amount of seconds to store your data in cache before its invalidated, change it to your needs
        }

        return $data; // now you can use the data
    } 
}

This is quite GENERIC example, you should change it to your exact needs, but idea is simple. You can cache data and avoid unnecessary API calls to speed things up. Be careful though, because cache has drawback of presenting stale(obsolete) data. Some things can (and should) be cached, but some things don't.

Upvotes: 1

Related Questions