public L2CacheTest() { IServiceCollection services = new ServiceCollection(); services.AddMemoryServices(options => { options.DBConfig.SizeLimit = 100; options.DBConfig.ExpirationScanFrequency = 10; }); services.AddRedisServices(options => { options.DBConfig.Password = "******"; options.DBConfig.Endpoints.Add(new Cache.Core.Configurations.ServerEndPoint() { Port = 6379, Host = "r-wz952aaada291544pd.redis.rds.aliyuncs.com" }); }).AddRedisL2Services() ; services.AddLogging(); IServiceProvider serviceProvider = services.BuildServiceProvider(); ICachingProviderFactory _factory = serviceProvider.GetService <ICachingProviderFactory>(); _provider = _factory.GetCachingProvider(CachingConstValue.DefaultInMemoryName); _redis = _factory.GetCachingProvider(CachingConstValue.DefaultRedisName); _l2CacheProvider = serviceProvider.GetService <IL2CacheProvider>(); _defaultTs = TimeSpan.FromSeconds(600); }
public RedisAndMemoryProvider(ICachingProviderFactory providerFactory , ILoggerFactory loggerFactory = null) { _providerFactory = providerFactory; _memoryProvider = _providerFactory.GetCachingProvider(CachingConstValue.DefaultInMemoryName); _redisProvider = _providerFactory.GetCachingProvider(CachingConstValue.DefaultRedisName); _logger = loggerFactory?.CreateLogger <RedisAndMemoryProvider>(); }
public HomeController(ICachingProviderFactory providerFactory, IL2CacheProvider l2CacheProvider) { _providerFactory = providerFactory; _l2CacheProvider = l2CacheProvider; _memoryProvider = _providerFactory.GetCachingProvider(CachingConstValue.DefaultInMemoryName); _redisProvider = _providerFactory.GetCachingProvider(CachingConstValue.DefaultRedisName); }
public async Task <LoginOutput> LoginAsync([FromBody] LoginInput input) { var redis = _cachingProviderFactory.GetCachingProvider("default_redis"); var redis_img_code = await redis.GetAsync <string>($"ImgCode:{input.Guid}"); if (redis_img_code.SafeString().ToLower() != input.ImgCode.SafeString().ToLower()) { throw new BucketException("GO_2003", "图形验证码错误"); } // 短信验证 // 用户验证 var userInfo = _superDbContext.Queryable <UserInfo>().First(it => it.UserName == input.UserName); if (userInfo == null) { throw new BucketException("GO_0004007", "账号不存在"); } if (userInfo.State != 1) { throw new BucketException("GO_0004008", "账号状态异常"); } if (userInfo.Password != Encrypt.SHA256(input.Password + userInfo.Salt)) { throw new BucketException("GO_4009", "账号或密码错误"); } // 用户角色 var roleList = _superDbContext.Queryable <RoleInfo, UserRoleInfo>((role, urole) => new object[] { JoinType.Inner, role.Id == urole.RoleId }) .Where((role, urole) => urole.Uid == userInfo.Id) .Where((role, urole) => role.IsDel == false) .Select((role, urole) => new { role.Key }) .ToList(); // token返回 var token = _authService.CreateAccessToken(new UserTokenDto { Email = userInfo.Email, Id = userInfo.Id, Mobile = userInfo.Mobile, RealName = userInfo.RealName, Ids = userInfo.Id.ToString() }, roleList.Select(it => it.Key).ToList()); return(new LoginOutput { Data = new { AccessToken = $"Bearer {token}", Expire = _authService.GetExpireInValue(4), RealName = userInfo.RealName.SafeString(), Mobile = userInfo.Mobile.SafeString(), userInfo.Id } }); }
public async Task <IActionResult> ValidateCode(string guid, int width = 100, int height = 32) { if (guid.IsEmpty()) { throw new BucketException("pz_001", "请输入用户标识"); } var code = Bucket.Utility.Helpers.Randoms.CreateRandomValue(4, false); var st = Bucket.ImgVerifyCode.VerifyCode.CreateByteByImgVerifyCode(code, width, height); var redis = _cachingProviderFactory.GetCachingProvider("default"); await redis.SetAsync($"ImgCode", code, new TimeSpan(0, 5, 0)); return(File(st, "image/jpeg")); }
public async Task <BaseOutput> SyncApiGatewayConfigurationToRedis([FromQuery] SyncApiGatewayConfigurationInput input) { var configInfo = _adminDbContext.Queryable <ApiGatewayConfigurationModel>().First(it => it.GatewayId == input.GatewayId); if (configInfo != null) { var data = GetGatewayData(input.GatewayId); var redis = _cachingProviderFactory.GetCachingProvider("default_redis"); await redis.SetAsync($"ApiGateway:{configInfo.GatewayKey}", data, TimeSpan.MaxValue); } return(new BaseOutput { }); }
/// <summary> /// 短息发送 /// </summary> /// <param name="mobile"></param> /// <param name="smsTemplateName"></param> public async Task <string> SendSmsCodeAsync(string mobile, string smsTemplateName) { var errCountKey = string.Format(CacheKeys.SmsCodeVerifyErr, mobile); var sendCountKey = string.Format(CacheKeys.SmsCodeSendIdentity, mobile); var loginCodeKey = string.Format(CacheKeys.SmsCodeLoginCode, mobile); var redis = _cachingProviderFactory.GetCachingProvider("default_redis"); // 错误次数过多 var errCount = await redis.GetAsync <int>(errCountKey); if (errCount > 5) { throw new BucketException("GO_0005055", "登陆错误次数过多,请30分钟后再试"); } // 验证一分钟发一条 if (await redis.ExistsAsync(sendCountKey)) { throw new BucketException("GO_2001", "一分钟只能发送一条短信"); } // 生成验证码 string loginCode = Randoms.CreateRandomValue(6, true); // 基础键值 // @event.MobIp.Split(',')[0] 当多层代理时x-forwarded-for多ip // 第三方短信发送 // 验证码缓存 await redis.SetAsync(loginCodeKey, loginCode, new TimeSpan(0, 0, 0, 300)); // 发送人缓存(60秒发一次) await redis.SetAsync(sendCountKey, loginCode, new TimeSpan(0, 0, 0, 60)); return(loginCode); }