Skip to content

Commit 3e65264

Browse files
committed
Moved caching logic inside "calculate_user_values" method.
1 parent 08983f8 commit 3e65264

1 file changed

Lines changed: 24 additions & 22 deletions

File tree

app/core.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ def get_option(self, name, defval):
8282
return False if self.request.get(name, defval) == '0' else True
8383

8484
def calculate_user_values(self, username):
85+
memcache_data_key = '!data!{}'.format(username)
86+
values = json.loads(memcache.get(memcache_data_key) or '{}')
87+
if values:
88+
return values
89+
8590
try:
8691
github_user = User.get(username)
8792
except pyresto.Error:
@@ -134,19 +139,25 @@ def calculate_user_values(self, username):
134139
),
135140
)
136141

137-
return {'user': github_user.__dict__,
138-
'own_repos': github_user.public_repos - fork_count,
139-
'fork_repos': fork_count,
140-
'languages': languages,
141-
'project_followers': github_user.project_followers -\
142-
github_user.public_repos,
143-
'commit_sparkline': commit_sparkline,
144-
'max_commits': max_commits,
145-
'last_project': last_project,
146-
'last_project_url': last_project_url,
147-
'days': RECENT_DAYS
142+
values = {'user': github_user.__dict__,
143+
'own_repos': github_user.public_repos - fork_count,
144+
'fork_repos': fork_count,
145+
'languages': languages,
146+
'project_followers': github_user.project_followers -\
147+
github_user.public_repos,
148+
'commit_sparkline': commit_sparkline,
149+
'max_commits': max_commits,
150+
'last_project': last_project,
151+
'last_project_url': last_project_url,
152+
'days': RECENT_DAYS
148153
}
149154

155+
if not memcache.set(memcache_data_key, json.dumps(values),
156+
MEMCACHE_EXPIRATION):
157+
logging.error('Memcache set failed for user data %s', username)
158+
159+
return values
160+
150161
def get(self, username):
151162
support = self.get_option('s', '0')
152163
analytics = self.get_option('a', '1')
@@ -165,19 +176,10 @@ def get(self, username):
165176
if cached_data:
166177
return self.write(cached_data)
167178
else:
168-
memcache_data_key = '!data!{}'.format(username)
169-
values = json.loads(memcache.get(memcache_data_key) or '{}')
170-
if not values:
171-
# Caution, the method below may alter state.
172-
values = self.calculate_user_values(username)
173-
174-
if not values: # still don't have the values, something went wrong
179+
values = self.calculate_user_values(username)
180+
if not values: # don't have the values, something went wrong
175181
return
176182

177-
if not memcache.set(memcache_data_key,
178-
json.dumps(values), MEMCACHE_EXPIRATION):
179-
logging.error('Memcache set failed for user data %s', username)
180-
181183
if jsonp:
182184
output = '{0}({1})'.format(jsonp, json.dumps(values))
183185
self.write(output)

0 commit comments

Comments
 (0)