高并发数据的重复写入问题
- 使用Redis的setnx来解决这个问题。setnx key value 若 value 存在 则返回 False.
def db_op_thread_func(i, num_of_op): r = redis.Redis(host='redisHost', port=6379, db=0) conn = MySQLdb.connect(host="dbHost", port=3306, user="root", passwd="pass", db="blog") cursor = conn.cursor() for j in range(0, int(num_of_op)): nickname = 'User' + str(int(i % 10)) lockkey = "lock"+nickname getsql = ("select ID from User where Username = '%s'") % (nickname) cursor.execute(getsql) fetchData = cursor.fetchall() reply = r.setnx(lockkey, 1) if (reply == True): r.expire(lockkey, 30) RedisLock = False else: RedisLock = True if not fetchData and RedisLock == False: sql = ("insert into User (Username) values('%s') ")%(nickname) cursor.execute(sql) id = int(conn.insert_id()) print int(id) print "thread", i, ":", " num:", j conn.commit() else: print '已经有' conn.close()
- 每当插入前设置 UserName 的一个 redis Lock ,expire 设置为30s ,这样就可以利用 setnx 的原子性 来实现分布式锁来保证数据唯一性。