Cache provider lookups for 2 seconds

For every email or text message we send we have to work out which
provider to send it with. Every time we do this we go and load the list
of providers from the database.

For emails, the result will always be the same.

For text messages the result is randomly chosen to balance the load
between the providers.

For international text messages the result is always the same (we only
have one international text message provider).

This commit adds an in-memory cache with a 2 second TTL so that we’re
not fetching the providers from the database every time, which should
speed things up a bit.

This does mean that, for text messages, the random choice will ‘stick’
for two seconds on each instance, before being re-chosen. I think this
is  OK because it will even out to the same distribution over time.

I really don’t like having to clear the cache in the tests, so would
welcome suggestions on a better way of doing this…
This commit is contained in:
Chris Hill-Scott
2020-12-23 15:33:27 +00:00
parent a2bb775b6f
commit 51b7192750
2 changed files with 26 additions and 1 deletions

View File

@@ -1,7 +1,7 @@
import random
from urllib import parse
from datetime import datetime, timedelta
from cachetools import TTLCache, cached
from flask import current_app
from notifications_utils.recipients import (
validate_and_format_phone_number,
@@ -148,6 +148,10 @@ def update_notification_to_sending(notification, provider):
dao_update_notification(notification)
provider_cache = TTLCache(maxsize=8, ttl=2)
@cached(cache=provider_cache)
def provider_to_use(notification_type, international=False):
active_providers = [
p for p in get_provider_details_by_notification_type(notification_type, international) if p.active