Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Query cache #108

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions master/requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ pydantic
boto3[crt]
boto3-stubs[dynamodb]
click
cachetools
types-cachetools
4 changes: 4 additions & 0 deletions master/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ botocore==1.27.59
# s3transfer
botocore-stubs==1.27.59
# via boto3-stubs
cachetools==5.2.0
# via -r ./master/requirements.in
click==8.1.3
# via -r ./master/requirements.in
jmespath==1.0.1
Expand All @@ -32,6 +34,8 @@ six==1.16.0
# via python-dateutil
types-awscrt==0.14.0
# via botocore-stubs
types-cachetools==5.2.1
# via -r ./master/requirements.in
types-s3transfer==0.6.0.post4
# via boto3-stubs
typing-extensions==4.3.0
Expand Down
11 changes: 9 additions & 2 deletions master/storage/backends/dynamodb.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import logging
import os
from decimal import Decimal
from threading import Lock
from typing import Any, Mapping, Optional, Sequence, Union

import cachetools
from boto3.dynamodb.conditions import Attr
from boto3.session import Session
from mypy_boto3_dynamodb.service_resource import DynamoDBServiceResource, Table
from mypy_boto3_dynamodb.type_defs import (
GetItemOutputTableTypeDef,
PutItemOutputTableTypeDef,
ScanOutputTableTypeDef,
UpdateItemOutputTableTypeDef,
)
Expand Down Expand Up @@ -37,6 +37,8 @@
],
]

# TODO: maxsize=number of active protocols
CACHE = cachetools.TTLCache(maxsize=3, ttl=60)
DEFAULT_SESSION = Session()
DEFAULT_TABLE_NAME = f"{APP_NAME}-{DEPLOYMENT_ENVIRONMENT}"

Expand Down Expand Up @@ -120,6 +122,11 @@ def get_servers(self, game: Optional[str] = None) -> list[Server]:

return servers

@cachetools.cached(
cache=CACHE,
key=cachetools.keys.methodkey,
lock=Lock(),
)
def _scan(self, game: Optional[str] = None) -> list[Item]:
response: ScanOutputTableTypeDef
if game:
Expand Down
4 changes: 4 additions & 0 deletions requirements.test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ botocore-stubs==1.27.59
# boto3-stubs
build==0.8.0
# via pip-tools
cachetools==5.2.0
# via -r ././master/requirements.txt
cattrs==22.1.0
# via
# -r ././deployment/requirements.txt
Expand Down Expand Up @@ -259,6 +261,8 @@ types-awscrt==0.14.0
# via
# -r ././master/requirements.txt
# botocore-stubs
types-cachetools==5.2.1
# via -r ././master/requirements.txt
types-s3transfer==0.6.0.post4
# via
# -r ././master/requirements.txt
Expand Down