I am wondering if it is a good idea to do the following:
I have a Django model (which is related to a migration, therefore it has a database entry) with a bunch of properties. Accessing these are obviously hitting the database every time.
I also have many methods using these properties, like:
def is_good(self): return not self.bad and self.good > self.threshold
And these methods are used pretty frequently. So I am trying to reduce the database queries as much as possible, for performance's sake.
Maybe I can take advantage of the fact that my models are very "static", meaning that most of these attributes will never actually change their values, so
self.threshold will always hold the same values and it will never change in the database. Perhaps I can use this to my advantage and cache
is_good() to reduce the database work?
def is_good(self): try: return self._is_good except AttributeError: self._is_good = not self.bad and self.good > self.threshold return self._is_good
Is this a common and recommended practice?