Comments (6)
So yeah, i was having some issues with the remote redis-server hosted by redis, so i move to file cache, which is working great..
Actually i'm trying to cache the response of the POST, as it's for checking the status of an order, and post value has a 2step key in the body.
But i'm thinking of a different approach now, i'll instead host that particular script seprately, and have a get request to get data from that script, that way, i have more control over caching.
from httpx-cache.
I don't think caching is working on the request, as the response is similar in multiple runs.
Can you give more details about the issue ?
If my understanding is correct, you mean multiple runs of your app/script, and not multiple runs of the request in the same program. I see you are using the default memory cache in your httpx_cache.AsyncClient
, so maybe just switching that to a FileCache
or a RedisCache
wuld solve your issue.
More in cache type and how to use them: https://obendidi.github.io/httpx-cache/guide/#cache-types
The (default) in-memory dict cache is cleared at the end of your script (it's a simple python dict), so you can't reuse cache across multiple runs of your script.
The file/redis cache are more consistent.
from httpx-cache.
I'm using fastapi and Uvicorn, so the the request is basically to our other servers, so it's not a complete re-run, i'll still consider running redis or something though
from httpx-cache.
Does httpx_cache support caching of POST requests as well, or do we have to use custom transport
from httpx-cache.
I'm using fastapi and Uvicorn, so the the request is basically to our other servers, so it's not a complete re-run, i'll still consider running redis or something though
If you are using httpx_cache
in fastapi endpoints, you can keep a single cache lient in the app state so that it shared by all endpoints and request instances.
Creating a different httpx_cache.Client
for each request will discard the cache at the end of that request, example:
from fastapi import FastAPI, Depends, Request
import httpx_cache
async def lifespan(app: FastAPI):
print("startup ...")
httpx_client = httpx_cache.AsyncClient()
app.state.client = httpx_client
yield
print("shutdown ...")
await httpx_client.aclose()
app = FastAPI(lifespan=lifespan)
async def get_httpx_client(request: Request) -> httpx_cache.AsyncClient:
return request.app.state.httpx_client
@app.get("/")
async def get_httpbin(
httpx_client: httpx_cache.AsyncClient = Depends(get_httpx_client),
):
response = await httpx_client.get("https://httpbin.org/get")
response.raise_for_status()
return response.json()
Does httpx_cache support caching of POST requests as well, or do we have to use custom transport
Not by default, but you can easily add it if you want, links in the docs:
- https://obendidi.github.io/httpx-cache/cache_control/
- https://obendidi.github.io/httpx-cache/api/#client
httpx_cache.Client(cacheable_methods=["GET", "POST"])
You should keep in mind that you probably don't want to cache POST
request, because they are requests that make changes in the requested server, so it doesn't make sens to cache them (it might be different for your use case, so you know better :) )
from httpx-cache.
Closing as the issue is no longer related to httpx-cache
from httpx-cache.
Related Issues (10)
- You have an undeclared dependency on attrs HOT 3
- Hope to use redis cache HOT 3
- Patch httpx dependency HOT 2
- Conda HOT 4
- Overwrite is_response_cacheable / ignore `no-store` HOT 3
- Errors in concurrent access to cached files HOT 6
- `FileCache` on an `AsyncClient` doesn't work with `s3fs` HOT 5
- Cache Confirmation Header HOT 2
- Comparison to httpx-caching HOT 2
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 httpx-cache.