Skip to content

Commit

Permalink
Merge pull request #844 from uktrade/develop
Browse files Browse the repository at this point in the history
Staging release
  • Loading branch information
richtier authored Jun 12, 2020
2 parents f1f0c06 + b6cd19b commit d4b5247
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
32 changes: 16 additions & 16 deletions core/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,23 +277,23 @@ def subscribe(cls):
post_migrate.connect(receiver=cls.clear)


class CountryPagesCache:
class MarketPagesCache:
cache = cache

@staticmethod
def build_key(country, industry):
return f'countryguide_{country}{industry}'
def build_key(region, industry):
return f'countryguide_{region}{industry}'

@classmethod
def set(cls, data, country=None, industry=None,):
key = cls.build_key(country=country, industry=industry)
def set(cls, data, region=None, industry=None,):
key = cls.build_key(region=region, industry=industry)
cls.cache.set(key, data, timeout=settings.API_CACHE_EXPIRE_SECONDS)

@classmethod
def get_many(cls, industries=[None], countries=[None]):
keys = [cls.build_key(*args) for args in itertools.product(countries, industries)]
pages = {}
# making the values returned distint
# making the values returned distinct
for records in cls.cache.get_many(keys).values():
for record in records:
pages[record['id']] = record
Expand All @@ -314,31 +314,31 @@ def __exit__(self, *args):
return Transaction()


class CountryPageCachePopulator:
class MarketPagesCachePopulator:

@classmethod
def populate(cls, *args, **kwargs):
pages = collections.defaultdict(list)
serializer_class = MODELS_SERIALIZERS_MAPPING[CountryGuidePage]

# store the record four times: so can be filtered by country+industry, country, industry, or no filter
for page in CountryGuidePage.objects.select_related('country').live():
# store the record four times: so can be filtered by region+industry, region, industry, or no filter
for page in CountryGuidePage.objects.select_related('country__region').live():
serializer = serializer_class(instance=page)

# allows retrieve with no filter
pages[(None, None)].append(serializer.data)
for industry in page.tags.values_list('name', flat=True):
if page.country:
# allow retrieve with both filters
pages[(industry, page.country.name)].append(serializer.data)
# allow retrieve with country
pages[(None, page.country.name)].append(serializer.data)
pages[(industry, page.country.region.name)].append(serializer.data)
# allow retrieve with region
pages[(None, page.country.region.name)].append(serializer.data)
# allow retrieve with industry
pages[(industry, None)].append(serializer.data)

with CountryPagesCache.transaction() as country_cache:
for (industry, country), data in pages.items():
country_cache.set(data, country=country, industry=industry)
with MarketPagesCache.transaction() as market_cache:
for (industry, region), data in pages.items():
market_cache.set(data, region=region, industry=industry)


class DatabaseCacheSubscriber:
Expand Down Expand Up @@ -374,4 +374,4 @@ def rebuild_all_cache():
for page in Page.objects.live().specific():
if page.__class__ in MODELS_SERIALIZERS_MAPPING and page.__class__ is not Page:
CachePopulator.populate_async(page)
CountryPageCachePopulator.populate()
MarketPagesCachePopulator.populate()
4 changes: 2 additions & 2 deletions export_readiness/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from conf.signature import SignatureCheckPermission
from export_readiness import serializers, snippets
from core.cache import CountryPagesCache
from core.cache import MarketPagesCache

THIRTY_MINUTES_IN_SECONDS = 30 * 60

Expand Down Expand Up @@ -38,7 +38,7 @@ def get(self, *args, **kwargs):
filters['industries'] = industries.split(',')
if countries:
filters['countries'] = countries.split(',')
cached_content = CountryPagesCache.get_many(**filters)
cached_content = MarketPagesCache.get_many(**filters)
return Response(cached_content or [], content_type='application/json')


Expand Down
24 changes: 12 additions & 12 deletions tests/export_readiness/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from tests.export_readiness import factories
from directory_constants import urls
from core.cache import CountryPageCachePopulator
from core.cache import MarketPagesCachePopulator


@pytest.mark.django_db
Expand Down Expand Up @@ -232,7 +232,7 @@ def test_lookup_market_guides_missing_filters(client):
market2 = factories.CountryGuidePageFactory()
market2.tags = [tag]
market2.save_revision().publish()
CountryPageCachePopulator.populate()
MarketPagesCachePopulator.populate()

url = reverse('api:lookup-country-guides-list-view')

Expand All @@ -251,7 +251,7 @@ def test_lookup_market_guides_missing_region(client):
market2 = factories.CountryGuidePageFactory()
market2.tags = [tag]
market2.save_revision().publish()
CountryPageCachePopulator.populate()
MarketPagesCachePopulator.populate()

url = reverse('api:lookup-country-guides-list-view')

Expand All @@ -274,7 +274,7 @@ def test_lookup_market_guides_missing_region_markets_have_region(client):
market2.save_revision().publish()
factories.CountryGuidePageFactory()

CountryPageCachePopulator.populate()
MarketPagesCachePopulator.populate()

url = reverse('api:lookup-country-guides-list-view')
response = client.get(f'{url}?industry={tag.name},{tag2.name}')
Expand All @@ -287,8 +287,8 @@ def test_lookup_market_guides_missing_region_markets_have_region(client):
@pytest.mark.django_db
def test_lookup_market_guides_all_filters_no_cache(client):
tag = factories.IndustryTagFactory(name='aerospace')
region = factories.RegionFactory()
country = factories.CountryFactory(name='France', region=region)
region = factories.RegionFactory(name='europe')
country = factories.CountryFactory(region=region)
market1 = factories.CountryGuidePageFactory(country=country)
market1.tags = [tag]
market1.save_revision().publish()
Expand All @@ -298,8 +298,8 @@ def test_lookup_market_guides_all_filters_no_cache(client):
market2.save_revision()
url = reverse('api:lookup-country-guides-list-view')

CountryPageCachePopulator.populate()
response = client.get(f'{url}?industry={tag.name}&region={country.name}')
MarketPagesCachePopulator.populate()
response = client.get(f'{url}?industry={tag.name}&region={region.name}')
assert response.status_code == 200
assert len(response.json()) == 1
assert response.json()[0]['id'] == market1.pk
Expand All @@ -308,18 +308,18 @@ def test_lookup_market_guides_all_filters_no_cache(client):
@pytest.mark.django_db
def test_lookup_market_guides_all_filters_cache(client):
tag = factories.IndustryTagFactory(name='aerospace')
region = factories.RegionFactory()
country = factories.CountryFactory(name='France', region=region)
region = factories.RegionFactory(name='europe')
country = factories.CountryFactory(region=region)
market1 = factories.CountryGuidePageFactory(country=country)
market1.tags = [tag]
market1.save_revision().publish()

factories.CountryGuidePageFactory(country=country)

CountryPageCachePopulator.populate()
MarketPagesCachePopulator.populate()
url = reverse('api:lookup-country-guides-list-view')

response = client.get(f'{url}?industry={tag.name}&region={country.name}')
response = client.get(f'{url}?industry={tag.name}&region={region.name}')
assert response.status_code == 200
assert response.content_type == 'application/json'
parsed = response.json()
Expand Down

0 comments on commit d4b5247

Please sign in to comment.