0
votes

The documentation (https://cloud.google.com/appengine/docs/python/ndb/) states that

NDB uses Memcache as a cache service for "hot spots" in the data

I am now using memcache only as follows: memcache.set(key=(id), value=params, time=0)

That expires (auto flushes) pretty often and so I would like to use NDB Datastore. I thought I would have to always put the key-value in both NDB and Memcache, then check both. Is this being done automatically by NDB?

Ie. ancestor_key = ndb.Key("Book", guestbook_name or "*notitle*")

greetings = Greeting.query_book(ancestor_key).fetch(20)

Would that implicitly set Memcache ?

And when I read from NDB, would it implicitly try a memcache.get(key) first?

Thanks for your patience.

EDIT - What I tried:

As a test I tried something like this:

class Book(ndb.Model):
    content = ndb.StringProperty() 

class update(webapp2.RequestHandler): 
    def post(self): 
        p1='1' 
        p2='2' 
        p3='3' 
        p4='4' 
        p5='5' 
        id='test'
        paramarray = (p1,p2,p3,p4,p5) 

        book = Book(name=id,value=paramarray) 
        # OR likes this -  book = Book(ndb.Key(id),value=paramarray)          
        book.put() 

Both versions error out. Trying to get a key of the var id with the values of paramarray

EDIT 2 Daniel, Thank you for everything.

Have follow up formatting questions, will ask a new question.

1
The appstats page will show you rpc requests so you can verify your memcache get/set behavior. - Josh J
@JoshJ Thanks, that sounds useful and I'll try that. - PyGAE

1 Answers

1
votes

Yes; see the full documentation on ndb caching. Basically, every write is cached both in a request-local in-context cache, and in the main memcached store; a get by key will look up in both caches first before falling back to the real datastore.

Edit I can't understand why you think your example would work. You defined a model with a content property, but then try to set name and value properties on it; naturally that will fail.

You should go through the ndb documentation, which gives a good introduction to using the model class.