Cache named profiles in memory in the Python application, since they're immutable once written.
This commit is contained in:
@@ -47,6 +47,21 @@ class AccessDenied(Exception):
|
|||||||
class Profile(db.Model):
|
class Profile(db.Model):
|
||||||
google_user = db.UserProperty()
|
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):
|
def MergeFrom(self, source_profile):
|
||||||
# This is non-transactional and racy (new messages can be introduced by the
|
# 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
|
# old client after we start). This is hard to solve because we're not in
|
||||||
|
|||||||
@@ -23,11 +23,7 @@ from cosmopolite.lib import models
|
|||||||
def CreateClientAndProfile(client_id, google_user):
|
def CreateClientAndProfile(client_id, google_user):
|
||||||
if google_user:
|
if google_user:
|
||||||
# We're going to need a profile for this user regardless
|
# We're going to need a profile for this user regardless
|
||||||
profile = models.Profile.get_by_key_name(google_user.user_id())
|
profile = models.Profile.FindOrCreate(google_user)
|
||||||
if not profile:
|
|
||||||
profile = models.Profile(
|
|
||||||
key_name=google_user.user_id(), google_user=google_user)
|
|
||||||
profile.put()
|
|
||||||
else:
|
else:
|
||||||
profile = None
|
profile = None
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user