Amano aa6ab9934e
Add Cache API helper (#36)
* Add new CacheMiddleware cache helper + add getUri method

* Example of cache http middleware added

* remove getUri method from RequestWrapper + add generateEtag method to CacheMiddleware

* fix etag

* Standardising the http cache example

* Add better exemple for http cache

* Implement CacheMiddleware tests and refactoring

* rename example

---------

Co-authored-by: Aymane <aymane@cashflowpositifi.fr>
Co-authored-by: Jamie Barton <jamie@notrab.dev>
2024-09-08 09:23:35 +01:00
..
2024-09-08 09:23:35 +01:00
2024-09-08 09:23:35 +01:00
2024-09-08 09:23:35 +01:00

Http Cache Example

This example demonstrates how to use the http cache middleware in Dumbo

Running the Example

  1. Install dependencies:

    composer install
    
  2. Start the server:

    composer start
    
  3. To test the cached route, use the curl command to send a GET request to http://localhost:8000/cached/greet. This route has the CacheMiddleware applied to it, so it should return cache control headers. Here's the command:

      curl -i http://localhost:8000/cached/greet
    

    Look for the ETag and Cache-Control headers in the response. The ETag header is a unique identifier for the version of the resource, and the Cache-Control header indicates the caching policy

  4. To test the cache functionality, you can send the same request again, but this time include the If-None-Match header with the ETag value from the previous response. If the resource hasn't changed, the server should return a 304 Not Modified status. Here's the command:

      curl -i -H 'If-None-Match: $ETAG' http://localhost:8000/cached/greet
      # curl -i -H 'If-None-Match: W/"..."' http://localhost:8000/cached/greet
    
  5. To test the uncached route, send a GET request to http://localhost:8000/uncached/greet. This route does not have the CacheMiddleware applied to it, so it should not return cache control headers. Here's the command:

      curl -i http://localhost:8000/uncached/greet