FastAPI Cache


1. Simple In-Memory Cache

from fastapi_cache import FastAPICache
from fastapi_cache.backends.inmemory import InMemoryBackend

cache = FastAPICache(
    backend=InMemoryBackend()
)

2. Redis Cache

from fastapi_cache import FastAPICache
from fastapi_cache.backends.redis import RedisBackend

cache = FastAPICache(
    backend=RedisBackend(host="localhost", port=6379)
)

3. Memcached Cache

from fastapi_cache import FastAPICache
from fastapi_cache.backends.memcached import MemcachedBackend

cache = FastAPICache(
    backend=MemcachedBackend(host="localhost", port=11211)
)

4. MongoDB Cache

5. PostgreSQL Cache

6. SQLite Cache

7. Caching a Function Output

8. Caching with Expiry Time

9. Caching with Key Prefix

10. Caching Response Data

11. Invalidate Cache Items

12. Invalidate Cache Items with Prefix

13. Clear Cache

14. Cache Object with Overwrite Policy

15. Configure Default Cache Duration

16. Use a Cache Middleware

17. Inject Cache into Dependency Injection

18. Accessing Cache Stats (backend-dependent)

19. Caching with a Key-Value Pair

20. Caching with a Dictionary

21. Caching with a List

22. Caching with a Set

23. Caching with a ZSet

24. Caching with a Bitmap

25. Caching with a HyperLogLog

26. Caching with a GeoSet

27. Caching with a Stream

28. Caching with a TimeSeries

29. Caching with a WebSocket Channel

30. Caching with a Crontab Schedule

31. Caching with a Time of Day Schedule

32. Caching with a Rate Limiter

33. Caching with a Sliding Window Rate Limiter

34. Caching with a Leaky Bucket Rate Limiter

35. Caching with a Token Bucket Rate Limiter

36. Caching with a RedisPubSub Backend