-
Notifications
You must be signed in to change notification settings - Fork 31
feat: Add Redis caching for navigation pagination #488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
d6d268e
52a57ff
cb7ee7c
ac1d2e4
072592d
dbfe1b2
fe0efb2
18339d6
7b238f2
0a022e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -117,4 +117,4 @@ docs-image: | |
| .PHONY: docs | ||
| docs: docs-image | ||
| docker compose -f compose.docs.yml \ | ||
| run docs | ||
| run docs | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,9 +24,10 @@ | |
| from stac_fastapi.core.base_settings import ApiBaseSettings | ||
| from stac_fastapi.core.datetime_utils import format_datetime_range | ||
| from stac_fastapi.core.models.links import PagingLinks | ||
| from stac_fastapi.core.redis_utils import redis_pagination_links | ||
| from stac_fastapi.core.serializers import CollectionSerializer, ItemSerializer | ||
| from stac_fastapi.core.session import Session | ||
| from stac_fastapi.core.utilities import filter_fields | ||
| from stac_fastapi.core.utilities import filter_fields, get_bool_env | ||
| from stac_fastapi.extensions.core.transaction import AsyncBaseTransactionsClient | ||
| from stac_fastapi.extensions.core.transaction.request import ( | ||
| PartialCollection, | ||
|
|
@@ -262,6 +263,7 @@ async def all_collections( | |
| A Collections object containing all the collections in the database and links to various resources. | ||
| """ | ||
| base_url = str(request.base_url) | ||
| redis_enable = get_bool_env("REDIS_ENABLE", default=False) | ||
|
|
||
| global_max_limit = ( | ||
| int(os.getenv("STAC_GLOBAL_COLLECTION_MAX_LIMIT")) | ||
|
|
@@ -417,6 +419,14 @@ async def all_collections( | |
| }, | ||
| ] | ||
|
|
||
| if redis_enable: | ||
| await redis_pagination_links( | ||
| current_url=str(request.url), | ||
| token=token, | ||
| next_token=next_token, | ||
| links=links, | ||
| ) | ||
|
|
||
| if next_token: | ||
| next_link = PagingLinks(next=next_token, request=request).link_next() | ||
| links.append(next_link) | ||
|
|
@@ -761,8 +771,8 @@ async def post_search( | |
| search_request.limit = limit | ||
|
|
||
| base_url = str(request.base_url) | ||
|
|
||
| search = self.database.make_search() | ||
| redis_enable = get_bool_env("REDIS_ENABLE", default=False) | ||
|
|
||
| if search_request.ids: | ||
| search = self.database.apply_ids_filter( | ||
|
|
@@ -866,6 +876,34 @@ async def post_search( | |
| ] | ||
| links = await PagingLinks(request=request, next=next_token).get_links() | ||
|
|
||
| collection_links = [] | ||
| # Add "collection" and "parent" rels only for /collections/{collection_id}/items | ||
| if search_request.collections and "/items" in str(request.url): | ||
| for collection_id in search_request.collections: | ||
| collection_links.extend( | ||
| [ | ||
| { | ||
| "rel": "collection", | ||
| "type": "application/json", | ||
| "href": urljoin(base_url, f"collections/{collection_id}"), | ||
| }, | ||
| { | ||
| "rel": "parent", | ||
| "type": "application/json", | ||
| "href": urljoin(base_url, f"collections/{collection_id}"), | ||
| }, | ||
| ] | ||
| ) | ||
| links.extend(collection_links) | ||
|
|
||
| if redis_enable: | ||
| await redis_pagination_links( | ||
| current_url=str(request.url), | ||
| token=token_param, | ||
| next_token=next_token, | ||
| links=links, | ||
| ) | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can this code - the redis_enabled block - be put into a function? It is used with all_collections too There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code block was replaced here with func as well. Thank you! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's change the function name for handle_pagination_links - also, mentioned below - and let's check the REDIS IS ENABLED here before we go to the function. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, thank you! |
||
| return stac_types.ItemCollection( | ||
| type="FeatureCollection", | ||
| features=items, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this code into a function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jonhealy1 I have moved this code into
handle_pagination_linksfunc that can be reused where needed.