Reputation: 354
I want to add the 'check username available' functionality on my signup page using AJAX. I have few doubts about the way I should implement it.
With which event should I register my AJAX requests? We can send the requests when user focus out of the 'username' input field (blur event) or as he types (keyup event). Which provides better user experience?
On the server side, a simple way of dealing with requests would be to query my main 'Accounts' database. But this could lead to a lot of request hitting my database(even more if we POST using the keyup event). Should I maintain a separate model for registered usernames only and use that to get better results?
Is it possible to use Memcache in this case? Initializing cache with every username as key and updating it as we register users and use a random key to check if cache is actually initialized or pass the queries directly to db.
Upvotes: 1
Views: 200
Reputation: 728
Answers -
If you are concerned about 2 people simultaneously requesting the same user name, put your create method in a transaction:
@classmethod
@ndb.transactional()
def create_account(cls, name, other_params):
acct = Account.get_by_id(name)
if not acct:
acct = Account(id=name, other param assigns)
acct.put()
Upvotes: 2
Reputation: 1613
I would recommend the blur event of the username field, combined with some sort of inline error/warning display.
I would also suggest maintaining a memcache of registered usernames, to reduce DB hits and improve user experience - although probably not populate this with a warm-up, but instead only when requests are made. This is sometimes called a "Repository" pattern.
BUT, you can only populate the cache with USED usernames - you should not store the "available" usernames here (or if you do, use a much lower timeout).
You should always check directly against the DB/Datastore when actually performing the registration. And ideally in some sort of transactional method so that you don't have race conditions with multiple people registering.
BUT, all of this work is dependant on several things, including how busy your app is and what data storage tech you are using!
Upvotes: 2