22
33
44from django .conf import settings
5+ from django .core .cache import cache
56from django .core .exceptions import ImproperlyConfigured
67from django .core .urlresolvers import reverse
78from django .views .generic import FormView
89from django .views .generic .base import TemplateView
10+ from functools import wraps
911from pythonkc_meetups import PythonKCMeetups
1012from pythonkc_site .forms import ContactForm
13+ import logging
14+
15+
16+ logger = logging .getLogger (__name__ )
1117
1218
1319try :
@@ -33,15 +39,16 @@ def form_valid(self, form):
3339 return super (PythonKCHome , self ).form_valid (form )
3440
3541 def get_context_data (self , ** kwargs ):
36- # NOTE Instead of individually caching next/past events, we're
37- # simply going to cache this whole page until things get more
38- # complicated (see urls.py for caching decoration).
3942
43+ @memoize_in_cache ('next_event' , 60 * 5 )
4044 def get_next_event ():
45+ logging .info ('Retrieving next event from Meetup.com' )
4146 upcoming_events = meetups .get_upcoming_events ()
4247 return upcoming_events [0 ] if upcoming_events else None
4348
49+ @memoize_in_cache ('past_events' , 60 * 60 )
4450 def get_past_events ():
51+ logging .info ('Retrieving past events from Meetup.com' )
4552 return meetups .get_past_events ()[:num_past_events ]
4653
4754 return {
@@ -51,5 +58,19 @@ def get_past_events():
5158 }
5259
5360
61+ def memoize_in_cache (cache_key , cache_timeout_seconds = None ):
62+ """Cache the result of a no-args function."""
63+ def decorator (func ):
64+ @wraps (func )
65+ def decorated ():
66+ value = cache .get (cache_key )
67+ if not value :
68+ value = func ()
69+ cache .set (cache_key , value , cache_timeout_seconds )
70+ return value
71+ return decorated
72+ return decorator
73+
74+
5475class PythonKCComingSoon (TemplateView ):
5576 template_name = 'coming_soon.html'
0 commit comments