Short Version: In Django (ideally Django 1.7), I want to read certain database objects just once each day at 2 AM and cache them. Each time I do MyModel.objects.get()
it will not touch the database, it will use the cache. Then I will manually refresh the cache at 2 AM. I do not want to change every single line that has a query like MyModel.objects.get()
because I would need to change 100+ lines and it would get verbose.
Long Version:
I am working with a very large Django project that is old and not entirely sensible. Once each day (at 2 AM after backups, during scheduled downtime), the Django application reads a set of protected database objects (rows, model instances), checks them for consistency, and then it uses exactly those values until the next day. These database objects (rows) can only be changed during the scheduled downtime at 2 AM, and changing them in the middle of the day would corrupt data and make things confusing.
I already wrote a pre_save
hook that errors out if you try to modify these database objects when it is not in scheduled downtime. I also gave very few people access to these database tables via the Django Admin Console.
But it is low performance, because it actually does hit the database every single time these objects are used, despite the fact that it should only hit the database once each day.
Sadly, there are over 100 lines of code that lookup and then use the tables, like this:
# In views.py:
from myapp.models import WidgetType
# In literally over 100 places:
widget_t1 = WidgetType.objects.get(pk=1)
local_var.part_num = widget_t1.part_num
Every single time it says WidgetType.objects.get
, I want it to use a cached version of the database object. I only want to refresh the cache manually once per day at 2 AM, for performance and extra safety against data corruption.
I do not want to use the cache
module every single time I do a database lookup, because that would require changing over 100 lines of code, and it would make the code more verbose.