Guest User

Untitled

a guest
Apr 25th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. from google.appengine.ext import db
  2.  
  3. class EntityCache(object):
  4. """Caches fetched entities so subsequent fetches return the same instance.
  5.  
  6. Usage:
  7. cache = EntityCache()
  8. e1, e2 = cache.get([k1, k2])
  9. e3, e4 = cache.get([k2, k3])
  10. assert e2 == e3
  11. """
  12.  
  13. def __init__(self):
  14. self.cache = {}
  15.  
  16. def get(self, keys):
  17. """Get the entities matching the provided keys."""
  18. if isinstance(keys, db.Key):
  19. keys = [keys]
  20. missing_keys = [k for k in keys if k not in self.cache]
  21. if missing_keys:
  22. self.cache.update((x.key(), x) for x in db.get(missing_keys))
  23. return [self.cache[k] for k in keys]
Add Comment
Please, Sign In to add comment