Skip to content

Instantly share code, notes, and snippets.

@erwan-lemonnier
Last active November 1, 2020 06:31
Show Gist options
  • Save erwan-lemonnier/a90cec4b6d47df4a6bf9c198ecc0c7fa to your computer and use it in GitHub Desktop.
Save erwan-lemonnier/a90cec4b6d47df4a6bf9c198ecc0c7fa to your computer and use it in GitHub Desktop.
Bulk operation via a smart cache (proof of concept)
from pymacaron.auth import get_userid
# Implements an hypothetical endpoint that let's a user (identified by a JWT token)
# add another user as friend (identified by its user ID in the data object received
# as parameter, mapping to the POST request's body)
def do_add_friend(data):
viewer_id = get_userid() # Extract the ID of the current user from its JWT auth token
friend_id = data.user_id # And 'user_id' in the POST request is the friend's ID
# Use a smart cache object that fetches both profiles in one single
# bulk get call:
cache = ObjectCache()
cache.fetch_missing_objects(viwer_id, friend_id)
viewer = cache.get_object(viewer_id)
friend = cache.get_object(friend_id)
# do some stuff....
viewer.add_friend(friend)
# And a saver objects, that keeps a queue of objects to bulk insert into Datastore.
saver = ObjectSaver(cache=cache)
saver.put_object(viewer)
saver.commit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment