У меня есть следующий метод (упрощенный) внутри пользовательского набора запросов:
Код: Выделить всё
def queryset_from_cache(self, key: str=None, timeout: int=60):
# Generate a key based on the query.
if key is None:
key = self.__generate_key # ()
# If the cache has the key, return the cached object.
cached_object = cache.get(key, None)
# If the cache doesn't have the key, set the cache,
# and then return self (from DB) as cached_object
if cached_object is None:
cached_object = self
cache.set(key, cached_object , timeout=timeout)
return cached_object
Код: Выделить всё
queryset = MyModel.objects.filter(id__range=[0,99]).queryset_from_cache()
Мой вопрос:
Будет ли такое использование работать?Или будет ли он вызывать MyModel.objects.filter(id__range=[0,99]) из базы данных, несмотря ни на что?
Поскольку обычно кэширование выполняется следующим образом:
Код: Выделить всё
cached_object = cache.get(key, None)
if cached_object is None:
cached_object = MyModel.objects.filter(id__range=[0,99])
#Only now call the query
cache.set(key, cached_object , timeout=timeout)
Подробнее здесь: https://stackoverflow.com/questions/735 ... -in-django
Мобильная версия