Cache named profiles in memory in the Python application, since they're immutable once written.

This commit is contained in:
Ian Gulliver
2014-06-07 23:06:17 -07:00
parent 66e68ea8dd
commit 1b4b55ac23
2 changed files with 16 additions and 5 deletions

View File

@@ -47,6 +47,21 @@ class AccessDenied(Exception):
class Profile(db.Model):
google_user = db.UserProperty()
_cache = {}
@classmethod
@db.transactional()
def FindOrCreate(cls, google_user):
key_name = google_user.user_id()
if key_name in cls._cache:
return cls._cache[key_name]
profile = cls.get_by_key_name(key_name)
if not profile:
profile = cls(key_name=key_name, google_user=google_user)
profile.put()
cls._cache[key_name] = profile
return profile
def MergeFrom(self, source_profile):
# This is non-transactional and racy (new messages can be introduced by the
# old client after we start). This is hard to solve because we're not in

View File

@@ -23,11 +23,7 @@ from cosmopolite.lib import models
def CreateClientAndProfile(client_id, google_user):
if google_user:
# We're going to need a profile for this user regardless
profile = models.Profile.get_by_key_name(google_user.user_id())
if not profile:
profile = models.Profile(
key_name=google_user.user_id(), google_user=google_user)
profile.put()
profile = models.Profile.FindOrCreate(google_user)
else:
profile = None