private async Task <List <Permission> > GetListInCacheInternalAsync() { var list = await _cache.GetJsonAsync <List <Permission> >(ListCacheKey); if (list == null) { list = await _repository.GetListAsync(); await _cache.SetJsonAsync <List <Permission> >(ListCacheKey, list); } return(list); /* * if (!_cache.TryGetValue(PermissionListCacheKey, out List<Permission> permissions)) * { * // Key not in cache, so get data. * permissions = await _repository.GetListAsync(); * * // Set cache options. * var cacheEntryOptions = new MemoryCacheEntryOptions() * // Keep in cache for this time, reset time if accessed. * .SetSlidingExpiration(TimeSpan.FromDays(30)); * * // Save data in cache. * _cache.Set(PermissionListCacheKey, permissions, cacheEntryOptions); * } * * return permissions; */ }
private async Task <List <Group> > GetListInCacheInternalAsync() { var groups = await _cache.GetJsonAsync <List <Group> >(ListCacheKey); if (groups == null) { groups = await _manager.GetListAsync(); CacheList(groups); } return(groups); /* * if (!_cache.TryGetValue(GroupListCacheKey, out List<Group> groups)) * { * // Key not in cache, so get data. * groups = await _manager.GetListAsync(); * * // Set cache options. * var cacheEntryOptions = new MemoryCacheEntryOptions() * // Keep in cache for this time, reset time if accessed. * .SetSlidingExpiration(TimeSpan.FromDays(30)); * * // Save data in cache. * _cache.Set(GroupListCacheKey, groups, cacheEntryOptions); * } * * return groups; */ }
private async Task <List <RegionInfo> > GetListInCacheInternalAsync() { /* * var list = await _distributedCache.GetJsonAsync<List<RegionInfo>>(ListCacheKey); * if (list == null) * { * list = await _repository.GetRegionInfoListAsync(); * await _distributedCache.SetJsonAsync<List<RegionInfo>>(ListCacheKey, list); * } * return list; */ if (!_memoryCache.TryGetValue(ListCacheKey, out List <RegionInfo> list)) { // Key not in cache, so get data. list = await _distributedCache.GetJsonAsync <List <RegionInfo> >(ListCacheKey); if (list == null) { list = await _repository.GetRegionInfoListAsync(); await _distributedCache.SetJsonAsync <List <RegionInfo> >(ListCacheKey, list); } // Set cache options. var cacheEntryOptions = new MemoryCacheEntryOptions() // Keep in cache for this time, reset time if accessed. .SetSlidingExpiration(TimeSpan.FromDays(30)); // Save data in cache. _memoryCache.Set(ListCacheKey, list, cacheEntryOptions); } return(list); }
private async Task <UserInfo> GetNormalItemByUserIdInCacheInternalAsync(int userId) { var cacheKey = UserCacheKeyFormat.FormatWith(userId); var userInfo = await _cache.GetJsonAsync <UserInfo>(cacheKey); if (userInfo == null) { userInfo = await _manager.GetItemByUserIdAsync(userId, UserStatus.Normal); CacheNormalUser(userInfo); } return(userInfo); /* * if (!_cache.TryGetValue(cacheKey, out UserInfo userInfo)) * { * // Key not in cache, so get data. * userInfo = await _manager.GetItemByUserIdAsync(userId, UserStatus.Normal); * if(userInfo == null) return null; * * // Set cache options. * var cacheEntryOptions = new MemoryCacheEntryOptions() * // Keep in cache for this time, reset time if accessed. * .SetSlidingExpiration(TimeSpan.FromDays(30)); * * // Save data in cache. * _cache.Set(cacheKey, userInfo, cacheEntryOptions); * } * * return userInfo; */ }
private async Task <Bulletin> GetItemInCacheInternalAsync() { var bulletin = await _cache.GetJsonAsync <Bulletin>(CacheKey); if (bulletin == null) { bulletin = await _repository.GetItemAsync(); await _cache.SetJsonAsync <Bulletin>(CacheKey, bulletin); } return(bulletin); /* * if (!_cache.TryGetValue(CacheKey, out Bulletin bulletin)) * { * // Key not in cache, so get data. * bulletin = await _repository.GetItemAsync(); * * // Set cache options. * var cacheEntryOptions = new MemoryCacheEntryOptions() * // Keep in cache for this time, reset time if accessed. * .SetSlidingExpiration(TimeSpan.FromDays(30)); * * // Save data in cache. * _cache.Set(CacheKey, bulletin, cacheEntryOptions); * } * * return bulletin; */ }
private async Task <List <Role> > GetListInCacheInternalAsync() { var roles = await _cache.GetJsonAsync <List <Role> >(RoleListCacheKey); if (roles == null) { roles = await _manager.GetListAsync(); CleanupCache(); } return(roles); }
private async Task <List <Role> > GetListInCacheInternalAsync() { var roles = await _cache.GetJsonAsync <List <Role> >(RoleListCacheKey); if (roles == null) { roles = await _repository.GetListAsync(); await _cache.SetJsonAsync <List <Role> >(RoleListCacheKey, roles); } return(roles); }
private async Task <List <RegionInfoBase> > GetListInCacheInternalAsync() { var list = await _cache.GetJsonAsync <List <RegionInfoBase> >(CacheKey); if (list == null) { list = await _repository.GetRegionInfoBaseListAsync(); await _cache.SetJsonAsync <List <RegionInfoBase> >(CacheKey, list); } return(list); }
public async Task <RateLimitCacheEntry> CanAccessResource(string policyId, int limitPerHour, IClientIdentifier clientId) { var key = $"{policyId}:{clientId.IdentifierString}"; var existing = await _cache.GetJsonAsync <RateLimitCacheEntry>(key) ?? new RateLimitCacheEntry { Remaining = limitPerHour, Resets = DateTime.UtcNow + TimeSpan.FromHours(1), }; var updated = new RateLimitCacheEntry { Remaining = existing.Remaining - 1, Resets = existing.Resets, }; await _cache.SetJsonAsync(key, updated, new DistributedCacheEntryOptions { AbsoluteExpiration = new DateTimeOffset(updated.Resets, TimeSpan.Zero), }); return(existing); }
public async Task <bool> GetMobileValidationCodeAsync(GetMobileValidationCodeInput getMobileValidationCodeInput, ModelStateDictionary modelState) { if (getMobileValidationCodeInput.Type == MobileValidationCodeType.Register) { if (await _manager.IsExistsMobileAsync(getMobileValidationCodeInput.Mobile)) { modelState.AddModelError("Mobile", "手机号码已经被使用"); return(false); } } else if (getMobileValidationCodeInput.Type == MobileValidationCodeType.Login || getMobileValidationCodeInput.Type == MobileValidationCodeType.ChangeMobile) { if (!await _manager.IsExistsMobileAsync(getMobileValidationCodeInput.Mobile)) { modelState.AddModelError("Mobile", "手机号码尚未注册"); return(false); } } string validationCode = null; var cacheKey = MobileValidationCodeCacheKeyFormat.FormatWith(getMobileValidationCodeInput.Mobile); var mobileValidationCode = await _cache.GetJsonAsync <MobileValidationCode>(cacheKey); var now = DateTime.Now; if (mobileValidationCode != null) { if (now - mobileValidationCode.CreationTime < TimeSpan.FromSeconds(_mobileValidationCodeSettings.RequestInterval)) { modelState.AddModelError("Mobile", "请求过于频繁,请稍后再试"); return(false); } if (!mobileValidationCode.ValidationCode.IsNullOrWhiteSpace() && mobileValidationCode.Type == getMobileValidationCodeInput.Type /* 验证码用途未发生更改 */ && mobileValidationCode.ExpirationDate <= now /* 验证码没到期 */ && mobileValidationCode.VerifyTimes < mobileValidationCode.MaxVerifyTimes /* 验证码在合理使用次数内 */) { // 继续沿用之前的验证码 validationCode = mobileValidationCode.ValidationCode; } } if (validationCode == null) { validationCode = GenerateMobileValidationCode(_mobileValidationCodeSettings.CodeLength); mobileValidationCode = new MobileValidationCode { Mobile = getMobileValidationCodeInput.Mobile, Type = getMobileValidationCodeInput.Type, ValidationCode = validationCode, ExpirationDate = now.AddSeconds(_mobileValidationCodeSettings.Expiration), MaxVerifyTimes = _mobileValidationCodeSettings.MaxVerifyTimes, VerifyTimes = 0, FinishVerifyDate = null, CreationTime = now, }; await _cache.SetJsonAsync(cacheKey, mobileValidationCode, new DistributedCacheEntryOptions { SlidingExpiration = TimeSpan.FromSeconds(_mobileValidationCodeSettings.Expiration) }); } var sms = "{\"code\":\"" + validationCode + "\",\"time\":\"" + (_mobileValidationCodeSettings.Expiration / 60) + "\"}"; return(await _smsSender.SendAsync(getMobileValidationCodeInput.Mobile, sms)); }