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

cache_url doesn't handle backend properly. #464

Open
RemiDesgrange opened this issue Apr 12, 2023 · 5 comments
Open

cache_url doesn't handle backend properly. #464

RemiDesgrange opened this issue Apr 12, 2023 · 5 comments
Assignees
Labels
need reproducible example Example is required to reproduce the issue

Comments

@RemiDesgrange
Copy link

I have no idea if it's bug or a feature:

let's say I have 2 env var:

DEFAULT_COMPONENT_URL=dummycache://
DEFAULT_COMPONENT_BACKEND=dumy

in settings.py

import environ
from pathlib import Path

BASE_DIR = Path(environ.Path())
env = environ.FileAwareEnv(
    interpolate=True,
)
environ.Env.read_env(BASE_DIR / ".env")
CACHES = {
    "default": env.cache_url(
        "DEFAULT_CACHE_URL",
        f"redis://redis:6379/1",
        backend=env.str("DEFAULT_CACHE_BACKEND", "django_redis.cache.RedisCache"),
    )
}

the backend is set here

'BACKEND': cls.CACHE_SCHEMES[url.scheme],
to django.core.cache.backends.dummy.DummyCache.

But the backendkey is reset to the default value 44 lines later here

if backend:
config['BACKEND'] = backend

no idea if this is intentional or not. I the fix is only removing the if backend line I'll be happy to submit a PR.

@sergeyklay
Copy link
Collaborator

Hi @RemiDesgrange,

Thank you for bringing this issue to our attention. To better understand the problem and evaluate your proposed solution, could you please provide a test case that reproduces the issue? A test that highlights the current problems and needs would be particularly helpful.

@sergeyklay sergeyklay self-assigned this Sep 9, 2023
@sergeyklay sergeyklay added the need reproducible example Example is required to reproduce the issue label Sep 9, 2023
@RemiDesgrange
Copy link
Author

Ok, in what form this solution should be provided ? The settings.py I gave can be executed in a shell (without even django). Do you need a test case with pytest etc... ?

Thx.

@sergeyklay
Copy link
Collaborator

Ok, in what form this solution should be provided ? The settings.py I gave can be executed in a shell (without even django). Do you need a test case with pytest etc... ?

Thx.

Providing a unit test would be ideal

@fdemmer
Copy link
Contributor

fdemmer commented Jan 30, 2024

@RemiDesgrange

in your example you mention DEFAULT_COMPONENT_URL. is that a typo? should it mean DEFAULT_CACHE_URL?

no idea if this is intentional or not. I the fix is only removing the if backend line I'll be happy to submit a PR.

i am guessing it is intentional, although there is no documentation or testcase for it.

the backend kwarg should override the backend determined via the "scheme" and can be used to set a specific cache backend implementation, e.g.:

# use 3rd party django-redis backend instead of built-in
env.cache_url("redis://redis:6379/1", backend="django_redis.cache.RedisCache")

A few other shortcuts support the same mechanism, like db(engine=...), email(backend=...) or search_url(engine=...).

edit: correction, here's a test:

def test_custom_backend():

/cc @sergeyklay looks like this is properly working and tested; may need docs.

@RemiDesgrange
Copy link
Author

Sorry DEFAULT_CACHE_URL it is. DEFAULT_COMPONENT_URL is indeed a typo.

Thanks for the clarification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need reproducible example Example is required to reproduce the issue
Projects
None yet
Development

No branches or pull requests

3 participants