Skip to content

DJDT not showing low level caching correctly in Docker #1496

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

Closed
LiorA1 opened this issue Aug 26, 2021 · 10 comments · Fixed by #1497
Closed

DJDT not showing low level caching correctly in Docker #1496

LiorA1 opened this issue Aug 26, 2021 · 10 comments · Fixed by #1497

Comments

@LiorA1
Copy link

LiorA1 commented Aug 26, 2021

Hi,
I working with DJDT for a few months.
I figure out the "not showing" issue in docker, but the data inside the cache panel is not showing properly.
The inner data, doesnt match the outer data and in the arguments column it shows addresses or something alike.

A proper data: (taken from an online guide)
image
(From: https://testdriven.io/blog/django-low-level-cache/#cache-backend)

What it shows inside a Docker:
ch

BTW: It also shows it like a view cache, but the view is not cached (Its a dynamic view)
My Configuration:


DEBUG_TOOLBAR_ENABLED = True
if DEBUG and DEBUG_TOOLBAR_ENABLED:
    INSTALLED_APPS.append('debug_toolbar')
    MIDDLEWARE.insert(0, 'debug_toolbar.middleware.DebugToolbarMiddleware')
    MIDDLEWARE.insert(1, 'django.middleware.cache.UpdateCacheMiddleware')
    MIDDLEWARE.append('django.middleware.cache.FetchFromCacheMiddleware')
    #INTERNAL_IPS.append('127.0.0.1')

    DEBUG_TOOLBAR_CONFIG = {
        'SHOW_TOOLBAR_CALLBACK': lambda request: False if request.is_ajax() else True,
    }

Does anyone have idea how to solve it ?
Where to start ?

@tim-schilling
Copy link
Member

Hi @LiorA1, can you share the view or code that you're trying to use this with? Looking at the arguments, it seems like you're using some decorators to handle the caching and since none are set, I would assume any low level caching logic is being skipped over because the entire view's response is being cached.

Please also share any caching related settings, such as middleware, decorators, caching applied via the urls and whether or not you're using template caching for this view.

@LiorA1
Copy link
Author

LiorA1 commented Aug 26, 2021

The view was view-cached in the past, but I remove it.
I have another static view that is view-cached.
I tried to restart Docker, and to rebuild the image.

My Base Settings:

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

My Development Settings:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.PyMemcacheCache',
        'LOCATION': 'cache:11211',
    }
}

urls:

path('list/', views.ResumeListView.as_view(), name="resume_list"),

The view:

class ResumeListView(OwnerListView):
    ""Display all the resumes""
    model = Resume
    ordering = ['-created_at']
    # template_name = "resumes/<modelName>_list.html"
    queryset = Resume.objects.prefetch_related('tags', 'author', 'author__profile')

    def get_queryset(self):
        base_queryset = super(ResumeListView, self).get_queryset()

        # Check for searchTerm existence
        searchTerm = self.request.GET.get("search", False)

        if searchTerm:
            tags_required = Tag.objects.tags_id_from_str(searchTerm)

            # ordering attributes kept
            ordering = self.get_ordering()
            if not ordering:
                ordering = []
            else:
                ordering = self.ordering[:]
            ordering.insert(0, '-score')

            # Query which resumes have the wanted tags, order by the match score.
            q_Query = Q(tags__in=tags_required)
            # res_queryset = base_queryset.filter(tags__isnull=False).distinct().annotate(score=Count('tags', filter=q_Query)).filter(score__gt=0).order_by(*ordering)

            res_queryset = base_queryset.filter(tags__isnull=False).distinct().annotate(score=Count('tags')).filter(q_Query, score__gt=0).order_by(*ordering)

        else:
            res_queryset = base_queryset.fetch_store_resume_list()

        return res_queryset

(I decided to do all the caching in the models file)
The specific Custom Manager method:

def fetch_store_resume_list(self):
        ""
        Used for Fetch and Store the query of all the resumes with the related fields.
        Used primarily by the 'ResumeListView' View.
        ""
        resumes_cache_key = "resumes"
        resumes_queryset = cache.get(resumes_cache_key)
        if resumes_queryset is None:
            resumes_queryset = self
            cache.set(resumes_cache_key, resumes_queryset, timeout=300)

        return resumes_queryset

If I forget smoething the project is on github:
https://github.com/LiorA1/resume_reviews

@tim-schilling
Copy link
Member

The view was view-cached in the past, but I remove it.
I have another static view that is view-cached.

Sounds good. Please retry the request without the view level caching and let us know what the toolbar presents in the cache panel.

@LiorA1
Copy link
Author

LiorA1 commented Aug 27, 2021

Tried it.
I stopped using any caching in the following App, but now nothing shows.. (After erase all the docker containers/images/volumes)

This is a sequential request, in the first one SQL queries appeared.

ch

It doesnt show nothing.
Is there any place that I check with debugger (in the code) ?

P.S-
I trying to cache entries with file storage at AWS S3. Maybe this cause the issue ?

@tim-schilling
Copy link
Member

Did you try without the view level decorators and without the middleware?

It does seem like something is broken, but I can't tell what. I'd really appreciate it if you could create a small reproducible example for us to more thoroughly investigate.

@LiorA1
Copy link
Author

LiorA1 commented Aug 27, 2021

In the particular App I even dont import the cache_page decorator.
I didnt try without the Django middleware. It doesnt required to allow caching ?

I'm working with VSC and docker-compose, it hard to debug, but I will try and update here.

I will try to reproduce it and update here when finish.

@tim-schilling
Copy link
Member

Whoops. I didn't mean without all middleware. Only without the middleware highlighted in the link above which shows your project's settings:

    MIDDLEWARE.insert(1, 'django.middleware.cache.UpdateCacheMiddleware')
    MIDDLEWARE.append('django.middleware.cache.FetchFromCacheMiddleware')

@LiorA1
Copy link
Author

LiorA1 commented Aug 27, 2021

Ok, Four hours later - reproduce it.
pls see: https://github.com/LiorA1/djdt_project

I will try to comment out these middleware.
I have there a json file for a superuser (name:root/pass:qwertyuiop) and few entries.

Edit:
I tried to comment these middleware - The caching doesnt get called. (Only SQL queries happens)

@tim-schilling
Copy link
Member

Thanks for the help! There was indeed a bug, but it was unrelated to docker.

@LiorA1
Copy link
Author

LiorA1 commented Aug 27, 2021

No problem, I also tried to figure it out. (reach to "_store_call_info"/"get_cache_key" - It took time to set the debugger ...)

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

Successfully merging a pull request may close this issue.

2 participants