GithubHelp home page GithubHelp logo

Comments (9)

Krukov avatar Krukov commented on June 21, 2024 2

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.

ceteri avatar ceteri commented on June 21, 2024 2

Thank you @Krukov yes, that looks really good.

from cashews.contrib.fastapi import CacheDeleteMiddleware, CacheEtagMiddleware, CacheRequestControlMiddleware

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.

Krukov avatar Krukov commented on June 21, 2024 1

I'll keep this issue open till I will add documentation about this "contrib" module

from cashews.

Krukov avatar Krukov commented on June 21, 2024 1

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.

AIGeneratedUsername avatar AIGeneratedUsername commented on June 21, 2024

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.

ceteri avatar ceteri commented on June 21, 2024

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.

ceteri avatar ceteri commented on June 21, 2024

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.

shijo-keyvalue avatar shijo-keyvalue commented on June 21, 2024

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

@ceteri @Krukov

from cashews.

Krukov avatar Krukov commented on June 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.