Last active
November 1, 2020 06:31
-
-
Save erwan-lemonnier/a90cec4b6d47df4a6bf9c198ecc0c7fa to your computer and use it in GitHub Desktop.
Bulk operation via a smart cache (proof of concept)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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