It doesn't necessarily have to be a QuerySet; any function or method that takes some processing time could be decorated (e.g., an external call to an API). My aim is to reduce this code:
def do_some_processing(): cache_key = "key" if cached_value := cache.get(cache_key): return cached_value fresh_value = do_some_computing() cache.set(cached_value, fresh_value, 30) return fresh_value to this: @cached_context("key", timeout=30) def do_some_processing(): return do_some_computing() AFAIK cache.set evaluates QuerySets, how would that be a problem?. 26 Ağustos 2022 Cuma tarihinde saat 12:24:39 UTC+3 itibarıyla Adam Johnson şunları yazdı: > I'm not sure I quite understand your proposal. Are you suggesting a > decorator that caches the results of every queryset that is resolved within > the decorated function? > > If so, I'm not sure how useful it would be. First, if a single query is > problematic, there’s normally a way to optimize it within the database - > for example, a materialized view allows you to “cache” query results and > recompute them in the background. Second, QuerySets are lazy, so it could > be surprising to decorate a function that returns a QuerySet that is not > yet resolved. > > On Wed, Aug 24, 2022 at 3:49 PM suayip uzulmez <suayi...@gmail.com> wrote: > >> Usually, I need to construct a simple cache pipeline to optimize database >> access using low-level cache API (django.core.cache). >> >> I think we could use a decorator as such to ease this process: >> >> cached_context(key, *, timeout, cache) >> >> I know higher-level utilities exist such as cache_page, however I think >> the decorator above provides more granularity over what to cache. For >> example, I usually need such decorator in model methods where I do some >> database aggregation operations based on the instance. >> >> What do you think? >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Django developers (Contributions to Django itself)" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to django-develop...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-developers/facbcfa8-2404-4dc6-b56d-eb0f25ba491cn%40googlegroups.com >> >> <https://groups.google.com/d/msgid/django-developers/facbcfa8-2404-4dc6-b56d-eb0f25ba491cn%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> > -- You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-developers+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/3e380893-2211-47a9-bb6c-71e626459a87n%40googlegroups.com.