Comments (9)
Yes, this library is framework agnostic.
However there are some experimental functionality that's comes in 6.3.0 version and not documented yet
A cashews.contrib.fastapi
module with a set of middlewares: CacheEtagMiddleware, CacheRequestControlMiddleware, CacheDeleteMiddleware
CacheEtagMiddleware
- will add the Etag
+ If-None-Match
headers if cache was used during the request handle
CacheRequestControlMiddleware
- will handle Cache-Control
request and response header, properly disable cache on Cache-Control
no-store/no-cache/max-age-0 and add Age and Cache-Control
header if cache was used during the request handle
CacheDeleteMiddleware
- handle Clear-Site-Data
@ceteri I will be happy if you will try to use it, but at your own risk because it not tested much yet ( but I think that is better then https://github.com/long2ice/fastapi-cache )))
from cashews.contrib.fastapi import CacheRequestControlMiddleware
app = FastAPI()
app.add_middleware(CacheRequestControlMiddleware)
from cashews.
Thank you @Krukov yes, that looks really good.
cashews/examples/fastapi_app.py
Line 11 in 0b64ec9
Ultimately it might also be helpful to include features that take into account the edge (CDN) cache rules, and how the middleware needs to interact with those. For example, https://developers.cloudflare.com/cache/how-to/cache-rules/
We have not seen any caching libraries focus on the edge or intermediate storage grids used by edge caches. We've built out code to handle this, although it is not generalized in a way that it could be used readily in a framework-agnostic library.
from cashews.
I'll keep this issue open till I will add documentation about this "contrib" module
from cashews.
Hi @shijo-keyvalue - Looks like a bug with python3.7 support , I will take a look at weekends. Also I found a few bugs with CacheRequestControlMiddleware
- I gonna fix it soon, stay tuned )
from cashews.
Could you please clarify your use case and what you want to achieve or share a small code example?
If you are talking about caching HTTP responses with a minimal setup on your side, then you can look at https://github.com/requests-cache/aiohttp-client-cache or https://github.com/requests-cache/requests-cache
from cashews.
When HTTP page response get cached, shouldn't there also be directives communicated to the CDN and client through Cache-Control
headers:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control
As far as I can tell, the @cache
decorator here does not modify FastAPI/Starlette response headers?
For example, using @cache(ttl="30s")
should that also set a directive such as
Cache-Control: max-age=30
Handling ETags are another matter.
from cashews.
Also, my apologies if I'm conflating the intended usage.
It has been a common practice for cache libraries on web frameworks to support both the caching strategy and the directives from the decorator. Although clearly that's not needed if the cache is used internally, not for HTTP routes.
from cashews.
Tried adding middleware as mentioned in the above comments :
app.add_middleware(CacheRequestControlMiddleware)
But I am getting this error while starting the server.
...lib/python3.7/site-packages/cashews/contrib/fastapi.py", line 86, in CacheRequestControlMiddleware
def _to_disable(cache_control_value: Optional[str]) -> tuple[Command]:
TypeError: 'type' object is not subscriptable
from cashews.
Added a few words in README (documentation) about contrib module and integrations/tools for an fastapi app
@shijo-keyvalue Sorry but I dropped 3.7 support as we don't have CI pipelines for it and it's a very "unpopular" version.
from cashews.
Related Issues (20)
- CacheEtagMiddleware breaks with redis client_side cache HOT 1
- Counter in Cache condition
- cache_control_ttl is undocumented HOT 1
- Documenting with sphinx? HOT 1
- ValueError: Duplicated timeseries in CollectorRegistry HOT 2
- early_ttl not working in combination with CacheEtagMiddleware HOT 5
- Gracefully close a connection pool with redis > 5.0.1
- Cannot close Redis connection - AttributeError: 'Redis' object has no attribute '_client' HOT 4
- Cache doesn't work with GZIPMiddleware HOT 5
- JSON serializer HOT 5
- Early cache with sqlalchemy & fastapi depend (sqlalchemy.exc.IllegalStateChangeError: Method 'close()' can't be called here; method '_connection_for_bind()' is already in progress and this would cause an unexpected state change to <SessionTransactionState.CLOSED: 5>) HOT 1
- Formatter Called with Formatted Value HOT 6
- CacheRequestControlMiddleware: Backend not found exception with non-default cache prefix HOT 3
- cache_control_ttl can't be used directly with cache api HOT 1
- thunder_protection with multiple event loops breaks HOT 3
- re.error: bad character in group name when using custom formatter in a tag HOT 3
- Unhandled exception when there are only backends with prefixes HOT 1
- Feature: Get or set
- _empty and _empty_in_redis marker objects leak HOT 1
- Redis Client Side Connection Timeout HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cashews.