🎉 Proudly, we are happy to announce the only Headless solution for PrestaShop. Read More

PrestaShop Webservices API caching trick to save network

Prevent sending duplicate content

Based on this Pull Request, it's possible to do some sort of caching when data has not changed. How?

When we send a request to one of the PrestaShop webservices API, within the response there is header:

Content-Sha1

This header contains the calculated hash value of the requested content (that CRUD resource). For example, when we request for product with ID=2, PrestaShop fetches the data and calculates its hash, then puts the result in Content-Sha1 header and returns back to the client.

In the subsequent requests, when we request the same resource (product with ID=2), we also set this header Content-Sha1. The back-end API fetches the data and calculates the hash again. This time it compares the hash result with the header value sent from client. If the hashes are same, PrestaShop returns 304 Not Modified, but if they are not equal, PrestaShop returns the data.

How to do caching?

Take attention that this practice is useful for large data which saves network traffic, and the client-side should take care of the caching mechanism. It's good to mention that the fetching and processing data still will be done by PrestaShop.


Posted 3 months ago by Sam Berry

Comments

Add a comment