fastapi-cache icon indicating copy to clipboard operation
fastapi-cache copied to clipboard

Implement Graceful Cache Failure Handling and Logging in Initialization

Open ShantanuKudva opened this issue 1 year ago • 9 comments

Issue Summary

Currently, when the cache fails, an "AssertionError: You must call init first!" error is thrown, which is not user-friendly. Instead, the system should provide a warning message indicating that the results are not cached. Additionally, during the initialization function, it should log whether the connection to Redis was successful or not.

Steps to Reproduce the Issue

  1. Attempt to access cache results without initializing the cache.
  2. Observe the resulting error message.

Observed Behavior

An "AssertionError: You must call init first!" is thrown, disrupting the user experience.

Expected Behavior

  1. If the cache fails, a warning message should be displayed, informing the user that the results are not cached.
  2. During the cache initialization, the system should log whether the connection to Redis was established successfully.

Suggested Enhancements

Graceful Failure Handling

  • Modify the cache access logic to check if the cache has been initialized.
  • If not initialized, return a warning message instead of throwing an error.
  • Example warning message: "Cache is not initialized. Results are not cached."

Logging Connection Status

  • Enhance the init function to log the status of the connection to Redis.
  • Example log messages:
    • "Successfully connected to Redis."
    • "Failed to connect to Redis."

ShantanuKudva avatar Oct 21 '24 05:10 ShantanuKudva