Skip to content

Commit ae79a1e

Browse files
Yuri ZmytrakovYuri Zmytrakov
authored andcommitted
feat: Add Redis caching for navigation
Implement Redis caching to support proper pagination navigation in STAC FastAPI. - Adds Redis configuration for both Sentinel and standalone Redis setups - Caches pagination tokens - Enables prev/next links in paginated responses Environment variables provided for flexible deployment configurations.
1 parent 57afb55 commit ae79a1e

File tree

5 files changed

+194
-1
lines changed

5 files changed

+194
-1
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ repos:
3131
]
3232
additional_dependencies: [
3333
"types-attrs",
34-
"types-requests"
34+
"types-requests",
35+
"types-redis"
3536
]
3637
- repo: https://github.com/PyCQA/pydocstyle
3738
rev: 6.1.1

dockerfiles/Dockerfile.dev.es

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ COPY . /app
1818
RUN pip install --no-cache-dir -e ./stac_fastapi/core
1919
RUN pip install --no-cache-dir -e ./stac_fastapi/sfeos_helpers
2020
RUN pip install --no-cache-dir -e ./stac_fastapi/elasticsearch[dev,server]
21+
RUN pip install --no-cache-dir redis types-redis

stac_fastapi/core/setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"pygeofilter~=0.3.1",
2020
"jsonschema~=4.0.0",
2121
"slowapi~=0.1.9",
22+
"redis==6.4.0",
2223
]
2324

