Permalink
Browse files

Moved caching logic inside "calculate_user_values" method.

  • Loading branch information...
BYK committed Dec 28, 2011
1 parent 08983f8 commit 3e6526459901f56734b72d54984c7621166f4c6a
Showing with 24 additions and 22 deletions.
  1. +24 −22 app/core.py
View
@@ -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:
@@ -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')
@@ -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)

0 comments on commit 3e65264

Please sign in to comment.