Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncRedisStorage does not write to Redis #211

Open
arseniiarsenii opened this issue Mar 28, 2024 · 1 comment
Open

AsyncRedisStorage does not write to Redis #211

arseniiarsenii opened this issue Mar 28, 2024 · 1 comment

Comments

@arseniiarsenii
Copy link

arseniiarsenii commented Mar 28, 2024

Hi, I'm using an async Httpx client with Hishel transport, that I create with such code:

transport = hishel.AsyncCacheTransport(
    transport=httpx.AsyncHTTPTransport(), 
    storage=hishel.AsyncRedisStorage(
        client=get_redis_connection(),
        ttl=300,
    ), 
    controller=hishel.Controller(
        cacheable_methods=["GET", "POST"],
        cacheable_status_codes=[200],
        allow_heuristics=False,
        clock=None,
        allow_stale=True,
        always_revalidate=False,
        force_cache=True,
        key_generator=custom_key_generator,  # Same func from docs
    )
)

I make requests using client looking at the state of my Redis instance at the same time, and can see no keys appearing.
However, I can see responses are returned from cache. response.extensions["from_cache"] is True.
Seems like Hishel falls back to RAM caching instead of using Redis for some reason.

@karpetrosyan
Copy link
Owner

Hi!
I can't reproduce the issue locally:(

My code:

import httpx
import hishel
import anyio

def get_redis_connection():
    import redis.asyncio as redis

    client = redis.Redis()
    return client

async def main():

    transport = hishel.AsyncCacheTransport(
        transport=httpx.AsyncHTTPTransport(), 
        storage=hishel.AsyncRedisStorage(
            client=get_redis_connection(),
            ttl=300,
        ), 
        controller=hishel.Controller(
            cacheable_methods=["GET", "POST"],
            cacheable_status_codes=[200],
            allow_heuristics=False,
            clock=None,
            allow_stale=True,
            always_revalidate=False,
            force_cache=True,
        )
    )

    async with httpx.AsyncClient(transport=transport) as client:
        response = await client.get("https://hishel.com")
        print(response.extensions["from_cache"])

anyio.run(main)

My redis response

127.0.0.1:6379> KEYS *
1) "41ebb4dd16761e94e2ee36b71e0d916e"

If I am not wrong, there is no any fall back logic in the storages.

Try something like this from your code

# get all keys
print(await transport._storage._client.keys("*"))

If it print keys, I guess you were connected to the wrong redis instance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants