2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2024-11-25 08:41:51 +00:00

make cache work even if timestamps wrong

This commit is contained in:
Philip Sargent 2022-10-05 21:10:05 +03:00
parent a6e60c0bf7
commit 9e5bdace2c

View File

@ -585,15 +585,19 @@ def LoadLogbookForExpedition(expedition):
expect = entries[year] expect = entries[year]
# print(" - Logbook for: " + year) # print(" - Logbook for: " + year)
def validcache(year,n): def validcache(year,n, lbsize):
if year != expedition: if year != expedition:
print(" ! year != expedition ",year, expedition ) print(" ! cache loading: year != expedition ",year, expedition )
return False
currentsize = logbookpath.stat().st_size
if lbsize != currentsize:
print(f" ! cache loading: Logbook size {lbsize} != {currentsize} ")
return False return False
if len(logentries) != n: if len(logentries) != n:
print(" ! len(logentries) != n ",len(logentries), n ) print(" ! cache loading: len(logentries) != n ",len(logentries), n )
return False return False
if n != expect: if n != expect:
print(" ! n != expect ",n, expect ) print(" ! cache loading: n != expect ",n, expect )
return False return False
return True return True
@ -652,15 +656,15 @@ def LoadLogbookForExpedition(expedition):
try: try:
# print(" - Reading cache: " + str(cache_filename), end='') # print(" - Reading cache: " + str(cache_filename), end='')
with open(cache_filename, "rb") as f: with open(cache_filename, "rb") as f:
year,n,logentries = pickle.load(f) year, lbsize, n, logentries = pickle.load(f)
if validcache(year,n): if validcache(year, n, lbsize):
print(f" -- {year} : Loaded {len(logentries)} log entries") print(f" -- {year} : Loaded {len(logentries)} log entries")
logbook_cached = True logbook_cached = True
else: else:
print(" !- Told to expect ", expect, " but ", len(logentries), " found in cache") print(" !- {year} : Cache failed validity checks")
raise raise
except: except:
print(" ! Failed to load corrupt cache. (Or I was told to ignore it). Deleting it.") print(" ! Failed to load corrupt cache (or I was told to ignore it). Deleting it.")
os.remove(cache_filename) os.remove(cache_filename)
logentries=[] logentries=[]
raise raise
@ -684,8 +688,9 @@ def LoadLogbookForExpedition(expedition):
SetDatesFromLogbookEntries(expedition) SetDatesFromLogbookEntries(expedition)
if len(logentries) >0: if len(logentries) >0:
print(" - Cacheing " , len(logentries), " log entries") print(" - Cacheing " , len(logentries), " log entries")
lbsize = logbookpath.stat().st_size
with open(cache_filename, "wb") as fc: # we much check that permission are g+w ! or expo can't delete the cache with open(cache_filename, "wb") as fc: # we much check that permission are g+w ! or expo can't delete the cache
logbk=(expedition,len(logentries),logentries) logbk=(expedition,lbsize,len(logentries),logentries)
pickle.dump(logbk, fc, protocol=4) pickle.dump(logbk, fc, protocol=4)
else: else:
print(" ! NO TRIP entries found in logbook, check the syntax.") print(" ! NO TRIP entries found in logbook, check the syntax.")
@ -737,7 +742,7 @@ def LoadLogbooks():
expd ={} expd ={}
actuals = [] actuals = []
for expo in expos: for expo in expos: # pointless as we explicitly know the years in this code.
year = expo.year year = expo.year
TROG['pagecache']['expedition'][year] = None # clear cache TROG['pagecache']['expedition'][year] = None # clear cache
if year in sqlfail: if year in sqlfail: