public bool SetUpDwon(UpDownInputDto upDownInput) { if (upDownInput.IsUp) { using (var datalock = cacheClientDB.AcquireLock("DataLock:", TimeSpan.FromSeconds(2))) { string lua = @""; cacheClientDB.RemoveItemFromSet(GetSetIDByUpDown(PostRepleyEnum.Repley, UpDownEnum.Down, upDownInput.PostOrReplyId), upDownInput.UserId.ToString()); cacheClientDB.AddItemToSet(GetSetIDByUpDown(PostRepleyEnum.Repley, UpDownEnum.Up, upDownInput.PostOrReplyId), upDownInput.UserId.ToString()); } return(true); } var isok = cacheClientDB.Add <string>("lockdata3", "lockdata3", TimeSpan.FromSeconds(2)); if (isok) { try { cacheClientDB.RemoveItemFromSet(GetSetIDByUpDown(PostRepleyEnum.Repley, UpDownEnum.Up, upDownInput.PostOrReplyId), upDownInput.UserId.ToString()); cacheClientDB.AddItemToSet(GetSetIDByUpDown(PostRepleyEnum.Repley, UpDownEnum.Down, upDownInput.PostOrReplyId), upDownInput.UserId.ToString()); } catch (Exception) { throw; } finally { cacheClientDB.Remove("lockdata3"); } } //cacheClientDB.AddItemToSet(ConfigManger.Project + PostRepleyEnum.Repley.ToString()+":"+upDownInput.PostOrReplyId, upDownInput.UserId.ToString()); return(true); //return UpDown(upDownInput.IsUp, upDownInput.UserId, upDownInput.PostOrReplyId); }
public bool SetUpDwon(UpDownInputDto upDownInput) { // 需要缓存 去重 互斥 //如果是赞,则删除踩 // 如果是踩,则删除赞 //比如踩,则判断有么有赞,如果有,则删出,没有啥都不敢,然后直接写入踩 if (upDownInput.IsUp) { //事务 集群的话事务百搭 // 锁 /// 阻塞锁,能不用则不用 /// //实现原子性, redis ,一个lua 脚本对于redis服务而已就是调了一个方法,想当于打包然后保证原子性执行 using (var datalock = cacheClientDB.AcquireLock("DataLock:", TimeSpan.FromSeconds(2))) { // lua // 大道至简 // 持久化-- 定时作业 一个key是一个帖子,到时候定时刷盘,不能全盘扫描redis的key // 每次不管是点赞还是点踩,都维护一个list,专门存在我们点赞和点踩的key,list有分页 // 能做,,clay // 想想,换------ string lua = @""; cacheClientDB.RemoveItemFromSet(GetSetIDByUpDown(PostRepleyEnum.Content, UpDownEnum.Down, upDownInput.PostOrReplyId), upDownInput.UserId.ToString()); //cacheClientDB.GetClient().Custom("SMOVE", GetSetIDByUpDown(PostRepleyEnum.Content, UpDownEnum.Down, upDownInput.PostOrReplyId), GetSetIDByUpDown(PostRepleyEnum.Content, UpDownEnum.Up, upDownInput.PostOrReplyId), upDownInput.UserId.ToString()); cacheClientDB.AddItemToSet(GetSetIDByUpDown(PostRepleyEnum.Content, UpDownEnum.Up, upDownInput.PostOrReplyId), upDownInput.UserId.ToString()); } return(true); } var isok = cacheClientDB.Add <string>("lockdata2", "lockdata2", TimeSpan.FromSeconds(2)); // 非阻塞锁 if (isok) { try { cacheClientDB.RemoveItemFromSet(GetSetIDByUpDown(PostRepleyEnum.Content, UpDownEnum.Up, upDownInput.PostOrReplyId), upDownInput.UserId.ToString()); // 首先往redis里面写入这个key cacheClientDB.AddItemToSet(GetSetIDByUpDown(PostRepleyEnum.Content, UpDownEnum.Down, upDownInput.PostOrReplyId), upDownInput.UserId.ToString()); } catch (Exception) { throw; } finally { cacheClientDB.Remove("lockdata2"); } } //cacheClientDB.GetClient().Custom("SMOVE", GetSetIDByUpDown(PostRepleyEnum.Content, UpDownEnum.Up, upDownInput.PostOrReplyId), GetSetIDByUpDown(PostRepleyEnum.Content, UpDownEnum.Down, upDownInput.PostOrReplyId), upDownInput.UserId.ToString()); return(true); //return UpDown(upDownInput.IsUp, upDownInput.UserId, upDownInput.PostOrReplyId); }