Skip to content

Commit

Permalink
(#102) FileCache: async stream optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
ForNeVeR committed Aug 28, 2022
1 parent 107c4be commit e8e8153
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Emulsion.ContentProxy/FileCache.fs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,17 @@ type FileCache(logger: ILogger,
let getFilePath(cacheKey: string) =
Path.Combine(settings.Directory, FileCache.EncodeFileName(sha256, cacheKey))

let readFileOptions =
FileStreamOptions(Mode = FileMode.Open, Access = FileAccess.Read, Options = FileOptions.Asynchronous, Share = (FileShare.Read ||| FileShare.Delete))

let writeFileOptions =
FileStreamOptions(Mode = FileMode.CreateNew, Access = FileAccess.Write, Options = FileOptions.Asynchronous, Share = FileShare.None)

let getFromCache(cacheKey: string) = async {
let path = getFilePath cacheKey
return
if File.Exists path then
Some(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read|||FileShare.Delete))
Some(new FileStream(path, readFileOptions))
else
None
}
Expand Down Expand Up @@ -115,7 +121,7 @@ type FileCache(logger: ILogger,
logger.Information("Saving {Uri} to path {Path}…", uri, path)

do! async { // to limit the cachedFile scope
use cachedFile = new FileStream(path, FileMode.CreateNew, FileAccess.Write, FileShare.None)
use cachedFile = new FileStream(path, writeFileOptions)
do! Async.AwaitTask(stream.CopyToAsync(cachedFile, ct))
logger.Information("Download successful: \"{Uri}\" to \"{Path}\".")
}
Expand Down

0 comments on commit e8e8153

Please sign in to comment.