2424from stac_fastapi.core.base_settings import ApiBaseSettings
2525from stac_fastapi.core.datetime_utils import format_datetime_range
2626from stac_fastapi.core.models.links import PagingLinks
27+ from stac_fastapi.core.redis_utils import (
28+ add_previous_link_if_exists,
29+ cache_current_url,
30+ cache_previous_url,
31+ connect_redis,
32+ )
2733from stac_fastapi.core.serializers import CollectionSerializer, ItemSerializer
2834from stac_fastapi.core.session import Session
2935from stac_fastapi.core.utilities import filter_fields
@@ -237,6 +243,13 @@ async def all_collections(self, **kwargs) -> stac_types.Collections:
237243 base_url = str(request.base_url)
238244 limit = int(request.query_params.get("limit", os.getenv("STAC_ITEM_LIMIT", 10)))
239245 token = request.query_params.get("token")
246+ current_url = str(request.url)
247+ redis = None
248+ try:
249+ redis = await connect_redis()
250+ except Exception as e:
251+ logger.error(f"Redis connection error: {e}")
252+ redis = None
240253
241254 collections, next_token = await self.database.get_all_collections(
242255 token=token, limit=limit, request=request
@@ -252,6 +265,12 @@ async def all_collections(self, **kwargs) -> stac_types.Collections:
252265 },
253266 ]
254267
268+ await add_previous_link_if_exists(
269+ redis, links, "collections", current_url, token
270+ )
271+ if redis:
272+ await cache_previous_url(redis, current_url, "collections")
273+
255274 if next_token:
256275 next_link = PagingLinks(next=next_token, request=request).link_next()
257276 links.append(next_link)
@@ -310,20 +329,19 @@ async def item_collection(
310329 """
311330 request: Request = kwargs["request"]
312331 token = request.query_params.get("token")
313- if not hasattr(self, '_prev_links'):
314- self._prev_links = {}
315-
316- session_id = request.cookies.get('stac_session', 'default_session')
317- current_self_link = str(request.url)
318-
319- if session_id not in self._prev_links:
320- self._prev_links[session_id] = []
321-
322- history = self._prev_links[session_id]
323- if not history or current_self_link != history[-1]:
324- history.append(current_self_link)
325332 base_url = str(request.base_url)
326333
334+ current_url = str(request.url)
335+
336+ try:
337+ redis = await connect_redis()
338+ except Exception as e:
339+ logger.error(f"Redis connection error: {e}")
340+ redis = None
341+
342+ if redis:
343+ await cache_current_url(redis, current_url, collection_id)
344+
327345 collection = await self.get_collection(
328346 collection_id=collection_id, request=request
329347 )
@@ -374,21 +392,22 @@ async def item_collection(
374392 "href": urljoin(str(request.base_url), f"collections/{collection_id}"),
375393 },
376394 {
377- "rel": "parent",
395+ "rel": "parent",
378396 "type": "application/json",
379397 "href": urljoin(str(request.base_url), f"collections/{collection_id}"),
380- }
398+ },
381399 ]
382400
383401 paging_links = await PagingLinks(request=request, next=next_token).get_links()
384- history = self._prev_links.get(session_id, [])
385- if len(history) > 1:
386- previous_self_link = history[-2]
387- paging_links.append({
388- "rel": "previous",
389- "type": "application/json",
390- "href": previous_self_link,
391- })
402+
403+ if redis:
404+ await add_previous_link_if_exists(
405+ redis, paging_links, collection_id, current_url, token
406+ )
407+
408+ if redis:
409+ await cache_previous_url(redis, current_url, collection_id)
410+
392411 links = collection_links + paging_links
393412
394413 return stac_types.ItemCollection(
@@ -529,7 +548,15 @@ async def post_search(
529548 HTTPException: If there is an error with the cql2_json filter.
530549 """
531550 base_url = str(request.base_url)
551+ current_url = str(request.url)
552+ try:
553+ redis = await connect_redis()
554+ except Exception as e:
555+ logger.error(f"Redis connection error: {e}")
556+ redis = None
532557
558+ if redis:
559+ await cache_current_url(redis, current_url, "search_result")
533560 search = self.database.make_search()
534561
535562 if search_request.ids:
@@ -628,6 +655,14 @@ async def post_search(
628655 ]
629656 links = await PagingLinks(request=request, next=next_token).get_links()
630657
658+ if redis:
659+ await add_previous_link_if_exists(
660+ redis, links, "search_result", current_url, search_request.token
661+ )
662+
663+ if redis:
664+ await cache_previous_url(redis, current_url, "search_result")
665+
631666 return stac_types.ItemCollection(
632667 type="FeatureCollection",
633668 features=items,
0 commit comments