2881099 / csredis

.NET Core or .NET Framework 4.0+ client for Redis and Redis Sentinel (2.8) and Cluster. Includes both synchronous and asynchronous clients.
MIT License
2.02k stars 414 forks source link

多进程多线程使用RedisHelper.LPop取出相同项 #413

Open lonelyislong opened 2 years ago

lonelyislong commented 2 years ago
new Thread(() =>
                {
                    LeaguerAccountDto account = null;

                    try
                    {
                        while (RedisHelper.LLen(key) > 0)
                        {
                            account = RedisHelper.LPop<LeaguerAccountDto>(key);
                            if (account != null)
                            {
                                  //业务处理
                            }
                        }
                    }
                  catch(Exception ex){
                  //记录错误
                  }
            });

希望依赖redis lpop命令的原子性来多个程序多线程跑数据,加快进度,数据表因为有做主键约束,结果发现有取到重复项现象: [2021-12-20 11:08:40] 记录已存在:Duplicate entry '25a814b3-5177-4f14-b486-9bf9849e1903-20211126' for key 'PRIMARY',跳过执行 [2021-12-20 11:09:03] 记录已存在:Duplicate entry '22aad058-9f14-46f2-a9ac-dae7b67d1b31-20211126' for key 'PRIMARY',跳过执行 现在的问题:难道redis lpop命令不是原子性?如果有大神看到,麻烦解答下,非常感谢!!!

2881099 commented 2 years ago

不是