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 ,
29+ cache_current_url ,
30+ cache_previous_url ,
31+ connect_redis_sentinel ,
32+ connect_redis ,
33+ )
2734from stac_fastapi .core .serializers import CollectionSerializer , ItemSerializer
2835from stac_fastapi .core .session import Session
2936from stac_fastapi .core .utilities import filter_fields
@@ -237,6 +244,12 @@ async def all_collections(self, **kwargs) -> stac_types.Collections:
237244 base_url = str (request .base_url )
238245 limit = int (request .query_params .get ("limit" , os .getenv ("STAC_ITEM_LIMIT" , 10 )))
239246 token = request .query_params .get ("token" )
247+ current_url = str (request .url )
248+ redis = None
249+ try :
250+ redis = await connect_redis ()
251+ except Exception as 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 (
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,18 @@ 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+ redis = None
340+
341+ if redis :
342+ await cache_current_url (redis , current_url , collection_id )
343+
327344 collection = await self .get_collection (
328345 collection_id = collection_id , request = request
329346 )
@@ -374,21 +391,22 @@ async def item_collection(
374391 "href" : urljoin (str (request .base_url ), f"collections/{ collection_id } " ),
375392 },
376393 {
377- "rel" : "parent" ,
394+ "rel" : "parent" ,
378395 "type" : "application/json" ,
379396 "href" : urljoin (str (request .base_url ), f"collections/{ collection_id } " ),
380- }
397+ },
381398 ]
382399
383400 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- })
401+
402+ if redis :
403+ await add_previous_link (
404+ redis , paging_links , collection_id , current_url , token
405+ )
406+
407+ if redis :
408+ await cache_previous_url (redis , current_url , collection_id )
409+
392410 links = collection_links + paging_links
393411
394412 return stac_types .ItemCollection (
@@ -529,7 +547,14 @@ async def post_search(
529547 HTTPException: If there is an error with the cql2_json filter.
530548 """
531549 base_url = str (request .base_url )
550+ current_url = str (request .url )
551+ try :
552+ redis = await connect_redis ()
553+ except Exception as e :
554+ redis = None
532555
556+ if redis :
557+ await cache_current_url (redis , current_url , "search_result" )
533558 search = self .database .make_search ()
534559
535560 if search_request .ids :
@@ -628,6 +653,14 @@ async def post_search(
628653 ]
629654 links = await PagingLinks (request = request , next = next_token ).get_links ()
630655
656+ if redis :
657+ await add_previous_link (
658+ redis , links , "search_result" , current_url , search_request .token
659+ )
660+
661+ if redis :
662+ await cache_previous_url (redis , current_url , "search_result" )
663+
631664 return stac_types .ItemCollection (
632665 type = "FeatureCollection" ,
633666 features = items ,
0 commit comments