Skip to content

Commit

Permalink
Fixed issue with cache not returning the correct expire time.
Browse files Browse the repository at this point in the history
Added logging to monitor cache use
  • Loading branch information
kenkendk committed Jul 11, 2015
1 parent c598538 commit a49a12d
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ def get(self, service=None):
self.response.write(template.render(template_values))
statetoken.delete()

logging.info('Created new authid %s for service %s', keyid, provider['id'])

except:
logging.exception('handler error for ' + display)

Expand Down Expand Up @@ -410,10 +412,15 @@ def get(self):
cacheurl = '/refresh?id=' + keyid + '&h=' + hashlib.sha256(password).hexdigest()

cached_res = memcache.get(cacheurl)
if cached_res != None:
logging.info('Serving cached response to: %s', keyid)
self.response.write(cached_res)
return
if cached_res != None and type(cached_res) != type(''):
exp_secs = (int)((cached_res['expires'] - datetime.datetime.utcnow()).total_seconds())

if exp_secs > 30:
logging.info('Serving cached response to: %s, expires in %s secs', keyid, exp_secs)
self.response.write(json.dumps({'access_token': cached_res['access_token'], 'expires': exp_secs, 'type': cached_res['type']}))
return
else:
logging.info('Cached response to: %s is invalid because it expires in %s', keyid, exp_secs)

# Find the entry
entry = dbmodel.AuthToken.get_by_key_name(keyid)
Expand Down Expand Up @@ -472,12 +479,13 @@ def get(self):
entry.blob = base64.b64encode(cipher)
entry.put()

cached_res = json.dumps({'access_token': resp['access_token'], 'expires': exp_secs, 'type': servicetype})
cached_res = {'access_token': resp['access_token'], 'expires': entry.expires, 'type': servicetype}

memcache.set(key=cacheurl, value=cached_res, time=exp_secs - 10)
logging.info('Caching response to: %s for %s secs, service: %s', keyid, exp_secs - 10, servicetype)

# Write the result back to the client
self.response.write(cached_res)
self.response.write(json.dumps({'access_token': resp['access_token'], 'expires': exp_secs, 'type': servicetype}))

except:
logging.exception('handler error for ' + servicetype)
Expand Down

0 comments on commit a49a12d

Please sign in to comment.