Skip to content

Commit

Permalink
API access time improvements (#424)
Browse files Browse the repository at this point in the history
* drop s3 head call as this turns out to be heavy

* also enable cache for the get endpoints
  • Loading branch information
Ziyang Liu committed Feb 9, 2022
1 parent 23beede commit 1fa522a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 22 deletions.
6 changes: 2 additions & 4 deletions .happy/terraform/modules/api-gateway-proxy-stage/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@ resource aws_api_gateway_stage api_stage {
resource aws_api_gateway_method_settings api_gateway_stage_setting {
rest_api_id = var.rest_api_id
stage_name = aws_api_gateway_stage.api_stage.stage_name
method_path = "*/*"
method_path = "{proxy+}/GET"

settings {
data_trace_enabled = false
metrics_enabled = false
logging_level = "OFF"
caching_enabled = true
}
}
20 changes: 2 additions & 18 deletions backend/api/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,16 @@
s3_client = boto3.client("s3", endpoint_url=endpoint_url, config=Config(max_pool_connections=50))


def cache_available(key: str) -> bool:
"""
Check if cache is available for the key.
:param key: key to check in s3
:return: True iff cache exists
"""
if bucket is None:
return False
try:
s3_client.head_object(Bucket=bucket, Key=os.path.join(bucket_path, key))
return True
except ClientError:
return False


def get_cache(key: str) -> Union[Dict, List, None]:
"""
Get the cached file for a given key if exists, None otherwise.
:param key: key to the cache to get
:return: file content for the key if exists, None otherwise
"""
if cache_available(key):
try:
return json.loads(s3_client.get_object(Bucket=bucket, Key=os.path.join(bucket_path, key))['Body'].read())
else:
except ClientError:
print(f"Not cached: {key}")
return None

Expand Down

0 comments on commit 1fa522a

Please sign in to comment.