Skip to content

Commit

Permalink
feat: add caching of FC query
Browse files Browse the repository at this point in the history
  • Loading branch information
recalcitrantsupplant committed Nov 15, 2024
1 parent 2ec2f05 commit 29caf56
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion prez/services/listings.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from urllib.parse import urlencode
from zoneinfo import ZoneInfo

from aiocache import cached
from fastapi import Depends
from fastapi.responses import PlainTextResponse
from rdf2geojson import convert
Expand Down Expand Up @@ -175,6 +176,7 @@ async def ogc_features_listing_function(
construct_tss_list.extend(profile_nodeshape.tss_list)

queries = []
fc_item_graph = None
if endpoint_uri_type[0] in [
OGCFEAT["queryables-local"],
OGCFEAT["queryables-global"],
Expand Down Expand Up @@ -259,7 +261,10 @@ async def ogc_features_listing_function(
limit=1,
offset=0,
).to_string()
queries.append(feature_collection_query)
# queries.append(feature_collection_query)
# run the feature_collection_query by itself with caching as it will be the same for all paginated sets of features

fc_item_graph = await _cached_feature_collection_query(collection_uri, data_repo, feature_collection_query)

link_headers = None
if selected_mediatype == "application/sparql-query":
Expand All @@ -270,6 +275,8 @@ async def ogc_features_listing_function(
return content, link_headers

item_graph, _ = await data_repo.send_queries(queries, [])
if fc_item_graph:
item_graph += fc_item_graph
annotations_graph = await return_annotated_rdf(item_graph, data_repo, system_repo)
if count_query:
count_g, _ = await data_repo.send_queries([count_query], [])
Expand Down Expand Up @@ -524,3 +531,12 @@ async def generate_queryables_from_shacl_definition(
"properties": queryable_props,
}
return Queryables(**queryable_params)


@cached(ttl=600, key=lambda collection_uri: collection_uri)
async def _cached_feature_collection_query(collection_uri, data_repo, feature_collection_query):
"""cache the feature collection information for 10 minutes as it is an expensive query at present"""
fc_item_graph, _ = await data_repo.send_queries(
[feature_collection_query], []
)
return fc_item_graph

0 comments on commit 29caf56

Please sign in to comment.