Skip to content

Commit

Permalink
added cache expiry time
Browse files Browse the repository at this point in the history
  • Loading branch information
situx committed Feb 3, 2025
1 parent 749c743 commit 6d112b5
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions tasks/processing/loadtriplestoretask.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
from rdflib import Graph
from qgis.core import Qgis,QgsTask, QgsMessageLog
import os
import time

__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))

MESSAGE_CATEGORY = 'LoadTripleStoreTask'

CACHEDAYS=30

## Loads a graph from an RDF file either by providing an internet address or a file path.
class LoadTripleStoreTask(QgsTask):

Expand All @@ -17,6 +20,11 @@ def __init__(self, description, curtriplestoreconf,endpointIndex,dlg):
self.curtriplestoreconf = curtriplestoreconf
self.dlg=dlg

@staticmethod
def is_file_older_than_x_days(path, days=1):
modtime=os.path.getmtime(path)
return ((time.time() - modtime) / 3600 > 24 * days)

def run(self):
#QgsMessageLog.logMessage('Started task: Serializing loaded graph to '.format(self.description()) + str(self.curtriplestoreconf["resource"]["url"]),
# MESSAGE_CATEGORY, Qgis.Info)
Expand All @@ -28,11 +36,12 @@ def run(self):
path = os.path.join(__location__, "../../tmp/graphcache/" + str(
str(self.curtriplestoreconf["resource"]["url"]).replace("/", "_").replace("['", "")
.replace("']","").replace("\\", "_").replace(":", "_")) + ".ttl")
if os.path.isfile(path):
#QgsMessageLog.logMessage('Started task: Loading graph from file '.format(self.description()) + str(path),MESSAGE_CATEGORY, Qgis.Info)
if os.path.isfile(path) and not LoadTripleStoreTask.is_file_older_than_x_days(path,CACHEDAYS):
QgsMessageLog.logMessage('FILE IS PRESENT AND NOT OLDER THAN '+str(CACHEDAYS),MESSAGE_CATEGORY, Qgis.Info)
self.graph.parse(path)
self.curtriplestoreconf["instance"]=self.graph
else:
QgsMessageLog.logMessage('FILE IS NOT PRESENT OR OLDER THAN '+str(CACHEDAYS),MESSAGE_CATEGORY, Qgis.Info)
#QgsMessageLog.logMessage('Started task: Loading graph from URI '.format(self.description()) +self.curtriplestoreconf["resource"]["url"],
# MESSAGE_CATEGORY, Qgis.Info)
SPARQLUtils.loadGraph(self.curtriplestoreconf["resource"]["url"],self.graph)
Expand Down

0 comments on commit 6d112b5

Please sign in to comment.