openfoodfacts / openfoodfacts-php

PHP wrapper for Open Food Facts
https://packagist.org/packages/openfoodfacts/openfoodfacts-php
MIT License
58 stars 34 forks source link

feat: prevent-product-not-found-response-from-being-cached #38

Closed khalyomede closed 1 year ago

khalyomede commented 2 years ago

This PR prevents "product not found" responses to be cached. It will help in case the product is not found at a given moment, then a contributor adds this product.

With this PR, next time the API is called, the product will be found, and will be cached (instead of caching the "product not found" response, and never being able to get the product found response until the cache is flushed).

sonarcloud[bot] commented 2 years ago

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot E 4 Security Hotspots
Code Smell A 3 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

Dwarfex commented 2 years ago

I'd think it would be best to let the user of this library decide, if the cache should be used - and not hard-code it like we both did in the past :sweat_smile:

khalyomede commented 2 years ago

I get situations when I scan a product, the product does not exist yet, then I add it using the OpenFoodFact Android app, and re-run the API call but the "product not found" result is still in cache despite the product exist in the O.F.Fact database.

@Dwarfex Can you give me examples when you see caching the result of the API if the product is not found is legit?

If the reason is legit, then I propose to offer the possibility to flush the cache of the specific product (to do on another PR) if you do not agree to merge this PR.

Currently I have to fix this using an hard coded base URL in my code base (Laravel):

$code = $request->code;
$product = OpenFoodFacts::barcode($code);

if (empty($product)) {
  Cache::forget(md5("https://world.openfoodfacts.org/api/v0/product/$code.json"));

  throw new NoProductFoundException();
}
sonarcloud[bot] commented 2 years ago

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot E 4 Security Hotspots
Code Smell A 3 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication