Cache enabled for 'expedition' pages

This commit is contained in:
Philip Sargent 2021-04-10 01:07:49 +01:00
parent 16a6e05849
commit 6dc54adec8
2 changed files with 40 additions and 7 deletions

View File

@ -32,7 +32,11 @@ There are more subclasses define in models_caves.py models_survex.py etc.
# This variable is a dictionary holding gloablly visible indexes and cache functions.
# It is a Global Object, see https://python-patterns.guide/python/module-globals/
# troggle.models.TROG
TROG = {}
TROG = {
'pagecache' : {
'expedition' : {}
}
}
def get_process_memory():
usage=resource.getrusage(resource.RUSAGE_SELF)

View File

@ -12,10 +12,9 @@ from django.template.defaultfilters import slugify
from django.utils import timezone
from django.views.generic.list import ListView
import troggle.core.models as models
import troggle.parsers.logbooks as logbookparsers
#import troggle.parsers.logbooks as logbookparsers
from troggle.core.forms import getTripForm # , get_name, PersonForm
from troggle.core.models import Expedition, Person, PersonExpedition
from troggle.core.models import Expedition, Person, PersonExpedition, TROG
from troggle.core.models_caves import LogbookEntry, PersonTrip
from troggle.core.models_survex import SurvexBlock
from .login import login_required_if_public
@ -24,6 +23,11 @@ from troggle.parsers.people import GetPersonExpeditionNameLookup
import troggle.settings as settings
'''These views are for logbook items when they appear in an 'expedition' page
and for persons: their individual pages and their perseonexpedition pages.
It uses the global object TROG to hold some cached pages.
'''
def getNotablePersons():
notablepersons = []
@ -51,7 +55,27 @@ def personindex(request):
def expedition(request, expeditionname):
'''Returns a rendered page for one expedition, specified by the year e.g. '2019'.
If page caching is enabled, it caches the dictionaries used to render the template page.
The cache is refreshed if '?reload' is present in the requesting URL, which also re-parses the logbook.
By specifying a '0' for the expected number of entries in the logbook cache, this forces the parser to
re-parse the original logbook HTML file.
'''
if "reload" in request.GET:
this_expedition = Expedition.objects.get(year=int(expeditionname))
LoadLogbookForExpedition(this_expedition, 0)
ts = TROG['pagecache']['expedition']
if settings.CACHEDPAGES:
nexpos = len( TROG['pagecache']['expedition'])
#print(f'! - expo {expeditionname} CACHEDPAGES {nexpos} expo pages in cache.')
if expeditionname in ts:
#print('! - expo {expeditionanme} using cached page')
return render(request,'expedition.html', ts[expeditionname] )
this_expedition = Expedition.objects.get(year=int(expeditionname))
expeditions = Expedition.objects.all()
personexpeditiondays = [ ]
dateditems = list(this_expedition.logbookentry_set.all()) + list(this_expedition.survexblock_set.all())
@ -66,9 +90,14 @@ def expedition(request, expeditionname):
prow.append(pcell)
personexpeditiondays.append({"personexpedition":personexpedition, "personrow":prow})
if "reload" in request.GET:
LoadLogbookForExpedition(this_expedition)
return render(request,'expedition.html', {'expedition': this_expedition, 'expeditions':expeditions, 'personexpeditiondays':personexpeditiondays, 'settings':settings, 'dateditems': dateditems })
ts[expeditionname] = {'expedition': this_expedition, 'expeditions':expeditions,
'personexpeditiondays':personexpeditiondays, 'settings':settings,
'dateditems': dateditems }
TROG['pagecache']['expedition'][expeditionname] = ts[expeditionname]
nexpos = len( TROG['pagecache']['expedition'])
#print(f'! - expo {expeditionname} pre-render N expos:{nexpos}')
return render(request,'expedition.html', ts[expeditionname] )
def get_absolute_url(self):