This repository has been archived by the owner on Apr 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
95 changed files
with
5,055 additions
and
1,412 deletions.
There are no files selected for viewing
Submodule Atomex.Client.Core
updated
141 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
|
||
using Serilog; | ||
|
||
using Atomex.Common; | ||
using Atomex.Cryptography; | ||
|
||
namespace Atomex.Client.Wpf.Common | ||
{ | ||
public static class FileCache | ||
{ | ||
public const int MaxCacheFileSize = 1024 * 1024; // 1 Mb | ||
|
||
public static string DefaultCacheFolder { get; set; } = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "cache"); | ||
|
||
public static byte[] Get(string name, TimeSpan cachePeriod) | ||
{ | ||
var key = Hex.ToHexString(Sha256.Compute(Encoding.UTF8.GetBytes(name))); | ||
var fileExtension = "cache"; | ||
var fileName = $"{key}.{fileExtension}"; | ||
var filePath = Path.Combine(DefaultCacheFolder, fileName); | ||
|
||
try | ||
{ | ||
if (!Directory.Exists(DefaultCacheFolder)) | ||
Directory.CreateDirectory(DefaultCacheFolder); | ||
|
||
var fileInfo = new FileInfo(filePath); | ||
|
||
if (fileInfo.Length > MaxCacheFileSize) | ||
return null; | ||
|
||
if (cachePeriod != TimeSpan.MaxValue && fileInfo.LastWriteTimeUtc + cachePeriod < DateTime.UtcNow) | ||
return null; | ||
|
||
return File.ReadAllBytes(filePath); | ||
} | ||
catch (Exception e) | ||
{ | ||
Log.Error(e, "FileCache.Get error."); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public static void Set(string name, byte[] data) | ||
{ | ||
var key = Hex.ToHexString(Sha256.Compute(Encoding.UTF8.GetBytes(name))); | ||
var fileExtension = "cache"; | ||
var fileName = $"{key}.{fileExtension}"; | ||
var filePath = Path.Combine(DefaultCacheFolder, fileName); | ||
|
||
try | ||
{ | ||
File.WriteAllBytes(filePath, data); | ||
} | ||
catch (Exception e) | ||
{ | ||
Log.Error(e, "FileCache.Set error."); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.