From 50e79a9e1a7a8a8251ce345a93f1baeec28af13f Mon Sep 17 00:00:00 2001 From: richtier Date: Fri, 12 Jun 2020 14:29:59 +0100 Subject: [PATCH] Cache market pages on region not country --- core/cache.py | 32 ++++++++++++++-------------- export_readiness/views.py | 4 ++-- tests/export_readiness/test_views.py | 24 ++++++++++----------- 3 files changed, 30 insertions(+), 30 deletions(-) diff --git a/core/cache.py b/core/cache.py index ee2524ce..87daeb58 100644 --- a/core/cache.py +++ b/core/cache.py @@ -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 @@ -314,15 +314,15 @@ 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 @@ -330,15 +330,15 @@ def populate(cls, *args, **kwargs): 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: @@ -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() diff --git a/export_readiness/views.py b/export_readiness/views.py index 34f5f0d4..6cca48a8 100644 --- a/export_readiness/views.py +++ b/export_readiness/views.py @@ -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 @@ -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') diff --git a/tests/export_readiness/test_views.py b/tests/export_readiness/test_views.py index fa698b67..32a53fb4 100644 --- a/tests/export_readiness/test_views.py +++ b/tests/export_readiness/test_views.py @@ -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 @@ -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') @@ -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') @@ -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}') @@ -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() @@ -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}®ion={country.name}') + MarketPagesCachePopulator.populate() + response = client.get(f'{url}?industry={tag.name}®ion={region.name}') assert response.status_code == 200 assert len(response.json()) == 1 assert response.json()[0]['id'] == market1.pk @@ -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}®ion={country.name}') + response = client.get(f'{url}?industry={tag.name}®ion={region.name}') assert response.status_code == 200 assert response.content_type == 'application/json' parsed = response.json()