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 ,
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,12 @@ 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 :
251+ redis = None
240252
241253 collections , next_token = await self .database .get_all_collections (
242254 token = token , limit = limit , request = request
@@ -252,6 +264,10 @@ async def all_collections(self, **kwargs) -> stac_types.Collections:
252264 },
253265 ]
254266
267+ await add_previous_link (redis , links , "collections" , current_url , token )
268+ if redis :
269+ await cache_previous_url (redis , current_url , "collections" )
270+
255271 if next_token :
256272 next_link = PagingLinks (next = next_token , request = request ).link_next ()
257273 links .append (next_link )
@@ -310,20 +326,18 @@ async def item_collection(
310326 """
311327 request : Request = kwargs ["request" ]
312328 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 )
325329 base_url = str (request .base_url )
326330
331+ current_url = str (request .url )
332+
333+ try :
334+ redis = await connect_redis ()
335+ except Exception :
336+ redis = None
337+
338+ if redis :
339+ await cache_current_url (redis , current_url , collection_id )
340+
327341 collection = await self .get_collection (
328342 collection_id = collection_id , request = request
329343 )
@@ -374,21 +388,22 @@ async def item_collection(
374388 "href" : urljoin (str (request .base_url ), f"collections/{ collection_id } " ),
375389 },
376390 {
377- "rel" : "parent" ,
391+ "rel" : "parent" ,
378392 "type" : "application/json" ,
379393 "href" : urljoin (str (request .base_url ), f"collections/{ collection_id } " ),
380- }
394+ },
381395 ]
382396
383397 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- })
398+
399+ if redis :
400+ await add_previous_link (
401+ redis , paging_links , collection_id , current_url , token
402+ )
403+
404+ if redis :
405+ await cache_previous_url (redis , current_url , collection_id )
406+
392407 links = collection_links + paging_links
393408
394409 return stac_types .ItemCollection (
@@ -529,7 +544,14 @@ async def post_search(
529544 HTTPException: If there is an error with the cql2_json filter.
530545 """
531546 base_url = str (request .base_url )
547+ current_url = str (request .url )
548+ try :
549+ redis = await connect_redis ()
550+ except Exception :
551+ redis = None
532552
553+ if redis :
554+ await cache_current_url (redis , current_url , "search_result" )
533555 search = self .database .make_search ()
534556
535557 if search_request .ids :
@@ -628,6 +650,14 @@ async def post_search(
628650 ]
629651 links = await PagingLinks (request = request , next = next_token ).get_links ()
630652
653+ if redis :
654+ await add_previous_link (
655+ redis , links , "search_result" , current_url , search_request .token
656+ )
657+
658+ if redis :
659+ await cache_previous_url (redis , current_url , "search_result" )
660+
631661 return stac_types .ItemCollection (
632662 type = "FeatureCollection" ,
633663 features = items ,
0 commit comments