Compare commits

...

2 Commits

Author SHA1 Message Date
Paul Harrison abd36240e3 feat: Add hand cache 2023-05-16 19:20:58 +01:00
Paul Harrison 19ba2375a7 fix: Typo. 2023-05-16 19:16:43 +01:00
5 changed files with 86 additions and 2 deletions

33
poetry.lock generated
View File

@ -21,6 +21,18 @@ doc = ["packaging", "sphinx-autodoc-typehints (>=1.2.0)", "sphinx-rtd-theme"]
test = ["contextlib2", "coverage[toml] (>=4.5)", "hypothesis (>=4.0)", "mock (>=4)", "pytest (>=7.0)", "pytest-mock (>=3.6.1)", "trustme", "uvloop (<0.15)", "uvloop (>=0.15)"] test = ["contextlib2", "coverage[toml] (>=4.5)", "hypothesis (>=4.0)", "mock (>=4)", "pytest (>=7.0)", "pytest-mock (>=3.6.1)", "trustme", "uvloop (<0.15)", "uvloop (>=0.15)"]
trio = ["trio (>=0.16,<0.22)"] trio = ["trio (>=0.16,<0.22)"]
[[package]]
name = "async-timeout"
version = "4.0.2"
description = "Timeout context manager for asyncio programs"
category = "main"
optional = false
python-versions = ">=3.6"
files = [
{file = "async-timeout-4.0.2.tar.gz", hash = "sha256:2163e1640ddb52b7a8c80d0a67a08587e5d245cc9c553a74a847056bc2976b15"},
{file = "async_timeout-4.0.2-py3-none-any.whl", hash = "sha256:8ca1e4fcf50d07413d66d1a5e416e42cfdf5851c981d679a09851a6853383b3c"},
]
[[package]] [[package]]
name = "attrs" name = "attrs"
version = "23.1.0" version = "23.1.0"
@ -671,6 +683,25 @@ pluggy = ">=0.12,<2.0"
[package.extras] [package.extras]
testing = ["argcomplete", "attrs (>=19.2.0)", "hypothesis (>=3.56)", "mock", "nose", "pygments (>=2.7.2)", "requests", "xmlschema"] testing = ["argcomplete", "attrs (>=19.2.0)", "hypothesis (>=3.56)", "mock", "nose", "pygments (>=2.7.2)", "requests", "xmlschema"]
[[package]]
name = "redis"
version = "4.5.5"
description = "Python client for Redis database and key-value store"
category = "main"
optional = false
python-versions = ">=3.7"
files = [
{file = "redis-4.5.5-py3-none-any.whl", hash = "sha256:77929bc7f5dab9adf3acba2d3bb7d7658f1e0c2f1cafe7eb36434e751c471119"},
{file = "redis-4.5.5.tar.gz", hash = "sha256:dc87a0bdef6c8bfe1ef1e1c40be7034390c2ae02d92dcd0c7ca1729443899880"},
]
[package.dependencies]
async-timeout = {version = ">=4.0.2", markers = "python_full_version <= \"3.11.2\""}
[package.extras]
hiredis = ["hiredis (>=1.0.0)"]
ocsp = ["cryptography (>=36.0.1)", "pyopenssl (==20.0.1)", "requests (>=2.26.0)"]
[[package]] [[package]]
name = "rfc3986" name = "rfc3986"
version = "1.5.0" version = "1.5.0"
@ -780,4 +811,4 @@ dev = ["black (>=19.3b0)", "pytest (>=4.6.2)"]
[metadata] [metadata]
lock-version = "2.0" lock-version = "2.0"
python-versions = ">=3.11,<3.12" python-versions = ">=3.11,<3.12"
content-hash = "fb62fc026dc64b0f6707d666c39635b19459935e84947efdcbd7de5f68c70ff8" content-hash = "31c2219942b27c557028971055b425a867f575c736c5c282593f712f1cc0fa77"

38
poker/cache.py Normal file
View File

@ -0,0 +1,38 @@
from redis import Redis
from poker.constants import Rank
from poker.models import Hand
from poker.settings import CacheSettings
def get_redis_connection(settings: CacheSettings | None = None) -> Redis:
"""Get Redis connection."""
if settings is None:
settings = CacheSettings()
return Redis.from_url(settings.url)
class Cache:
"""Poker hands cache.
TODO: Only cache sorted hands.
"""
def __init__(self, conn: Redis | None = None) -> None:
"""Initialize cache."""
if conn is None:
self._conn = get_redis_connection()
else:
self._conn = conn
def get_rank(self, hand: Hand) -> Rank | None:
"""Get ranked hand from cache."""
key = str(hand)
rank = self._conn.get(key)
if rank is None:
return None
return Rank(rank)
def cache_rank(self, hand: Hand, rank: Rank) -> None:
"""Cache ranked hand."""
self._conn.set(str(hand), str(rank))

View File

@ -52,7 +52,7 @@ class Hand(BaseModel):
class RankedHand(Hand): class RankedHand(Hand):
"""Ranked hand domain model class.""" """Ranked hand domain model class."""
rank: Rank rank: Rank
description: str description: str

14
poker/settings.py Normal file
View File

@ -0,0 +1,14 @@
from pydantic import BaseSettings, RedisDsn
class CacheSettings(BaseSettings):
"""Cache settings."""
url: RedisDsn
class Config:
"""Settings configuration."""
env_prefix = "cache_"
env_file = ".env"
env_file_encoding = "utf-8"

View File

@ -11,6 +11,7 @@ fastapi = "^0.87.0"
loguru = "^0.6.0" loguru = "^0.6.0"
pydantic = "^1.10.2" pydantic = "^1.10.2"
uvicorn = "^0.20.0" uvicorn = "^0.20.0"
redis = "^4.5.5"
[tool.poetry.group.test.dependencies] [tool.poetry.group.test.dependencies]
coverage = "^6.5.0" coverage = "^6.5.0"