It looks like temporary variables are also cached when using NDB.
class MyModel(ndb.Model)
def __init__(self, *args, **kwds):
self._temporary = []
ndb.Model.__init__(self, *args, **kwds)
Is there a way not to store the temporary / helper variables in the cache?
EDIT:
Here is a simple test case proving my problem:
#Python imports
import unittest
# GAE imports
from google.appengine.ext import testbed
from google.appengine.datastore import datastore_stub_util
from google.appengine.ext import ndb
class TestModel(ndb.Model):
username = ndb.StringProperty()
def __init__(self, *args, **kwds):
self._temp = []
ndb.Model.__init__(self, *args, **kwds)
class TestModels(unittest.TestCase):
def setUp(self):
self.testbed = testbed.Testbed()
self.testbed.activate()
self.testbed.init_taskqueue_stub()
self.testbed.init_urlfetch_stub()
self.testbed.init_mail_stub()
self.testbed.init_images_stub()
self.testbed.init_blobstore_stub()
self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1)
self.testbed.init_datastore_v3_stub(consistency_policy=self.policy)
self.testbed.init_memcache_stub()
self.testbed.setup_env(app_id='ndb-test')
def tearDown(self):
self.testbed.deactivate()
def test1(self):
test_model = TestModel()
test_model.username = 'Tom'
test_model._temp = 'Temporary'
test_model.put()
test = TestModel.query().fetch(10)
self.assertEqual(1, len(test))
test = test[0]
self.assertEqual('Tom', test.username)
self.assertEqual([], test._temp)
The last line of the test self.assertEqual([], test._temp) fails even though I would expect it to pass.
Is there a way to either not store temporary / helper model properties or have a cleaver way of resetting them every time model is loaded from cache?
NDB has a couple of caches – instance memory and memcache. In instance memory, the actual entity itself is cached, so the same entity object will be returned for multiple get calls within the same request.
As long as you’re using NDB with caching enabled, this is something you’ll have to deal with – for instance, by not storing ‘temporary’ data on an entity in the first place.