Skip to content

Commit 65223e2

Browse files
Yuri ZmytrakovYuri Zmytrakov
authored andcommitted
temp
1 parent cb7ee7c commit 65223e2

File tree

2 files changed

+52
-67
lines changed

2 files changed

+52
-67
lines changed

stac_fastapi/core/stac_fastapi/core/core.py

Lines changed: 14 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
from stac_fastapi.core.base_settings import ApiBaseSettings
2525
from stac_fastapi.core.datetime_utils import format_datetime_range
2626
from stac_fastapi.core.models.links import PagingLinks
27-
from stac_fastapi.core.redis_utils import connect_redis, get_prev_link, save_self_link
27+
from stac_fastapi.core.redis_utils import handle_pagination_links
2828
from stac_fastapi.core.serializers import CollectionSerializer, ItemSerializer
2929
from stac_fastapi.core.session import Session
30-
from stac_fastapi.core.utilities import filter_fields, get_bool_env
30+
from stac_fastapi.core.utilities import filter_fields
3131
from stac_fastapi.extensions.core.transaction import AsyncBaseTransactionsClient
3232
from stac_fastapi.extensions.core.transaction.request import (
3333
PartialCollection,
@@ -329,20 +329,6 @@ async def all_collections(
329329
if parsed_sort:
330330
sort = parsed_sort
331331

332-
current_url = str(request.url)
333-
redis_enable = get_bool_env("REDIS_ENABLE", default=False)
334-
335-
redis = None
336-
if redis_enable:
337-
try:
338-
redis = await connect_redis()
339-
logger.info("Redis connection established successfully")
340-
except Exception as e:
341-
redis = None
342-
logger.warning(
343-
f"Redis connection failed, continuing without Redis: {e}"
344-
)
345-
346332
# Convert q to a list if it's a string
347333
q_list = None
348334
if q is not None:
@@ -441,21 +427,10 @@ async def all_collections(
441427
},
442428
]
443429

444-
if redis_enable and redis:
445-
if next_token:
446-
await save_self_link(redis, next_token, current_url)
447-
448-
prev_link = await get_prev_link(redis, token)
449-
if prev_link:
450-
links.insert(
451-
0,
452-
{
453-
"rel": "prev",
454-
"type": "application/json",
455-
"method": "GET",
456-
"href": prev_link,
457-
},
458-
)
430+
current_url = str(request.url)
431+
await handle_pagination_links(
432+
current_url=current_url, token=token, next_token=next_token, links=links
433+
)
459434

460435
if next_token:
461436
next_link = PagingLinks(next=next_token, request=request).link_next()
@@ -775,7 +750,7 @@ async def post_search(
775750
HTTPException: If there is an error with the cql2_json filter.
776751
"""
777752
base_url = str(request.base_url)
778-
redis_enable = get_bool_env("REDIS_ENABLE", default=False)
753+
# redis_enable = get_bool_env("REDIS_ENABLE", default=False)
779754

780755
search = self.database.make_search()
781756

@@ -901,29 +876,13 @@ async def post_search(
901876
)
902877
links.extend(collection_links)
903878

904-
if redis_enable:
905-
redis = None
906-
try:
907-
redis = await connect_redis()
908-
logger.info("Redis connection established successfully")
909-
self_link = str(request.url)
910-
await save_self_link(redis, next_token, self_link)
911-
912-
prev_link = await get_prev_link(redis, token_param)
913-
if prev_link:
914-
links.insert(
915-
0,
916-
{
917-
"rel": "prev",
918-
"type": "application/json",
919-
"method": "GET",
920-
"href": prev_link,
921-
},
922-
)
923-
except Exception as e:
924-
logger.warning(
925-
f"Redis connection failed, continuing without Redis: {e}"
926-
)
879+
current_url = str(request.url)
880+
await handle_pagination_links(
881+
current_url=current_url,
882+
token=token_param,
883+
next_token=next_token,
884+
links=links,
885+
)
927886

928887
return stac_types.ItemCollection(
929888
type="FeatureCollection",

stac_fastapi/core/stac_fastapi/core/redis_utils.py

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
"""Utilities for connecting to and managing Redis connections."""
22

3+
import logging
34
from typing import Optional
45

56
from pydantic_settings import BaseSettings
67
from redis import asyncio as aioredis
78
from redis.asyncio.sentinel import Sentinel
89

10+
from stac_fastapi.core.utilities import get_bool_env
11+
912
redis_pool: Optional[aioredis.Redis] = None
1013

14+
logger = logging.getLogger(__name__)
15+
1116

1217
class RedisSentinelSettings(BaseSettings):
1318
"""Configuration for connecting to Redis Sentinel."""
@@ -109,16 +114,37 @@ async def connect_redis_sentinel(
109114
return redis_pool
110115

111116

112-
async def save_self_link(
113-
redis: aioredis.Redis, token: Optional[str], self_href: str
117+
async def handle_pagination_links(
118+
current_url: str, token: str, next_token: str, links: list
114119
) -> None:
115-
"""Save the self link for the current token with 30 min TTL."""
116-
if token:
117-
await redis.setex(f"nav:self:{token}", 1800, self_href)
118-
119-
120-
async def get_prev_link(redis: aioredis.Redis, token: Optional[str]) -> Optional[str]:
121-
"""Get the previous page link for the current token (if exists)."""
122-
if not token:
123-
return None
124-
return await redis.get(f"nav:self:{token}")
120+
"""Handle Redis pagination."""
121+
redis_enable = get_bool_env("REDIS_ENABLE", default=False)
122+
redis = None
123+
if redis_enable:
124+
try:
125+
redis = await connect_redis()
126+
logger.info("Redis connection established successfully")
127+
except Exception as e:
128+
redis = None
129+
logger.warning(f"Redis connection failed, continuing without Redis: {e}")
130+
131+
if redis_enable and redis:
132+
# Save self link for next token (replaces save_self_link)
133+
if next_token:
134+
await redis.setex(f"nav:self:{next_token}", 1800, current_url)
135+
136+
# Get previous link for current token (replaces get_prev_link)
137+
prev_link = None
138+
if token:
139+
prev_link = await redis.get(f"nav:self:{token}")
140+
141+
if prev_link:
142+
links.insert(
143+
0,
144+
{
145+
"rel": "prev",
146+
"type": "application/json",
147+
"method": "GET",
148+
"href": prev_link,
149+
},
150+
)

0 commit comments

Comments
 (0)