From fc6e7113c40ec33c57164efbddceee80c934f476 Mon Sep 17 00:00:00 2001 From: Dan McGee Date: Thu, 7 Oct 2010 19:52:31 -0500 Subject: Store package feed last modified date in memcached Just like what we did for news items, we can do the same for packages. Signed-off-by: Dan McGee --- feeds.py | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/feeds.py b/feeds.py index ecb90b68..58c48c38 100644 --- a/feeds.py +++ b/feeds.py @@ -11,23 +11,41 @@ from news.models import News CACHE_TIMEOUT = 1800 +CACHE_PACKAGE_KEY = 'cache_package_latest' CACHE_NEWS_KEY = 'cache_news_latest' -def package_etag(request, *args, **kwargs): - latest = Package.objects.latest('last_update') +def retrieve_package_latest(): + # we could break this down based on the request url, but it would probably + # cost us more in query time to do so. + latest = cache.get(CACHE_PACKAGE_KEY) if latest: - return md5_constructor( - str(kwargs) + str(latest.last_update)).hexdigest() + return latest + try: + latest = Package.objects.values('last_update').latest( + 'last_update')['last_update'] + cache.set(CACHE_PACKAGE_KEY, latest, CACHE_TIMEOUT) + return latest + except Package.DoesNotExist: + pass return None -def package_last_modified(request, *args, **kwargs): - # we could break this down based on the request url, but it would probably - # cost us more in query time to do so. - latest = Package.objects.latest('last_update') +def refresh_package_latest(**kwargs): + # We could delete the value, but that could open a race condition + # where the new data wouldn't have been committed yet by the calling + # thread. Update it instead. + latest = Package.objects.values('last_update').latest( + 'last_update')['last_update'] + cache.set(CACHE_PACKAGE_KEY, latest, CACHE_TIMEOUT) + +def package_etag(request, *args, **kwargs): + latest = retrieve_package_latest() if latest: - return latest.last_update + return md5_constructor(str(kwargs) + str(latest)).hexdigest() return None +def package_last_modified(request, *args, **kwargs): + return retrieve_package_latest() + class PackageFeed(Feed): link = '/packages/' title_template = 'feeds/packages_title.html' @@ -135,6 +153,7 @@ def item_author_name(self, item): return item.author.get_full_name() # connect signals needed to keep cache in line with reality +post_save.connect(refresh_package_latest, sender=Package) post_save.connect(refresh_news_latest, sender=News) # vim: set ts=4 sw=4 et: -- cgit v1.2.3-54-g00ecf