-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #23 from alan-turing-institute/22-make-redis-optional
Make Redis optional
- Loading branch information
Showing
9 changed files
with
126 additions
and
48 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
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 |
---|---|---|
@@ -1,5 +1,9 @@ | ||
from .local_cache import LocalCache | ||
from .redis_cache import RedisCache | ||
from .uid_cache import UidCache | ||
|
||
__all__ = [ | ||
"LocalCache", | ||
"RedisCache", | ||
"UidCache", | ||
] |
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,18 @@ | ||
from .uid_cache import UidCache | ||
|
||
|
||
class LocalCache(UidCache): | ||
def __init__(self) -> None: | ||
self.cache: dict[str, int] = {} | ||
|
||
def get(self, identifier: str) -> int | None: | ||
return self.cache.get(identifier, None) | ||
|
||
def keys(self) -> list[str]: | ||
return [str(k) for k in self.cache.keys()] | ||
|
||
def set(self, identifier: str, uid_value: int) -> None: | ||
self.cache[identifier] = uid_value | ||
|
||
def values(self, keys: list[str]) -> list[int]: | ||
return [v for k, v in self.cache.items() if k in keys] |
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,36 @@ | ||
from typing import cast | ||
|
||
import redis | ||
|
||
from .uid_cache import UidCache | ||
|
||
|
||
class RedisCache(UidCache): | ||
def __init__(self, redis_host: str, redis_port: int) -> None: | ||
self.redis_host = redis_host | ||
self.redis_port = redis_port | ||
self.cache_: "redis.Redis[str]" | None = None | ||
|
||
@property | ||
def cache(self) -> "redis.Redis[str]": | ||
""" | ||
Lazy-load the cache on request | ||
""" | ||
if not self.cache_: | ||
self.cache_ = redis.Redis( | ||
host=self.redis_host, port=self.redis_port, decode_responses=True | ||
) | ||
return self.cache_ | ||
|
||
def get(self, identifier: str) -> int | None: | ||
value = self.cache.get(identifier) | ||
return None if value is None else int(value) | ||
|
||
def keys(self) -> list[str]: | ||
return [str(k) for k in self.cache.keys()] | ||
|
||
def set(self, identifier: str, uid_value: int) -> None: | ||
self.cache.set(identifier, uid_value) | ||
|
||
def values(self, keys: list[str]) -> list[int]: | ||
return [int(cast(str, v)) for v in self.cache.mget(keys)] |
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