It looks like this creates a new tag every time.  Can you get() the tag before putting and only put if there isn't already one?  I think there's a get_or_insert function that is slightly more efficient.

Also, this results in doing database calls serially rather than in parallel.  Can you do a put_multi instead to solve this issue?