Add app-local caching and stop using memcache; reduce overall datastore access.

This commit is contained in:
Ian Gulliver
2014-05-28 14:44:13 -07:00
parent 9736c15faa
commit a70982243b
4 changed files with 25 additions and 13 deletions

5
api.py
View File

@@ -54,6 +54,11 @@ def CreateChannel(google_user, client, instance_id, args):
def Pin(google_user, client, instance_id, args): def Pin(google_user, client, instance_id, args):
instance = models.Instance.FromID(instance_id) 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'] subject = args['subject']
message = args['message'] message = args['message']

View File

@@ -46,17 +46,15 @@ class OnChannelDisconnect(webapp2.RequestHandler):
instance_id = self.request.get('from') instance_id = self.request.get('from')
instance_key = db.Key.from_path('Instance', instance_id) instance_key = db.Key.from_path('Instance', instance_id)
subscriptions = models.Subscription.all().filter('instance =', instance_key) subscriptions = list(models.Subscription.all().filter('instance =', instance_key))
for subscription in subscriptions: if subscriptions:
subscription.delete() db.delete(subscriptions)
pins = models.Pin.all().filter('instance =', instance_key) pins = models.Pin.all().filter('instance =', instance_key)
for pin in pins: for pin in pins:
pin.Delete() pin.Delete()
instance = models.Instance.FromID(instance_id) db.delete(instance_key)
if instance:
instance.delete()
app = webapp2.WSGIApplication([ app = webapp2.WSGIApplication([

View File

@@ -17,7 +17,6 @@ import hmac
import random import random
import string import string
from google.appengine.api import memcache
from google.appengine.ext import db from google.appengine.ext import db
@@ -32,11 +31,11 @@ class AuthKey(db.Model):
_KEY_CHARS = string.ascii_letters + string.digits _KEY_CHARS = string.ascii_letters + string.digits
_KEY_LENGTH = 64 _KEY_LENGTH = 64
_AUTH_KEY = []
def GetAuthKey(): def GetAuthKey():
auth_key = memcache.get('auth_key') if _AUTH_KEY:
if auth_key: return _AUTH_KEY[0]
return auth_key
for key in AuthKey.all().filter('live =', True): for key in AuthKey.all().filter('live =', True):
auth_key = key.auth_key auth_key = key.auth_key
@@ -45,7 +44,7 @@ def GetAuthKey():
auth_key = ''.join(random.choice(_KEY_CHARS) for _ in xrange(_KEY_LENGTH)) auth_key = ''.join(random.choice(_KEY_CHARS) for _ in xrange(_KEY_LENGTH))
AuthKey(auth_key=auth_key).put() AuthKey(auth_key=auth_key).put()
memcache.set('auth_key', auth_key) _AUTH_KEY.append(auth_key)
return auth_key return auth_key

View File

@@ -112,6 +112,8 @@ class Subject(db.Model):
next_message_id = db.IntegerProperty(required=True, default=1) next_message_id = db.IntegerProperty(required=True, default=1)
_cache = {}
@classmethod @classmethod
def _UpdateHashWithString(cls, hashobj, string): def _UpdateHashWithString(cls, hashobj, string):
string = string.encode('utf8') string = string.encode('utf8')
@@ -138,12 +140,20 @@ class Subject(db.Model):
else: else:
writable_only_by = None writable_only_by = None
return cls.get_or_insert( key_name = cls._KeyName(subject)
cls._KeyName(subject), obj = cls._cache.get(key_name)
if obj:
return obj
obj = cls.get_or_insert(
key_name,
name=subject['name'], name=subject['name'],
readable_only_by=readable_only_by, readable_only_by=readable_only_by,
writable_only_by=writable_only_by) writable_only_by=writable_only_by)
cls._cache[key_name] = obj
return obj
@db.transactional() @db.transactional()
def GetRecentMessages(self, num_messages): def GetRecentMessages(self, num_messages):
query = ( query = (