-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature add simple family cache management #152
base: develop
Are you sure you want to change the base?
Conversation
Merge openIMIS Main to Fork main
MERGING develop into release/23.10
MERGING release/23.10 into main
Merge des develop
MERGING develop into release/24.04
MERGING develop into release/24.04
feat: Merge Main into mngoe
MERGING release/24.04 into main
Merge updated develop from MV to Ynote Version
here perplexity answer regarding additionnal sub fields: Yes, you can cache a Django object along with its first level of related resources. Here's how you can implement this: Caching StrategyTo cache a Django object with its first level of related resources, you can use Django's low-level cache API. This approach allows you to cache the main object and its related objects together. Implementation
from django.core.cache import cache
from django.db import models
def cache_object_with_relations(obj, timeout=3600):
key = f"{obj._meta.model_name}:{obj.pk}"
cached_data = {
'main': obj,
'relations': {}
}
for field in obj._meta.get_fields():
if isinstance(field, models.ForeignKey) or isinstance(field, models.OneToOneField):
related_obj = getattr(obj, field.name)
if related_obj:
cached_data['relations'][field.name] = related_obj
elif isinstance(field, models.ManyToManyField):
related_objs = getattr(obj, field.name).all()
cached_data['relations'][field.name] = list(related_objs)
cache.set(key, cached_data, timeout)
return cached_data
def get_cached_object_with_relations(model, pk):
key = f"{model._meta.model_name}:{pk}"
cached_data = cache.get(key)
if cached_data is None:
try:
obj = model.objects.get(pk=pk)
cached_data = cache_object_with_relations(obj)
except model.DoesNotExist:
return None
return cached_data
def my_view(request, object_id):
cached_data = get_cached_object_with_relations(MyModelA, object_id)
if cached_data:
my_model_a = cached_data['main']
related_b = cached_data['relations'].get('model_b')
# Use my_model_a and related_b as needed
else:
# Handle case when object is not found
# Rest of your view logic Key Points
Considerations
By using this caching strategy, you can efficiently cache Django objects along with their first-level related resources, potentially improving your application's performance by reducing database queries[4][7]. Citations: |
to be rework to use the named cache instead |
Add simple Family cache management as a start for GraphQL cache test.
Not so much performance impact at this stage, but I can imagine that each database request removed is an improvement.
We will still have few changes to implement :