This project demonstrates how to implement Redis cache middleware to handle cache responses synchronously. The application should be able to function properly even if Redis is not running; however, the caching process will be disabled in such cases.
Configure the environment variable REDIS_HOST, or if not set, the default host redis://localhost:4379 will be used. TLS is supported using the rediss:// protocol.
Run the server using cargo run.
To test the demo, send a GET request to /fibonacci/{number}, where {number} is a positive integer of type u64.
Cache-Control: no-cachewill return the most up-to-date response while still caching it. This will always yield amisscache status.Cache-Control: no-storewill prevent caching, ensuring you always receive the latest response.
When making the first GET request to /fibonacci/47, it may take around 8 seconds to respond.
If Redis is running and the connection is established, subsequent requests should return the cached result immediately, a hit cache status will be returned, but with content type application/json.
- Connecting to a remote Redis server might introduce significant overhead and could lead to prolonged connection times or even failure to reach the remote server.
- Implement asynchronous insertion of cache responses.
- Explore using an in-memory datastore within the application process to reduce reliance on Redis.