Skip to content

Commit 93c4ef6

Browse files
committed
Cache improvements and 15 min TTL
1 parent 7751e7c commit 93c4ef6

File tree

3 files changed

+22
-30
lines changed

3 files changed

+22
-30
lines changed

src/app.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@
6363
requests.packages.urllib3.disable_warnings(category = InsecureRequestWarning)
6464

6565
# For performance improvement and not overloading the server, especially helpful during Elasticsearch index/reindex
66-
# We use the cache as long as it exists
67-
# Only clear the expired one based on TTL setting passively upon lookup rather than the actual expiration time
68-
# This approach takes advantage of the cache and also prevents from memory overflow
6966
entity_cache = {}
7067

7168

@@ -4009,20 +4006,18 @@ def query_target_entity(id, user_token):
40094006
current_datetime = datetime.now()
40104007
current_timestamp = int(round(current_datetime.timestamp()))
40114008

4012-
# Use the cached data as long as it exists, no matter if it's expired or not
4013-
# Otherwise make a fresh request and add to the cache pool
4014-
# But we'll clear the expired cache based on TTL setting passively
4015-
if id in entity_cache:
4016-
entity_dict = entity_cache[id]['entity']
4009+
# Check if the cached entity is found and still valid based on TTL upon request, delete if expired
4010+
if (id in entity_cache) and (current_timestamp > entity_cache[id]['created_timestamp'] + SchemaConstants.REQUEST_CACHE_TTL):
4011+
del entity_cache[id]
40174012

4018-
# We can still return the cache entity even if it's expired.
4019-
# Just need to clear the expired data from cache so the next lookup makes a new call and stores the new data to cache
4020-
if current_timestamp <= entity_cache[id]['created_timestamp'] + SchemaConstants.REQUEST_CACHE_TTL:
4021-
logger.info(f'Using the entity data of {id} from cache at time {current_datetime}')
4022-
else:
4023-
logger.info(f'Using the entity data of {id} from cache at time {current_datetime} then clear it based on TTL setting')
4013+
logger.info(f'Deleted the expired entity cache of {id} at time {current_datetime}')
4014+
4015+
# Use the cached data if found and still valid
4016+
# Otherwise, make a fresh query and add to cache
4017+
if (id in entity_cache) and (current_timestamp <= entity_cache[id]['created_timestamp'] + SchemaConstants.REQUEST_CACHE_TTL):
4018+
entity_dict = entity_cache[id]['entity']
40244019

4025-
del entity_cache[id]
4020+
logger.info(f'Using the valid cache data of entity {id} at time {current_datetime}')
40264021
else:
40274022
logger.info(f'Cache not found or expired. Making a new query to retrieve {id} at time {current_datetime}')
40284023

src/schema/schema_constants.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class SchemaConstants(object):
2-
# Expire the request cache after the time-to-live (seconds), default 4 hours
3-
REQUEST_CACHE_TTL = 14400
2+
# Expire the request cache after the time-to-live (seconds), default 15 minutes
3+
REQUEST_CACHE_TTL = 900
44

55
# Constants used by validators
66
INGEST_API_APP = 'ingest-api'

src/schema/schema_manager.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@
3232
_neo4j_driver = None
3333

3434
# For handling cached requests to uuid-api and external static resources (github raw yaml files)
35-
# We use the cache as long as it exists
36-
# Only clear the expired one based on TTL setting passively upon lookup rather than the actual expiration time
37-
# This approach takes advantage of the cache and also prevents from memory overflow
3835
request_cache = {}
3936

4037

@@ -1557,18 +1554,18 @@ def make_request_get(target_url, internal_token_used = False):
15571554
current_datetime = datetime.now()
15581555
current_timestamp = int(round(current_datetime.timestamp()))
15591556

1560-
# Use the cached data as long as it exists, no matter if it's expired or not
1561-
# Otherwise make a fresh request and add to the cache pool
1562-
# But we'll clear the expired cache based on TTL setting passively
1563-
if target_url in request_cache:
1564-
response = request_cache[target_url]['response']
1557+
# Check if the cached response is found and still valid based on TTL upon request, delete if expired
1558+
if (target_url in request_cache) and (current_timestamp > request_cache[target_url]['created_timestamp'] + SchemaConstants.REQUEST_CACHE_TTL):
1559+
del request_cache[target_url]
15651560

1566-
if current_timestamp <= request_cache[target_url]['created_timestamp'] + SchemaConstants.REQUEST_CACHE_TTL:
1567-
logger.info(f'Using the cached HTTP response of GET {target_url} at time {current_datetime}')
1568-
else:
1569-
logger.info(f'Using the cached HTTP response of GET {target_url} at time {current_datetime} then clear it based on TTL setting')
1561+
logger.info(f'Deleted the expired HTTP response cache of GET {target_url} at time {current_datetime}')
1562+
1563+
# Use the cached data if found and still valid
1564+
# Otherwise, make a fresh query and add to cache
1565+
if (target_url in request_cache) and (current_timestamp <= request_cache[target_url]['created_timestamp'] + SchemaConstants.REQUEST_CACHE_TTL):
1566+
response = request_cache[target_url]['response']
15701567

1571-
del request_cache[target_url]
1568+
logger.info(f'Using the cached HTTP response of GET {target_url} at time {current_datetime}')
15721569
else:
15731570
logger.info(f'Cache not found or expired. Making a new HTTP request of GET {target_url} at time {current_datetime}')
15741571

0 commit comments

Comments
 (0)