DEV Community

HarmonyOS
HarmonyOS

Posted on

Setting usingCache to true in HTTP does not take effect

Read the original article:Setting usingCache to true in HTTP does not take effect

Problem Description

When usingCache: true is used in an HTTP request, the flush method is invoked. However, the cache cannot be obtained. The code is as follows:

let option: http.HttpRequestOptions = {
    usingCache: true,
    header: {
        'Cache-Control': 'max-age=315360000'
    }
};

let httpRequest = http.createHttp();
let httpResponseCache = http.createHttpResponseCache();

httpRequest.request('https://xxxxxx', option, (err: BusinessError, res: http.HttpResponse) => {
    if (!err) {
        httpResponseCache.flush((err: BusinessError) => {
            if (err) {
                console.error('HttpCache flush fail');
            } else {
                console.info('HttpCache flush success');
            }

            const newsResponse = JSON.parse(res.result.toString()) as NewsResponse;
            this.newsList = newsResponse.data;
        });

        httpRequest.destroy();
    } else {
        console.error(`HttpCache error: ${err}`);
        // When the request is used up, the developer must call the destroy method to destroy the JavaScript object.
        httpRequest.destroy();
    }
});
Enter fullscreen mode Exit fullscreen mode

Q: Cache-Control: max-age=60 is set in the IP address response header, but the cache still fails. Why?
A: The IP address may fail to be cached due to other caches. Clear the browser cache again or uninstall and install the app again.

Background Knowledge

flush: Writes data in the cache to the file system so that all cached data can be accessed in the next HTTP request. The callback mode is used as an asynchronous method. The cached data includes the response header, response body, cookies, request time, and response time.
How to set the Cache-Control header: Cache-Control is a general header, but it is usually done on the server side. It allows you to define when, how, and how long a response resource should be cached.

Solution

To invoke the network request capability, the usingCache field can be enabled and the Cache-Control parameter is carried in the header to set the cache period. A new cache solution is added to solve the problem that the browser and server time are not synchronized. The server does not directly inform the browser of the expiration time, but rather tells the relative time. For example:

If Cache-Control: max-age=120 is set, the validity period of the cache is two minutes. Within two minutes, the browser is used for cache.
UsingCache: true indicates that the cache is read first when a request is sent. flush indicates that data in the cache is written to the file system. If Cache-Control: max-age=0 indicates that the cache validity period is 0, the cache does not take effect.
Cache-Control is a general header, which is set on the server. Set the HTTP request header to Cache-Control: max-age=xxxx on the server. In this case, the cache takes effect. If'Cache-Control':'max-age=xxxx' is set in header of HttpRequestOptions, the cache does not take effect.

Related Documents or Links

https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-http#flush9

https://developer.huawei.com/consumer/en/doc/harmonyos-references/js-apis-http#httpresponsecache9

Written by Ataberk Celiktas

Top comments (0)