Your answer was very enjoying to read as it provides great insight into 
ndb, thanks

Offtopic: Is there any reason why one should store KeyProperty's instead of 
StringProperty's with string key names (assuming all keys are strings, 
that's how I store things) ?

On topic: Richard, I think you can get a PickleProperty and put the Y 
inside that pickle and occasionally refresh its contents (or never if Y 
stays the same), this way you can achieve what you were doing with db even 
easier with ndb (auto caching). 


On Tuesday, April 24, 2012 10:59:25 PM UTC+3, Guido van Rossum wrote:
>
> On Monday, April 23, 2012 10:21:26 PM UTC-7, Richard Arrano wrote:
>>
>> I'm switching from db to ndb and I have a question regarding caching: 
>>
>> In the old db, I would have a class X that contains a reference to a 
>> class Y. The Y type would be accessed most frequently and rarely 
>> change. So when I would query an X and retrieve the Y type it points 
>> to, I would store X in the memcache with the actual instance Y rather 
>> than the key. If X is invalidated in the memcache, then so is the Y 
>> instance but otherwise I would skip the step of querying Y upon re- 
>> retrieving X from the memcache. Is there any way to do this in ndb? Or 
>> must I re-query each Y type even if it is from memcache or context? 
>>
>
> If you leave the caching to NDB, you probably needn't worry about this 
> much. It's going to be an extra API call to retrieve Y (e.g. y = 
> x.yref.get()) but that will generally be a memcache roundtrip. If you are 
> retrieving a lot of Xes in one query, there's a neat NDB idiom to prefetch 
> all the corresponding Ys in one roundtrip:
>
> xs = MyModel.query(...).fetch()
> _ = ndb.get_multi([x.yref for x in xs])
>
>
>
> This effectively throws away the ys, but populates them in the context 
> cache. After this, for any x in xs, the call x.yref.get() will use the 
> context cache, which is a Python dict in memory. (Its lifetime is one 
> incoming HTTP request.)
>
> You can even postpone waiting for the ys, using an async call:
>
> xs = MyModel.query(...).fetch()
> _ = ndb.get_multi_async([x.yref for x in xs])
>
>
>
>
> Now the first time you reference some x.yref.get() it will block for the 
> get_multi_async() call to complete, and after that all subsequent 
> x.yref.get() calls will be satisfied from memory (no server roundtrip at 
> all).
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/google-appengine/-/4X9agm_IKGAJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en.

Reply via email to