Skip to content

Commit

Permalink
Moved caching logic inside "calculate_user_values" method.
Browse files Browse the repository at this point in the history
  • Loading branch information
BYK committed Dec 28, 2011
1 parent 08983f8 commit 3e65264
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions app/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ def get_option(self, name, defval):
return False if self.request.get(name, defval) == '0' else True

def calculate_user_values(self, username):
memcache_data_key = '!data!{}'.format(username)
values = json.loads(memcache.get(memcache_data_key) or '{}')
if values:
return values

try:
github_user = User.get(username)
except pyresto.Error:
Expand Down Expand Up @@ -134,19 +139,25 @@ def calculate_user_values(self, username):
),
)

return {'user': github_user.__dict__,
'own_repos': github_user.public_repos - fork_count,
'fork_repos': fork_count,
'languages': languages,
'project_followers': github_user.project_followers -\
github_user.public_repos,
'commit_sparkline': commit_sparkline,
'max_commits': max_commits,
'last_project': last_project,
'last_project_url': last_project_url,
'days': RECENT_DAYS
values = {'user': github_user.__dict__,
'own_repos': github_user.public_repos - fork_count,
'fork_repos': fork_count,
'languages': languages,
'project_followers': github_user.project_followers -\
github_user.public_repos,
'commit_sparkline': commit_sparkline,
'max_commits': max_commits,
'last_project': last_project,
'last_project_url': last_project_url,
'days': RECENT_DAYS
}

if not memcache.set(memcache_data_key, json.dumps(values),
MEMCACHE_EXPIRATION):
logging.error('Memcache set failed for user data %s', username)

return values

def get(self, username):
support = self.get_option('s', '0')
analytics = self.get_option('a', '1')
Expand All @@ -165,19 +176,10 @@ def get(self, username):
if cached_data:
return self.write(cached_data)
else:
memcache_data_key = '!data!{}'.format(username)
values = json.loads(memcache.get(memcache_data_key) or '{}')
if not values:
# Caution, the method below may alter state.
values = self.calculate_user_values(username)

if not values: # still don't have the values, something went wrong
values = self.calculate_user_values(username)
if not values: # don't have the values, something went wrong
return

if not memcache.set(memcache_data_key,
json.dumps(values), MEMCACHE_EXPIRATION):
logging.error('Memcache set failed for user data %s', username)

if jsonp:
output = '{0}({1})'.format(jsonp, json.dumps(values))
self.write(output)
Expand Down

0 comments on commit 3e65264

Please sign in to comment.