In my website, I want to show top categories by products' view / sell counts.Since this is not something changes every hour, I want to fetch real results only once a day from database (may be once a week). Here is what I have done:def global_context(request):    ## ... some other queries    today = datetime.datetime.now().day    top_cats = top_categories(today)  # hit once a day    return {'top_categories': top_cats}@lru_cache(maxsize=2)def top_categories(day):    # fetch first 50 top-selling products    top_products = Product.objects.all().order_by('-sell_count')[:50]     # get first 10 unique categories    categories = list(set([product.categori for product in top_products]))[:10]    return categoriesThis code is working. (I have tested with per minute version). But I wonder,Is this correct way to achieve my goal?When currsize reached to maxsize nothing happened. Code kept working. Why?Should I manually reset / clear cache with cache_clear?
Implementation of lru_cache for daily top categories query
python;python 3.x;cache;django
Answering your questions:You state your goal as wanting to show:top categories by products' view / sell countsbut what you actually show is:ten categories chosen at random from the categories that contain a product in the top 50 products by sell countwhich is not the same at all.Assuming your description in the post is what you really want, then you need a query like this:# Top 10 categories by product sell count plus view count.return (Category.objects        .annotate(score=Sum('product__sell_count') + Sum('product__view_count'))        .order_by('-score')[:10])I'm assuming here that you have a Category model with a many-to-many relation named product with the Product model, and that the Product model has a view_count field, and that adding the sell count and the view count is the appropriate scoring function. If your actual models or requirements are different then you'll have to update the query appropriately.If functools.lru_cache can't find the requested item in its cache, then it calls the function as usual and puts the result in the cache, throwing out the oldest cache entry if necessary to make room. So even if the cache is full, the function still works and returns the correct results. You could set maxsize=1 here since you only need to remember the most recent result.The only reasons to manually clear the cache would be (i) it's using too much memory; or (ii) the cached results may be out of date and need to be recomputed using up-to-date information.Further comments on the design:Each Django process ends up having a cache of the top categories query result. But this might differ according to exactly when the query was issued, leading to different processes having different top categories. If this is a problem for your application, you probably want to use a single application cache, as noted by alexce in comments.You probably want to incorporate some measure of time in your top categories query, for example aggregating over recent sales and views rather than all sales and views. (But maybe this is already done and recorded in the sell_count field?)