Suor / django-cacheops

A slick ORM cache with automatic granular event-driven invalidation.
BSD 3-Clause "New" or "Revised" License
2.1k stars 227 forks source link

Help Request: Automatic caching doesn't seem to be working, but manual does #420

Closed ShmuelTreiger closed 2 years ago

ShmuelTreiger commented 2 years ago

Hi! Thank you for the tool and sorry to bother you. Having an issue:

My settings:

CACHEOPS_DEFAULTS = {
    'timeout': 60*10
}

CACHEOPS = {
    'domain_settings.*': {},
    '*.*': {},
}

CACHEOPS_LRU = True
CACHEOPS_DEGRADE_ON_FAILURE = True

Test script:

import django
from django.db import connection

django.setup()

from domain_settings.models import Domains

print(len(connection.queries))
d = Domains.objects.cache()
print(len(connection.queries))
print(d.get(domain="green.com"))
print(len(connection.queries))
print(d.get(domain="green.com"))

print(len(connection.queries))
print(Domains.objects.all().cache())
print(len(connection.queries))

When I include the explicit .cache() calls, these work and each print(len(connection.queries)) prints 0 the second time I run the script. When I remove them, nothing gets cached and each query lengthens connection.queries no matter how many times I run the script. I looked through the tests and they always use the .cache(). Am I supposed to use it? The automatic vs manual distinction made me thing not. Do I have something set up incorrectly?

I'm using version 5.0 if that matters. Inherited a legacy project and if I can avoid it, I don't want to upgrade the cacheops version until I better understand what's going on and how we're using it.

Suor commented 2 years ago

You didn't include any ops to cache, like get or fetch. Configuration with empty ops only does manual caching. There are several commented examples in CACHEOPS setting in README.

ShmuelTreiger commented 2 years ago

Sorry about that. Completely misunderstood the read me. Thank you.