diff options
author | Philip Sargent <philip.sargent@gmail.com> | 2023-10-23 20:50:28 +0300 |
---|---|---|
committer | Philip Sargent <philip.sargent@gmail.com> | 2023-10-23 20:50:28 +0300 |
commit | c8c21fbe045e5321442017b9d59cd74bed8c92a2 (patch) | |
tree | 3994afb0f03acbc7df24cb93a03090aa2aa21e7f /core/models/wallets.py | |
parent | 1694d0153609ec85d04a328004a867ac61103490 (diff) | |
download | troggle-c8c21fbe045e5321442017b9d59cd74bed8c92a2.tar.gz troggle-c8c21fbe045e5321442017b9d59cd74bed8c92a2.tar.bz2 troggle-c8c21fbe045e5321442017b9d59cd74bed8c92a2.zip |
JSON cache per python instance - working
Diffstat (limited to 'core/models/wallets.py')
-rw-r--r-- | core/models/wallets.py | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/core/models/wallets.py b/core/models/wallets.py index 1cc42e8..b357344 100644 --- a/core/models/wallets.py +++ b/core/models/wallets.py @@ -79,6 +79,19 @@ class Wallet(models.Model): but we may change this if we need to do a Django query on e.g. personame ManyToMany field uses modern Django: a hidden Class, unlike CaveAndEntrances which is explict and visible. + + We parse all the JSON on initial reset/import but we only keep data on the Wallet objects that we need for + indexing: people, caves and year. So that we can quickly make reports on e.g. all wallets for a particular cave. + All the other fields in the JSON are parsed and loaded from file only dynamiclally, when a report is beging generated, + but since this only happens for a subset of wallets (e.g. for a specific year) the speed penalty is fine. Indeed it + might be faster overall as db operations in django are a bit slow. + This other data is the stuff which generates the tick-lists. + + A trick to minimize the number of times we hit the file to load JSON data is to use a field on every wallet object + called 'JSONdata', but this is not part of the schema and there is no corresponding field in the db. The property + 'JSONdata' only lives for as long as the ephemeral python wallet object. We use this to cache the JSON data so that + queries of several different things, e.g. 'name', or 'survexnotrequired', do not repeatedly re-read the JSON + which has not changed. """ fpath = models.CharField(max_length=200) @@ -86,6 +99,8 @@ class Wallet(models.Model): walletdate = models.DateField(blank=True, null=True) walletyear = models.DateField(blank=True, null=True) caves = models.ManyToManyField("Cave", related_name="wallets") + # not yet + #persons = models.ManyToManyField("Person", related_name="wallets") class Meta: ordering = ("walletname",) @@ -99,9 +114,11 @@ class Wallet(models.Model): Do it every time it is queried, to be sure the result is fresh import DataIssue locally to prevent import cycle problem""" - # jsonfile = Path(self.fpath, 'contents.json') - - # Get from git repo instead + + if hasattr(self, "JSONdata"): + print(f'seen it already {self}') + return self.JSONdata + # :drawings: walletjson/2022/2022#01/contents.json # fpath = /mnt/d/EXPO/expofiles/surveyscans/1999/1999#02 fp = Path(self.fpath) @@ -145,7 +162,7 @@ class Wallet(models.Model): from troggle.core.models.troggle import DataIssue DataIssue.objects.update_or_create(parser="wallets", message=message, url=wurl) - + self.JSONdata = waldata return waldata def check_survexlist(self): |