Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Datastore: Is it possible to only save to memcache when using ndb API?

dear all

Currently I'm using ndb API to store some statistic information. Unfortunately, this becomes the major source of my cost. I'm thinking it should be much cheaper if I only save them to memcache. It doesn't matter if data is lost due to cache expire.

After read the manual, I assume _use_datastore class variable can be used to configure this behaviour:

class StaticModel(ndb.Model):
    _use_datastore = False

    userid = ndb.StringProperty()
    created_at = ndb.DateTimeProperty(auto_now_add=True)

May I know if above statement is the right solution?

Cheers!

like image 359
James Gan Avatar asked Dec 03 '25 23:12

James Gan


1 Answers

I think there are three ways to achieve what you want.

The first is to set _use_datastore = False on the NDB model class as per your question.

The second would be to pass use_datastore=False whenever you put / get / delete a StaticModel. An example would be:

model = StaticModel(userid="foo")
key = model.put(use_datastore=False)
n = key.get(use_datastore=False)

The third option would be to set a datastore policy in the NDB Context which returns false for any StaticModel keys. Something like:

context.set_datastore_policy(lambda key: True if key.kind() == 'StaticModel' else False)
like image 183
PGower Avatar answered Dec 05 '25 16:12

PGower