Hello, I'm working on integrating Django's cache and signals into my project for the first time, and I want to ensure I'm on the right track. I've set up Redis Cache via Render, where my web service and database are also hosted. I also took note about the limitation of Django's Signal from one of the django users in this channel that shared this article; https://www.django-antipatterns.com/antipattern/signals.html . For my case, I think signals should be fine since I ma not working with ManytoMany relationships
Here's the summary:
What I'm Doing:
-
Caching Database Queries:
I cache shoe data with a key"cached_shoe_data", storing JSON representations of shoe objects. If the cache misses, I query the database and update the cache.
Here's the relevant code in mytools.py:# tools.py from django.core.cache import cache @method_tool() def get_shoes(self) -> str: """Get what shoes are in the database.""" cache_key = "cached_shoe_data" shoe_data = cache.get(cache_key) if shoe_data is not None: return shoe_data shoes = CollectableShoe.objects.all() # pyright: ignore if not shoes: return "Empty" shoe_data_list = [] for shoe in shoes: shoe_info = { "brand": shoe.brand, "model": shoe.model, "color": shoe.color, "sku_code": shoe.sku_code, "rarity": shoe.rarity, "description": shoe.description, "collaboration_artist": shoe.collaboration_artist, "year_produced": shoe.year_produced, "retail_price": float(shoe.retail_price), "market_price": float(shoe.market_price), "quantity_on_release": shoe.quantity_on_release, "size": float(shoe.size), "image": shoe.image.url, } shoe_data_list.append(shoe_info) shoe_data = json.dumps(shoe_data_list) cache.set(cache_key, shoe_data, timeout=3600) return shoe_data -
Signals to Clear Cache:
I'm using Django signals to delete cached data whenever theCollectableShoeorShoeDescriptionmodels are updated or deleted.Here's my
signals.py:from django.db.models.signals import post_save, post_delete from django.dispatch import receiver from search_app.models import CollectableShoe, ShoeDescription from django.core.cache import cache @receiver([post_save, post_delete], sender=CollectableShoe) def update_retriever_cache(sender, **kwargs): cache.delete("cached_shoe_data") print("CollectableShoe was updated or deleted")
Questions:
-
How to Confirm Redis Cache is Working?
I’m hosting Redis via Render. Is there a way to check if the cache is actually being used and the cache keys are being set/deleted properly? -
Signal Configuration:
Does the way I've set up signals look correct? Specifically, am I properly handling multiple senders with this signal setup? -
General Feedback:
Are there better practices or optimizations I should consider when caching or using signals in Django?
Any inputs are appreciated, especially if you’ve worked with Redis Cache and Django signals in production. Thanks in advance!