2425
setup(

stac_fastapi/core/stac_fastapi/core/core.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
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 (
28+
connect_redis_sentinel,
29+
get_prev_link,
30+
save_self_link,
31+
)
2732
from stac_fastapi.core.serializers import CollectionSerializer, ItemSerializer
2833
from stac_fastapi.core.session import Session
2934
from stac_fastapi.core.utilities import filter_fields
@@ -276,6 +281,13 @@ async def all_collections(
276281
if q is not None:
277282
q_list = [q] if isinstance(q, str) else q
278283

284+
current_url = str(request.url)
285+
redis = None
286+
try:
287+
redis = await connect_redis_sentinel()
288+
except Exception:
289+
redis = None
290+
279291
collections, next_token = await self.database.get_all_collections(
280292
token=token, limit=limit, request=request, sort=sort, q=q_list
281293
)
@@ -299,6 +311,22 @@ async def all_collections(
299311
},
300312
]
301313

314+
if redis:
315+
if next_token:
316+
await save_self_link(redis, next_token, current_url)
317+
318+
prev_link = await get_prev_link(redis, token)
319+
if prev_link:
320+
links.insert(
321+
0,
322+
{
323+
"rel": "prev",
324+
"type": "application/json",
325+
"method": "GET",
326+
"href": prev_link,
327+
},
328+
)
329+
302330
if next_token:
303331
next_link = PagingLinks(next=next_token, request=request).link_next()
304332
links.append(next_link)
@@ -529,6 +557,10 @@ async def post_search(
529557
HTTPException: If there is an error with the cql2_json filter.
530558
"""
531559
base_url = str(request.base_url)
560+
try:
561+
redis = await connect_redis_sentinel()
562+
except Exception:
563+
redis = None
532564

533565
search = self.database.make_search()
534566

@@ -639,6 +671,41 @@ async def post_search(
639671
]
640672
links = await PagingLinks(request=request, next=next_token).get_links()
641673

674+
collection_links = []
675+
if search_request.collections:
676+
for collection_id in search_request.collections:
677+
collection_links.extend(
678+
[
679+
{
680+
"rel": "collection",
681+
"type": "application/json",
682+
"href": urljoin(base_url, f"collections/{collection_id}"),
683+
},
684+
{
685+
"rel": "parent",
686+
"type": "application/json",
687+
"href": urljoin(base_url, f"collections/{collection_id}"),
688+
},
689+
]
690+
)
691+
links.extend(collection_links)
692+
693+
if redis:
694+
self_link = str(request.url)
695+
await save_self_link(redis, next_token, self_link)
696+
697+
prev_link = await get_prev_link(redis, token_param)
698+
if prev_link:
699+
links.insert(
700+
0,
701+
{
702+
"rel": "prev",
703+
"type": "application/json",
704+
"method": "GET",
705+
"href": prev_link,
706+
},
707+
)
708+
642709
return stac_types.ItemCollection(
643710
type="FeatureCollection",
644711
features=items,
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
"""Utilities for connecting to and managing Redis connections."""
2+
3+
from typing import Optional
4+
5+
from pydantic_settings import BaseSettings
6+
from redis import asyncio as aioredis
7+
from redis.asyncio.sentinel import Sentinel
8+
9+
redis_pool: Optional[aioredis.Redis] = None
10+
11+
12+
class RedisSentinelSettings(BaseSettings):
13+
"""Configuration for connecting to Redis Sentinel."""
14+
15+
REDIS_SENTINEL_HOSTS: str = ""
16+
REDIS_SENTINEL_PORTS: str = "26379"
17+
REDIS_SENTINEL_MASTER_NAME: str = "master"
18+
REDIS_DB: int = 0
19+
20+
REDIS_MAX_CONNECTIONS: int = 10
21+
REDIS_RETRY_TIMEOUT: bool = True
22+
REDIS_DECODE_RESPONSES: bool = True
23+
REDIS_CLIENT_NAME: str = "stac-fastapi-app"
24+
REDIS_HEALTH_CHECK_INTERVAL: int = 30
25+
26+
27+
class RedisSettings(BaseSettings):
28+
"""Configuration for connecting Redis."""
29+
30+
REDIS_HOST: str = ""
31+
REDIS_PORT: int = 6379
32+
REDIS_DB: int = 0
33+
34+
REDIS_MAX_CONNECTIONS: int = 10
35+
REDIS_RETRY_TIMEOUT: bool = True
36+
REDIS_DECODE_RESPONSES: bool = True
37+
REDIS_CLIENT_NAME: str = "stac-fastapi-app"
38+
REDIS_HEALTH_CHECK_INTERVAL: int = 30
39+
40+
41+
# Select the Redis or Redis Sentinel configuration
42+
redis_settings: BaseSettings = RedisSentinelSettings()
43+
44+
45+
async def connect_redis_sentinel(
46+
settings: Optional[RedisSentinelSettings] = None,
47+
) -> Optional[aioredis.Redis]:
48+
"""Return Redis Sentinel connection."""
49+
global redis_pool
50+
settings = settings or redis_settings
51+
52+
if (
53+
not settings.REDIS_SENTINEL_HOSTS
54+
or not settings.REDIS_SENTINEL_PORTS
55+
or not settings.REDIS_SENTINEL_MASTER_NAME
56+
):
57+
return None
58+
59+
hosts = [h.strip() for h in settings.REDIS_SENTINEL_HOSTS.split(",") if h.strip()]
60+
ports = [
61+
int(p.strip()) for p in settings.REDIS_SENTINEL_PORTS.split(",") if p.strip()
62+
]
63+
64+
if redis_pool is None:
65+
try:
66+
sentinel = Sentinel(
67+
[(host, port) for host, port in zip(hosts, ports)],
68+
decode_responses=settings.REDIS_DECODE_RESPONSES,
69+
)
70+
master = sentinel.master_for(
71+
service_name=settings.REDIS_SENTINEL_MASTER_NAME,
72+
db=settings.REDIS_DB,
73+
decode_responses=settings.REDIS_DECODE_RESPONSES,
74+
retry_on_timeout=settings.REDIS_RETRY_TIMEOUT,
75+
client_name=settings.REDIS_CLIENT_NAME,
76+
max_connections=settings.REDIS_MAX_CONNECTIONS,
77+
health_check_interval=settings.REDIS_HEALTH_CHECK_INTERVAL,
78+
)
79+
redis_pool = master
80+
81+
except Exception:
82+
return None
83+
84+
return redis_pool
85+
86+
87+
async def connect_redis(settings: Optional[RedisSettings] = None) -> aioredis.Redis:
88+
"""Return Redis connection."""
89+
global redis_pool
90+
settings = settings or redis_settings
91+
92+
if not settings.REDIS_HOST or not settings.REDIS_PORT:
93+
return None
94+
95+
if redis_pool is None:
96+
pool = aioredis.ConnectionPool(
97+
host=settings.REDIS_HOST,
98+
port=settings.REDIS_PORT,
99+
db=settings.REDIS_DB,
100+
max_connections=settings.REDIS_MAX_CONNECTIONS,
101+
decode_responses=settings.REDIS_DECODE_RESPONSES,
102+
retry_on_timeout=settings.REDIS_RETRY_TIMEOUT,
103+
health_check_interval=settings.REDIS_HEALTH_CHECK_INTERVAL,
104+
)
105+
redis_pool = aioredis.Redis(
106+
connection_pool=pool, client_name=settings.REDIS_CLIENT_NAME
107+
)
108+
return redis_pool
109+
110+
111+
async def save_self_link(
112+
redis: aioredis.Redis, token: Optional[str], self_href: str
113+
) -> None:
114+
"""Add the self link for next page as prev link for the current token."""
115+
if token:
116+
await redis.setex(f"nav:self:{token}", 1800, self_href)
117+
118+
119+
async def get_prev_link(redis: aioredis.Redis, token: Optional[str]) -> Optional[str]:
120+
"""Pull the prev page link for the current token."""
121+
if not token:
122+
return None
123+
return await redis.get(f"nav:self:{token}")

0 commit comments

Comments
 (0)