How can I implement a user name availability check on the google app engine?
When someone signs up for my website they must sign up with a unique user name. My current pseudocode for this is:
if(datastore.nameIsAvailable(name)){
datastore.createUser(name);
}
How do I make sure that in between the time when nameIsAvailable
returns true, and the createUser
is finished, another instance of my app doesn't create a user with the same 开发者_如何学Goname?
You could do a query for the name:
appuser = AppUser.all().filter('name =', name).get()
if appuser:
# user already has this name
else:
# it's all yours!
...or better yet have the user's name be the key_name for your AppUser
:
appuser = AppUser.get_by_key(name)
if appuser:
# user already has this name
else:
# it's all yours!
...and even better:
appuser = AppUser.get_or_insert(name)
# appuser is now either the existing user with this name,
# or a new user with this name!
Having your name be the entity's key_name means that it will always and forever be unique, and looking up a user by their name will be as fast as possible.
(This is presuming you're using Python, if not, I can add a Java example, but the idea is the same)
精彩评论