Add app-local caching and stop using memcache; reduce overall datastore access.
This commit is contained in:
5
api.py
5
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']
|
||||
|
||||
10
channel.py
10
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([
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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 = (
|
||||
|
||||
Reference in New Issue
Block a user