From a70982243b65fb8a67e35e34add6dc889029e7d0 Mon Sep 17 00:00:00 2001 From: Ian Gulliver Date: Wed, 28 May 2014 14:44:13 -0700 Subject: [PATCH] Add app-local caching and stop using memcache; reduce overall datastore access. --- api.py | 5 +++++ channel.py | 10 ++++------ lib/auth.py | 9 ++++----- lib/models.py | 14 ++++++++++++-- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/api.py b/api.py index 904de28..0875602 100644 --- a/api.py +++ b/api.py @@ -54,6 +54,11 @@ def CreateChannel(google_user, client, instance_id, args): def Pin(google_user, client, instance_id, args): instance = models.Instance.FromID(instance_id) + if not instance or not instance.active: + # Probably a race with the channel opening + return { + 'result': 'retry', + } subject = args['subject'] message = args['message'] diff --git a/channel.py b/channel.py index a51f24c..fce9879 100644 --- a/channel.py +++ b/channel.py @@ -46,17 +46,15 @@ class OnChannelDisconnect(webapp2.RequestHandler): instance_id = self.request.get('from') instance_key = db.Key.from_path('Instance', instance_id) - subscriptions = models.Subscription.all().filter('instance =', instance_key) - for subscription in subscriptions: - subscription.delete() + subscriptions = list(models.Subscription.all().filter('instance =', instance_key)) + if subscriptions: + db.delete(subscriptions) pins = models.Pin.all().filter('instance =', instance_key) for pin in pins: pin.Delete() - instance = models.Instance.FromID(instance_id) - if instance: - instance.delete() + db.delete(instance_key) app = webapp2.WSGIApplication([ diff --git a/lib/auth.py b/lib/auth.py index 4d21c44..4a92c5d 100644 --- a/lib/auth.py +++ b/lib/auth.py @@ -17,7 +17,6 @@ import hmac import random import string -from google.appengine.api import memcache from google.appengine.ext import db @@ -32,11 +31,11 @@ class AuthKey(db.Model): _KEY_CHARS = string.ascii_letters + string.digits _KEY_LENGTH = 64 +_AUTH_KEY = [] def GetAuthKey(): - auth_key = memcache.get('auth_key') - if auth_key: - return auth_key + if _AUTH_KEY: + return _AUTH_KEY[0] for key in AuthKey.all().filter('live =', True): auth_key = key.auth_key @@ -45,7 +44,7 @@ def GetAuthKey(): auth_key = ''.join(random.choice(_KEY_CHARS) for _ in xrange(_KEY_LENGTH)) AuthKey(auth_key=auth_key).put() - memcache.set('auth_key', auth_key) + _AUTH_KEY.append(auth_key) return auth_key diff --git a/lib/models.py b/lib/models.py index 4b6056f..853c0d0 100644 --- a/lib/models.py +++ b/lib/models.py @@ -112,6 +112,8 @@ class Subject(db.Model): next_message_id = db.IntegerProperty(required=True, default=1) + _cache = {} + @classmethod def _UpdateHashWithString(cls, hashobj, string): string = string.encode('utf8') @@ -138,12 +140,20 @@ class Subject(db.Model): else: writable_only_by = None - return cls.get_or_insert( - cls._KeyName(subject), + key_name = cls._KeyName(subject) + obj = cls._cache.get(key_name) + if obj: + return obj + + obj = cls.get_or_insert( + key_name, name=subject['name'], readable_only_by=readable_only_by, writable_only_by=writable_only_by) + cls._cache[key_name] = obj + return obj + @db.transactional() def GetRecentMessages(self, num_messages): query = (