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

Best way to change backend on the fly #147

Open
majorgilles opened this issue Oct 24, 2019 · 2 comments
Open

Best way to change backend on the fly #147

majorgilles opened this issue Oct 24, 2019 · 2 comments

Comments

@majorgilles
Copy link

Hello, sorry if this questions is a bit broad, but I'm looking for a way to not make ring be specifically tied to one precise backend, after the functions have been evaluated.

For example, I often need to dynamically switch from a dev environment to a staging or prod environment, and it would really be problematic if the cache was sharing values from all of those different environment.

One solution I have found is just to disable ring for non prod environments with my own decorator.

def redis_cache(expire: int, coder: str):
    def decorator_redis_cache(func):
        if get_current_stage() == Stage.PROD:
            client = get_redis_client()
            return ring.redis(client=client, expire=expire, coder=coder)(func)
        else:
            return func

    return decorator_redis_cache

but this is far from perfect.

I don't know if there would be a way to get the client dynamically inside ring via a function call...

Sorry if this seems a bit noobish.

@youknowone
Copy link
Owner

Hello,

For now, it is one of major missing feature of ring. I am working for that in #137 for now.

It is up to the complexity of your project, but in easy case when stage/prod is decided statically, this code worked for my projects

if get_current_stage() == stage.PROD:
    redis_cache = functools.partial(ring.redis, coder=coder)
else:
    redis_cache = functools.partial(ring.dict, coder=coder, default_action='execute')

If you are using python3.7+, passing ContextVar is also possible to switch only backend storage, but it doesn't help when you don't want to run function.

@majorgilles
Copy link
Author

Thanks for your answer,
this is more static than I would like, but would still improve the situation. Thanks again.

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