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

View File

@@ -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

View File

@@ -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 = (