public void StringSet() { var isTrue = _helper.StringSet("StringSet", "StringSet"); var str = _helper.GetString("StringSet"); Assert.True(isTrue); Assert.Equal("StringSet", str); }
public JsonResult TestRedis() { var guid = Guid.NewGuid().ToString(); _redisHelper.StringSet("guid:" + guid, guid, 1000); _redisHelper.StringGet("guid:" + guid); _redisHelper.KeyDelete("guid:" + guid); return(Json(guid)); }
public void ExcuteWorker(IEnumerable <int> rouIdList) { //返回现场 BackToLastExport(); foreach (var rid in rouIdList) { try { if (SignalConfig.IsStop) { Console.WriteLine("收到停止导入命令,当前导入id(当前id未导入):" + rid); return; } //time.Marke("6.导入活动数据+用户数据", rid.ToString()); if (ExportAct(rid)) { //time.Marke("7.设置redisKey", rid.ToString()); _redisHelper.StringSet(SUCCESSACTIDQUEUEKEY, rid.ToString(), 60 * 60 * 24 * 3); } else { //time.Marke("8.该活动导入数据失败了", rid.ToString()); Console.WriteLine(rid + "该活动导入数据失败了"); //加入日志 ExportError error = new ExportError() { RowId = rid.ToString(), ErrorMsg = "该活动导入数据失败了111" }; //time.Marke("9.开始插入失败日志", rid.ToString()); _exportErrorRepository.Insert(error); //time.Marke("10.失败日志插入完成", rid.ToString()); } } catch (Exception ex) { //加入错误队列 //_redisHelper.ListRightPush(SUCCESSACTIDQUEUEKEY, actId.ToString()); Console.WriteLine(rid + "该活动导入数据出错了,错误原因:" + ex.Message); //加入日志 ExportError error = new ExportError() { RowId = rid.ToString(), ErrorMsg = ex.Message, OperateException = ex.ToString() }; //time.Marke("11.开始插入异常日志", rid.ToString()); _exportErrorRepository.Insert(error); //time.Marke("12.异常日志插入完成", rid.ToString()); } //一个活动保存一次 time.SaveData(rid); } }
public AjaxResult SmsCode(string email) { AjaxResult amm; int limitcount = 10; int limitMinutes = 10; if (!Validate.IsEmail(email)) { return(AjaxResult.Error("邮箱格式不正确")); } AppUser userEntity = this.Queryable(u => u.Email == email && u.DeleteMark == false).FirstOrDefault(); if (userEntity == null) { amm = AjaxResult.Error("此邮箱尚未注册!"); } else { string count = _iRedisHelper.StringGet <string>(email); //缓存十分钟,如果缓存中存在,且请求次数超过10次,则返回 if (!string.IsNullOrEmpty(count) && Convert.ToInt32(count) >= limitcount) { amm = AjaxResult.Error("没收到邮箱:请联系[email protected]"); } else { #region 发送邮箱,并写入缓存,更新登录信息表的token,date,code int num = 0; if (!string.IsNullOrEmpty(count)) { num = Convert.ToInt32(count); } string countplus1 = num + 1 + ""; _iRedisHelper.StringSet <string>(email, countplus1, new TimeSpan(0, limitMinutes, 0)); ResetPwd resetpwdEntry = new ResetPwd { UserId = (int)userEntity.Id, ModifyPwdToken = Utils.GuId(), PwdCodeTme = DateTime.Now, ModfiyPwdCode = Utils.RndNum(6), LoginName = userEntity.LoginName, NickName = userEntity.NickName }; //将发送验证码的数据存入redis缓存中 _iRedisHelper.StringSet(email + "sendcodekey", resetpwdEntry, new TimeSpan(0, limitMinutes, 0)); //发送找回密码的邮件 string body = UiHelper.FormatEmail(resetpwdEntry, "PwdReSetTemplate"); _imailHelper.SendByThread(email, "[、天上有木月博客] 密码找回", body); #endregion //将修改密码的token返回给前端 amm = AjaxResult.Info("验证码已发送至你的邮箱!", resetpwdEntry.ModifyPwdToken, ResultType.Success.ToString()); } } return(amm); }
/// <summary> /// 根据邮箱发送四位验证码,并将token返回给前端 /// </summary> /// <param name="email"></param> /// <returns></returns> public ActionResult GetActiveCode(string email) { if (email.IsNullOrEmpty()) { return(Error("Email不能为空!!!")); } if (!Validate.IsEmail(email)) { return(Error("Email格式不正确!!!")); } int id = Op.CurrentUser.UserId; //bool EmailIsValid = _appUserRepository.IQueryable(u => u.Id == Id).Select(r => r.EmailIsValid).FirstOrDefault(); //if (EmailIsValid) //{ // return Error("您已经绑定成功了,请不要重复绑定!!!"); //} //为了安全,一个用户IP10分钟内只能请求此接口3次 string ip = Net.Ip; int count = _redisHelper.StringGet <int>(ip); if (count >= 3) { return(Error("请求过于频繁,请稍后再试!")); } count += 1; _redisHelper.StringSet(ip, count, new TimeSpan(0, 10, 0)); var amm = _appUserRepository.IsRepeat(new AppUser { Id = id, LoginName = Utils.GuId(), Email = email }); if (amm.State.Equals(ResultType.Error.ToString())) { return(Error("该邮箱已被绑定其他用户绑定!")); } //缓存1小时 TimeSpan saveTime = new TimeSpan(1, 0, 0); //生成token string token = Utils.GuId(); string rand4Num = Utils.RndNum(4); //redis缓存绑定邮箱随机token作为键,email作为值,随机u为键,当前登录id为值 _redisHelper.StringSet(token, email, saveTime); _redisHelper.StringSet(token + email, rand4Num, saveTime); EmailViewModel emailViewModel = new EmailViewModel { ToUserName = email, Code = rand4Num }; string body = UiHelper.FormatEmail(emailViewModel, "SendCodeTemplate"); _imailHelper.SendByThread(email, "[、天上有木月博客] 邮箱激活通知", body); return(Success("邮箱发送成功,请查收", token)); }
public void RedisTest() { var customerList = CustomerList(); var isTrue = _redisHelper.StringSet("StringSetList", customerList); var list = _redisHelper.GetString <List <Customer> >("StringSetList"); }
//Intercept方法是拦截的关键所在,也是IInterceptor接口中的唯一定义 public void Intercept(IInvocation invocation) { var method = invocation.MethodInvocationTarget ?? invocation.Method; //对当前方法的特性验证 var qCachingAttribute = method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(CachingAttribute)) as CachingAttribute; if (qCachingAttribute != null) { //获取自定义缓存键 var cacheKey = CustomCacheKey(invocation); //注意是 string 类型,方法GetValue var cacheValue = _cache.StringGet(cacheKey); if (cacheValue != null) { //将当前获取到的缓存值,赋值给当前执行方法 var type = invocation.Method.ReturnType; var resultTypes = type.GenericTypeArguments; if (type.FullName == "System.Void") { return; } object response; if (typeof(Task).IsAssignableFrom(type)) { //返回Task<T> if (resultTypes.Any()) { var resultType = resultTypes.FirstOrDefault(); // 核心1,直接获取 dynamic 类型 dynamic temp = Newtonsoft.Json.JsonConvert.DeserializeObject(cacheValue, resultType); //dynamic temp = System.Convert.ChangeType(cacheValue, resultType); // System.Convert.ChangeType(Task.FromResult(temp), type); response = Task.FromResult(temp); } else { //Task 无返回方法 指定时间内不允许重新运行 response = Task.Yield(); } } else { // 核心2,要进行 ChangeType response = Convert.ChangeType(_cache.StringGet <object>(cacheKey), type); } invocation.ReturnValue = response; return; } //去执行当前的方法 invocation.Proceed(); //存入缓存 if (!string.IsNullOrWhiteSpace(cacheKey)) { object response; //Type type = invocation.ReturnValue?.GetType(); var type = invocation.Method.ReturnType; if (typeof(Task).IsAssignableFrom(type)) { var resultProperty = type.GetProperty("Result"); response = resultProperty.GetValue(invocation.ReturnValue); } else { response = invocation.ReturnValue; } if (response == null) { response = string.Empty; } _cache.StringSet(cacheKey, response, TimeSpan.FromMinutes(qCachingAttribute.AbsoluteExpiration)); } } else { invocation.Proceed();//直接执行被拦截方法 } }
public void IsSuccessSecondExport() { //准备环境 IEnumerable <SignPointActivityDto> actIdList = _signPointActivityRepository.Query <SignPointActivityDto>("select top 20 * from SignPointActivity"); actIdList.ShouldNotBeNull(); actIdList.Any().ShouldBe(true); foreach (var act in actIdList) { _actuatorAppService.StartWorkFor(act.Id); _redisHelper.StringSet(SUCCESSACTIDQUEUEKEY, act.Id.ToString(), 60 * 60 * 24 * 3); } var entity = actIdList.ToList().GetRange(16, 1).FirstOrDefault(); _redisHelper.StringSet(SUCCESSACTIDQUEUEKEY, entity.Id.ToString(), 60 * 60 * 24 * 3); var watch = new Stopwatch(); watch.Start(); //返回现场 _actuatorAppService.ExcuteWorker(actIdList.Select(x => x.Id)); //检测数据条目有没有跟数据库条目一致 string actStr = _redisHelper.StringGet(SUCCESSACTIDQUEUEKEY); int actId = 0; if (!string.IsNullOrEmpty(actStr)) { actId = Convert.ToInt32(actStr); } //缓存中的ID为最终添加的记录 int mongoDbId = _signActivityRepository.Queryable().Max(t => t.RowId); actId.ShouldBe(actIdList.Max(t => t.Id)); mongoDbId.ShouldBe(actId); //MongoDb中没有相关记录 IEnumerable <SignPointActivityDto> actList = _signPointActivityRepository.Query <SignPointActivityDto>( "select top 2 * from SignPointActivity where id>@id", new { id = actId }); foreach (var actItem in actList) { //活动数据不存在 var actMongo = _signActivityRepository.FirstOrDefault(actItem.ActID.ToString()); actMongo.ShouldBeNull(); //用户数据不存在 IEnumerable <string> openIdList = _signRecordsRepository .Query <string>("select distinct openid from SignRecords where ActivityID=@actID ", new { actID = actItem.Id }); foreach (var openId in openIdList) { //活动数据不存在 UserSignActivity actMongoTemp = _userSignActRepositoryMongo.FirstOrDefault(UserSignActivity.GetId(openId, actItem.ActID.ToString())); actMongoTemp.ShouldBeNull(); } } //清理测试数据 foreach (var act in actIdList) { _actuatorAppService.DeleteActData(act.Id, act.ActID.ToString()); } //重置缓存 _redisHelper.StringSet(SUCCESSACTIDQUEUEKEY, "0", 60 * 60 * 24 * 3); watch.Stop(); watch.ElapsedMilliseconds.ShouldBeLessThan(10000); }