-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
282 additions
and
1 deletion.
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
Empty file.
23 changes: 23 additions & 0 deletions
23
app/entities/redis_repositories/category_point_redis_repository.py
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,23 @@ | ||
from typing import Sequence | ||
|
||
from app.entities.category.category_codes import CategoryCode | ||
from app.utils.redis_ import redis | ||
|
||
|
||
class CategoryPointRedisRepository: | ||
@classmethod | ||
async def get(cls, key: str) -> tuple[CategoryCode, ...] | None: | ||
cached = await redis.get(key) | ||
if cached is None: | ||
return None | ||
if cached == "": | ||
return () | ||
return tuple(CategoryCode(code) for code in cached.split(",")) | ||
|
||
@classmethod | ||
async def set(cls, key: str, codes: Sequence[CategoryCode]) -> None: | ||
await redis.set(key, ",".join(codes)) | ||
|
||
@classmethod | ||
async def delete(cls, *key: str) -> None: | ||
await redis.delete(*key) |
Empty file.
41 changes: 41 additions & 0 deletions
41
app/tests/entities/redis_repositories/test_category_point_redis_repository.py
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,41 @@ | ||
from app.entities.category.category_codes import CategoryCode | ||
from app.entities.redis_repositories.category_point_redis_repository import ( | ||
CategoryPointRedisRepository, | ||
) | ||
|
||
|
||
async def test_category_point_set_and_get() -> None: | ||
# Given | ||
key = "1_2" | ||
codes = (CategoryCode.CHICKEN, CategoryCode.PIZZA) | ||
|
||
# When | ||
await CategoryPointRedisRepository.set(key, codes) | ||
|
||
# Then | ||
assert await CategoryPointRedisRepository.get(key) == codes | ||
|
||
|
||
async def test_category_point_delete() -> None: | ||
# Given | ||
key = "1_2" | ||
codes = (CategoryCode.CHICKEN, CategoryCode.PIZZA) | ||
await CategoryPointRedisRepository.set(key, codes) | ||
|
||
# When | ||
await CategoryPointRedisRepository.delete(key) | ||
|
||
# Then | ||
assert await CategoryPointRedisRepository.get(key) is None | ||
|
||
|
||
async def test_category_point_set_and_get_empty() -> None: | ||
# Given | ||
key = "1_2" | ||
codes = () | ||
|
||
# When | ||
await CategoryPointRedisRepository.set(key, codes) | ||
|
||
# Then | ||
assert await CategoryPointRedisRepository.get(key) == codes |
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,12 @@ | ||
import os | ||
|
||
from redis.asyncio import ConnectionError, Redis | ||
|
||
# https://github.com/python/typeshed/issues/7597#issuecomment-1117572695 | ||
redis: "Redis[str]" = Redis.from_url( | ||
os.environ.get("REDIS_URL", "redis://localhost:6379"), | ||
decode_responses=True, # redis.get() 이 byte 대신 str 을 리턴합니다. | ||
socket_timeout=5, | ||
retry_on_timeout=True, # 타임아웃 발생시 재시도 합니다. | ||
retry_on_error=[ConnectionError], # 커넥션 에러가 발생 했을 때 재시도 합니다. | ||
) |
Large diffs are not rendered by default.
Oops, something went wrong.
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