I'm working on a project in which I should use Django, grpc and mongodb. The db operations are readonly except when user interacts with the system for example posting comments or like a post, in that case I need to update the existing db document. I also want to store caches and I do it on retrieve() and list().
Now I have problem with updating caches when user comments for example, I am able to update caches which are made in retrieve() and list() both, but doing it also for nested documents is really hard (it's doable but is not a clean way), hence I want to know if CachedReferenceField comes handy for this purpose or not??
I mean I have my own caching method which is exteremly personal (which I don't like and wish to do it in a "cleaner" way, any help about it would be appreciated too;), doesn't this CachedReferenceField interupt my caching method? or it is internall and has nothing to do with my method of caching?
Here are my models:
import uuid
from django.utils import timezone
from mongoengine import (
CASCADE,
BooleanField,
CachedReferenceField,
DateTimeField,
Document,
EmbeddedDocument,
EmbeddedDocumentListField,
IntField,
ListField,
StringField,
UUIDField,
)
### Abstracts
class Base(Document):
"""Provide Base fields which are common among many models."""
id = IntField(primary_key=True)
identifier = UUIDField(default=uuid.uuid4, binary=False)
updated_at = DateTimeField(default=timezone.now)
created_at = DateTimeField(default=timezone.now)
image_media_identifier = StringField(max_length=255, unique=True)
extra_data = StringField()
title = StringField(max_length=255)
meta = {"abstract": True}
def save(self, **kwargs):
self.updated_at = timezone.now()
return super().save(**kwargs)
### Embedded models
class Comment(EmbeddedDocument):
updated_at = DateTimeField(default=timezone.now)
created_at = DateTimeField(default=timezone.now)
name = StringField(max_length=255)
content = StringField()
visible = BooleanField(default=False)
### Actual Models
class Course(Base):
description = StringField()
summary = StringField()
course_time = IntField()
price = IntField()
content = StringField()
status = StringField(
choices=[("on_performing", "On_performing"), ("completed", "Completed")],
)
lesson_count = IntField()
likes = IntField(default=0)
visible = BooleanField(default=True)
comments = EmbeddedDocumentListField(Comment)
meta = {
"collection": "course",
}
class Lesson(Base):
lesson_time = IntField()
video_media_identifier = StringField(max_length=255, unique=True)
is_active = BooleanField(default=True)
price = IntField()
tags = ListField(StringField(max_length=50))
course = CachedReferenceField(Course, 1, reverse_delete_rule=CASCADE)
comments = EmbeddedDocumentListField(Comment)
meta = {
"collection": "lesson",
}
class Package(Base):
is_active = BooleanField(default=True)
courses = ListField(
CachedReferenceField(Course, "__all__", reverse_delete_rule=CASCADE)
)
meta = {
"collection": "package",
}
I read the document but it's not so descriptive.