async await 异步下载 异步代码加锁 lock 异步缓存
FTP异步下载代码:
/// <summary> /// 异步下载文件 </summary> <param name="ftpPath">ftp路径</param> <param name="ftpUserId">用户名<param name="ftpPassword">密码<param name="relativeFilePath">文件相对路径</param> public static async Task<MemoryStream> DownloadFileAsync(string ftpPath,string ftpUserId,1)">string ftpPassword,1)">string relativeFilePath) { FtpWebRequest request = null; try { LogTimeUtil log = new LogTimeUtil(); request = (FtpWebRequest)WebRequest.Create(new Uri(Path.Combine(ftpPath,relativeFilePath).Replace("\\",/"))); request.Credentials = NetworkCredential(ftpUserId,ftpPassword); request.Method = RETR; FtpWebResponse response = (FtpWebResponse)(await request.GetResponseAsync()); Stream responseStream = response.GetResponseStream(); MemoryStream stream = MemoryStream(); byte[] bArr = new byte[1024 * 1024]; int size = await responseStream.ReadAsync(bArr,1)">0,(int)bArr.Length); while (size > 0) { stream.Write(bArr,,size); size = )bArr.Length); } stream.Seek(ftputil.DownloadFileAsync 下载 filePath=" + relativeFilePath); return stream; } catch (Exception ex) { request.Abort(); LogUtil.Error(ex); } return ; }
异步缓存代码:
异步获取并缓存数据 <param name="cacheKey">键<param name="func">在此方法中初始化数据<param name="expirationSeconds">缓存过期时间(秒),0表示永不过期<param name="refreshCache">立即刷新缓存async Task<T> TryGetValueAsync<T>(string cacheKey,Func<Task<T>> func,1)">int expirationSeconds = bool refreshCache = false) { string pre = CacheHelper.TryGetValueAsync<T>; SemaphoreSlim sem = _dictSemaphoresForReadCache.GetorAdd(pre + cacheKey,1)">new SemaphoreSlim(1,1)">1)); sem.WaitAsync(); { object cacheValue = httpruntime.cache.Get(cacheKey); if (cacheValue != ) { (T)cacheValue; } else { T value = func(); if (expirationSeconds > ) { httpruntime.cache.Insert(cacheKey,value,Now.AddSeconds(expirationSeconds),Cache.NoSlidingExpiration); } { httpruntime.cache.Insert(cacheKey,value); } value; } } (Exception ex) { LogUtil.Error(ex); default(T); } finally { sem.Release(); } }
Web API 异步下载接口:
文件下载 <param name="filePath">文件存储相对路径[HttpGet] [Route(DownloadFileByPath)] async Task<HttpResponseMessage> DownloadFileByPath( filePath) { HttpResponseMessage response = HttpResponseMessage(); LogTimeUtil(); string fileName = Path.GetFileName(filePath); if (ftputil.FileExists(_ftpPath,_ftpUserId,_ftpPassword,filePath)) { await CacheHelper.TryGetValueAsync(DF9165DE189149258B34C405A2A7D7D1" + filePath,1)">async () => { MemoryStream ms = ftputil.DownloadFileAsync(_ftpPath,filePath); ms.ToArray(); },1)">180); response.Content = ByteArrayContent(bArr); response.Content.Headers.ContentLength = bArr.Length; response.Content.Headers.Contentdisposition = new ContentdispositionHeaderValue(attachment); response.Content.Headers.Contentdisposition.FileName = fileName; response.Content.Headers.ContentType = new MediaTypeHeaderValue(application/octet-stream); } { LogUtil.Error(DownloadFileByPath 错误:文件不存在); HttpResponseMessage(HttpStatusCode.NotFound); } log.LogTime(CommonController.DownloadFileByPath 下载 filePath= filePath); } (Exception ex) { LogUtil.Error(ex,1)">DownloadFileByPath 错误); HttpResponseMessage(HttpStatusCode.NotFound); } response; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。