Recipe: Response caching and Connection pooling (original) (raw)

In typical usage, EasyHttp is used in combination with response cache and connection pooling. Applications do not need to save instances of EasyHttp and rather can create new instances when required. To reuse the same Http cache and connection pool, just save these instances and configure them to EasyHttp when building its instances.

To create Http cache instance, EasyHttp needs a dir path and a maximum cache size (old data is removed whenever the cache size exceeds the maximum). Response caching used HTTP headers like Cache-Control: max-stale=3600 etc. to work. There are cache headers to force a cached response, force a network response, or force the network response to be validated with a conditional GET.

To create Http connection pool instance, EasyHttp needs a count of the no of idle connections to keep and a keep-alive timeout (default is 10 and 60 sec respectively).

try {
    // create a cache with dir path = current working dir; cache size = 100 KB
    easyhttpcpp::HttpCache::Ptr pCache = easyhttpcpp::HttpCache::createCache(Poco::Path::current(), 1024 * 100);

    // create a default http connection pool
    easyhttpcpp::ConnectionPool::Ptr pConnectionPool = easyhttpcpp::ConnectionPool::createConnectionPool();

    // configure http cache and connection pool instance
    easyhttpcpp::EasyHttp::Builder httpClientBuilder;
    httpClientBuilder.setCache(pCache)
            .setConnectionPool(pConnectionPool);

    // create http client
    easyhttpcpp::EasyHttp::Ptr pHttpClient = httpClientBuilder.build();

    // create a new request and execute synchronously
    easyhttpcpp::Request::Builder requestBuilder;
    easyhttpcpp::Request::Ptr pRequest = requestBuilder.setUrl("https://github.com/sony/easyhttpcpp").build();
    easyhttpcpp::Call::Ptr pCall1 = pHttpClient->newCall(pRequest);
    easyhttpcpp::Response::Ptr pResponse1 = pCall1->execute();

    if (!pResponse1->isSuccessful()) {
        std::cout << "HTTP GET Error: (" << pResponse1->getCode() << ")" << std::endl;
    } else {
        std::cout << "HTTP GET Success!" << std::endl;
    }

    // create another request with the same http client and 
    // this time response will be returned from cache (given server has configured proper cache headers)
    easyhttpcpp::Call::Ptr pCall2 = pHttpClient->newCall(pRequest);
    easyhttpcpp::Response::Ptr pResponse2 = pCall1->execute();

    if (!pResponse2->isSuccessful()) {
        std::cout << "HTTP GET Error: (" << pResponse2->getCode() << ")" << std::endl;
    } else {
        std::cout << "HTTP GET Success!" << std::endl;
    }
} catch (const std::exception& e) {
    std::cout << "Error occurred: " << e.what() << std::endl;
}

You can also force a request to ignore cache by using CacheControl::createForceNetwork() or the other way i.e. to force a request to only return cache with CacheControl::createForceCache(). Note that when using CacheControl::createForceCache(), if the response requires the network, EasyHttp will return a 504 Unsatisfiable Request response.