GithubHelp home page GithubHelp logo

Cache not working about httpx-cache HOT 6 CLOSED

shivsah avatar shivsah commented on June 15, 2024
Cache not working

from httpx-cache.

Comments (6)

shivsah avatar shivsah commented on June 15, 2024 1

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.

obendidi avatar obendidi commented on June 15, 2024

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.

shivsah avatar shivsah commented on June 15, 2024

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.

shivsah avatar shivsah commented on June 15, 2024

Does httpx_cache support caching of POST requests as well, or do we have to use custom transport

from httpx-cache.

obendidi avatar obendidi commented on June 15, 2024

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:

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.

obendidi avatar obendidi commented on June 15, 2024

Closing as the issue is no longer related to httpx-cache

from httpx-cache.

Related Issues (10)

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.