Skip to content

Commit 5badee4

Browse files
committed
Use Map Cache
1 parent b57ce48 commit 5badee4

5 files changed

Lines changed: 49 additions & 2 deletions

File tree

recordprocessor/src/clients.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
"""Initialise s3 and kinesis clients"""
22

33
import logging
4+
import os
5+
46
from boto3 import client as boto3_client, resource as boto3_resource
57
from botocore.config import Config
8+
from redis_cacher import RedisCacher
69

7-
REGION_NAME = "eu-west-2"
10+
REGION_NAME = os.getenv("AWS_REGION")
811

912
s3_client = boto3_client("s3", region_name=REGION_NAME)
1013
kinesis_client = boto3_client(
@@ -21,3 +24,7 @@
2124
logging.basicConfig(level="INFO")
2225
logger = logging.getLogger()
2326
logger.setLevel("INFO")
27+
28+
REDIS_HOST = os.getenv("REDIS_HOST")
29+
REDIS_PORT = os.getenv("REDIS_PORT")
30+
redis_cacher = RedisCacher(REDIS_HOST, REDIS_PORT, logger)

recordprocessor/src/constants.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,8 @@ class Urls:
8484
NHS_NUMBER = "https://fhir.nhs.uk/Id/nhs-number"
8585
NULL_FLAVOUR_CODES = "http://terminology.hl7.org/CodeSystem/v3-NullFlavor"
8686
VACCINATION_PROCEDURE = "https://fhir.hl7.org.uk/StructureDefinition/Extension-UKCore-VaccinationProcedure"
87+
88+
89+
class RedisCacheKeys:
90+
PERMISSIONS_CONFIG_FILE_KEY = "permissions_config.json"
91+
DISEASE_MAPPING_FILE_KEY = "disease_mapping.json"

recordprocessor/src/mappings.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
from enum import Enum
44
from typing import Dict, List
5-
from constants import Urls
5+
from constants import Urls, RedisCacheKeys
6+
from redis_cacher import redis_cacher
67

78

89
class Vaccine(Enum):
@@ -54,6 +55,8 @@ class DiseaseDisplayTerm(Enum):
5455
Vaccine.RSV: [Disease.RSV],
5556
}
5657

58+
VACCINE_DISEASE_MAPPING = redis_cacher.get(RedisCacheKeys.DISEASE_MAPPING_FILE_KEY)
59+
5760

5861
def map_target_disease(vaccine: Vaccine) -> list:
5962
"""Returns the target disease element for the given vaccine type using the vaccine_disease_mapping"""
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import json
2+
import redis
3+
4+
5+
class RedisCacher():
6+
""" RedisCacher abstraction class to decouple application code
7+
from direct use of Redis client.
8+
Also centralised error handling & extensibility.
9+
"""
10+
11+
def __init__(self, redis_host, redis_port, logger):
12+
try:
13+
# Attempt to connect to Redis
14+
self.redis_client = redis.StrictRedis(redis_host, redis_port, decode_responses=True)
15+
# Check the connection with a PING command
16+
if self.redis_client.ping():
17+
logger.info("Successfully connected to Redis.")
18+
else:
19+
logger.error("Failed to connect to Redis.")
20+
except Exception as e:
21+
logger.exception(f"Connection to Redis failed: {e}")
22+
23+
def get(self, key: str) -> dict:
24+
"""Gets the value from Redis cache for the given key."""
25+
value = self.redis_client.get(key)
26+
if value is not None:
27+
return json.loads(value)
28+
return {}

redis_sync/src/event_processor.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from clients import logger
22
from s3_event import S3Event
33
from record_processor import record_processor
4+
'''
5+
Event Processor
6+
The Business Logic for the Redis Sync Lambda Function.
7+
This module processes S3 events and iterates through each record to process them individually.'''
48

59

610
def event_processor(event, context):

0 commit comments

Comments
 (0)