/// <summary> /// 获得文件Byte数组 /// </summary> /// <param name="filePath">文件路径</param> /// <param name="ifModifiedSince"></param> /// <returns></returns> protected virtual async Task <(FileCacheModel fileCacheModel, bool isCache)> GetFileBytesAsync(string filePath, ICharSequence ifModifiedSince) { while (_readingFiles.Any(m => m.Key.Equals(filePath))) { await Task.Delay(1000); } if (ifModifiedSince != null) { return(await GetFileBytesFromCacheAsync(filePath, ifModifiedSince)); } FileCacheModel fileCacheModel = await GetFileBytesFromFileAsync(filePath); return(fileCacheModel, false); }
/// <summary> /// 从文件获取 /// </summary> /// <param name="filePath"></param> /// <returns></returns> protected virtual async Task <FileCacheModel> GetFileBytesFromFileAsync(string filePath) { _cacheManager.Remove($"{_cacheKey}{filePath}"); if (!File.Exists(filePath)) { throw new DotNettyServerException("文件不存在"); } _readingFiles.TryAdd(filePath, null); var fileCacheModel = new FileCacheModel { CacheTime = DateTime.Now, Body = await File.ReadAllBytesAsync(filePath) }; _cacheManager.SetByAbsolute($"{_cacheKey}{filePath}", fileCacheModel, CacheSecond, DateTimeTypeEnum.Second); _readingFiles.Remove(filePath, out _); return(fileCacheModel); }