-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DISCO-2028] Cache weather reports in Redis (#202)
- Loading branch information
Showing
21 changed files
with
811 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Caching support.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"""No-operation adapter that disables caching.""" | ||
|
||
from datetime import timedelta | ||
from typing import Optional, Union | ||
|
||
|
||
class NoCacheAdapter: # pragma: no cover | ||
"""A cache adapter that doesn't store or return anything.""" | ||
|
||
async def get(self, key: str) -> Optional[bytes]: # noqa: D102 | ||
return None | ||
|
||
async def set( | ||
self, | ||
key: str, | ||
value: Union[bytes, str], | ||
ttl: Optional[timedelta] = None, | ||
) -> None: # noqa: D102 | ||
pass | ||
|
||
async def close(self) -> None: # noqa: D102 | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
"""Protocol for cache adapters.""" | ||
|
||
from datetime import timedelta | ||
from typing import Optional, Protocol | ||
|
||
|
||
class CacheAdapter(Protocol): | ||
"""A protocol describing a cache backend.""" | ||
|
||
async def get(self, key: str) -> Optional[bytes]: # pragma: no cover | ||
"""Get the value associated with the key. Returns `None` if the key isn't in the cache. | ||
Raises: | ||
- `CacheAdapterError` for cache backend errors. | ||
""" | ||
... | ||
|
||
async def set( | ||
self, | ||
key: str, | ||
value: bytes, | ||
ttl: Optional[timedelta] = None, | ||
) -> None: # pragma: no cover | ||
"""Store a key-value pair in the cache, with an optional time-to-live. | ||
Raises: | ||
- `CacheAdapterError` for cache backend errors. | ||
""" | ||
... | ||
|
||
async def close(self) -> None: # pragma: no cover | ||
"""Close the adapter and release any underlying resources.""" | ||
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
"""Redis cache adapter.""" | ||
|
||
from datetime import timedelta | ||
from typing import Optional | ||
|
||
from redis.asyncio import Redis, RedisError | ||
|
||
from merino.exceptions import CacheAdapterError | ||
|
||
|
||
class RedisAdapter: | ||
"""A cache adapter that stores key-value pairs in Redis.""" | ||
|
||
redis: Redis | ||
|
||
def __init__(self, redis: Redis): | ||
self.redis = redis | ||
|
||
async def get(self, key: str) -> Optional[bytes]: | ||
"""Get the value associated with the key from Redis. Returns `None` if the key isn't in | ||
Redis. | ||
Raises: | ||
- `CacheAdapterError` if Redis returns an error. | ||
""" | ||
try: | ||
return await self.redis.get(key) | ||
except RedisError as exc: | ||
raise CacheAdapterError( | ||
f"Failed to get `{repr(key)}` with error: `{exc}`" | ||
) from exc | ||
|
||
async def set( | ||
self, | ||
key: str, | ||
value: bytes, | ||
ttl: Optional[timedelta] = None, | ||
) -> None: | ||
"""Store a key-value pair in Redis, overwriting the previous value if set, and optionally | ||
expiring after the time-to-live. | ||
Raises: | ||
- `CacheAdapterError` if Redis returns an error. | ||
""" | ||
try: | ||
await self.redis.set(key, value, ex=ttl.seconds if ttl else None) | ||
except RedisError as exc: | ||
raise CacheAdapterError( | ||
f"Failed to set `{repr(key)}` with error: `{exc}`" | ||
) from exc | ||
|
||
async def close(self) -> None: | ||
"""Close the Redis connection.""" | ||
await self.redis.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.