Skip to content

Commit

Permalink
Cloudfront cache only clears current app instead of everything
Browse files Browse the repository at this point in the history
  • Loading branch information
edwoodward committed Jun 30, 2022
1 parent 30ef3bb commit ef40003
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 25 deletions.
10 changes: 8 additions & 2 deletions donations/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@
from django.dispatch import receiver

from global_settings.functions import invalidate_cloudfront_caches
from .models import DonationPopup
from .models import DonationPopup, Fundraiser


@receiver(post_save, sender=DonationPopup)
def clear_cloudfront_on_donation_popup_save(sender, **kwargs):
invalidate_cloudfront_caches()
invalidate_cloudfront_caches('donations/donation-popup')


@receiver(post_save, sender=Fundraiser)
def clear_cloudfront_on_fundraiser_save(sender, **kwargs):
invalidate_cloudfront_caches('donations/fundraiser')
2 changes: 1 addition & 1 deletion errata/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def save(self, *args, **kwargs):
self.archived = True

# invalidate cloudfront cache so FE updates (remove when errata is no longer in CMS)
invalidate_cloudfront_caches()
invalidate_cloudfront_caches('errata')

super(Errata, self).save(*args, **kwargs)

Expand Down
41 changes: 28 additions & 13 deletions global_settings/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,37 @@
from botocore.exceptions import NoCredentialsError, ClientError
from .models import CloudfrontDistribution

def invalidate_cloudfront_caches():
def invalidate_cloudfront_caches(path=None):
try:
distribution = CloudfrontDistribution.objects.all()[0]
client = boto3.client('cloudfront')
response = client.create_invalidation(
DistributionId=distribution.distribution_id,
InvalidationBatch={
'Paths': {
'Quantity': 1,
'Items': [
'/apps/cms/api/*' # invalidate the entire cache for the website
],
},
'CallerReference': str(datetime.datetime.now().strftime('%m%d%Y%H%M'))
}
)
if path is None:
response = client.create_invalidation(
DistributionId=distribution.distribution_id,
InvalidationBatch={
'Paths': {
'Quantity': 1,
'Items': [
'/apps/cms/api/*' # invalidate the entire cache for the website
],
},
'CallerReference': str(datetime.datetime.now().strftime('%m%d%Y%H%M'))
}
)
else:
items = '/apps/cms/api/{}*'.format(path)
response = client.create_invalidation(
DistributionId=distribution.distribution_id,
InvalidationBatch={
'Paths': {
'Quantity': 1,
'Items': [
items
],
},
'CallerReference': str(datetime.datetime.now().strftime('%m%d%Y%H%M'))
}
)
except CloudfrontDistribution.DoesNotExist:
return
except IndexError:
Expand Down
14 changes: 10 additions & 4 deletions global_settings/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,26 @@
from .functions import invalidate_cloudfront_caches
from .models import StickyNote, Footer, GiveToday


def clear_cloudfront_on_page_publish(sender, **kwargs):
invalidate_cloudfront_caches()
invalidate_cloudfront_caches('v2/pages')


page_published.connect(clear_cloudfront_on_page_publish)


# These functions clear caches on non-page models that drive content on the website
@receiver(post_save, sender=StickyNote)
def clear_cloudfront_on_sticky_save(sender, **kwargs):
invalidate_cloudfront_caches()
invalidate_cloudfront_caches('sticky')


@receiver(post_save, sender=Footer)
def clear_cloudfront_on_footer_save(sender, **kwargs):
invalidate_cloudfront_caches()
invalidate_cloudfront_caches('footer')


@receiver(post_save, sender=GiveToday)
def clear_cloudfront_on_give_save(sender, **kwargs):
invalidate_cloudfront_caches()
invalidate_cloudfront_caches('give-today')

3 changes: 1 addition & 2 deletions salesforce/management/commands/sync_reviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ def handle(self, *args, **options):
sf.Partner_Review__c.delete(review.review_salesforce_id)
review.delete()


invalidate_cloudfront_caches()
invalidate_cloudfront_caches('salesforce/reviews')
response = self.style.SUCCESS("Successfully updated partner reviews")
self.stdout.write(response)
6 changes: 3 additions & 3 deletions salesforce/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def post(self, request):
serializer = PartnerReviewSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
invalidate_cloudfront_caches()
invalidate_cloudfront_caches('salesforce/partners/')
return JsonResponse(status=201, data=serializer.data)
return JsonResponse(status=400, data="wrong parameters")

Expand All @@ -88,7 +88,7 @@ def patch(self, request):
# set review status to Edited so it will reenter the review queue
review_object.status = 'Edited'
review_object.save()
invalidate_cloudfront_caches()
invalidate_cloudfront_caches('salesforce/partners/')
return JsonResponse(status=201, data=serializer.data)
return JsonResponse(status=400, data="wrong parameters")

Expand All @@ -100,7 +100,7 @@ def delete(self, request):
if user_uuid == str(review_object.submitted_by_account_uuid) or user_uuid == -1: # -1 is returned by get_logged_in_user_uuid when bypass_sso_cookie_check = True
review_object.status = 'Deleted'
review_object.save()
invalidate_cloudfront_caches()
invalidate_cloudfront_caches('salesforce/partners/')
serializer = PartnerReviewSerializer(review_object)
return Response(serializer.data)
else:
Expand Down

0 comments on commit ef40003

Please sign in to comment.