-
Notifications
You must be signed in to change notification settings - Fork 12.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cache when reading embedded resources
- Loading branch information
Showing
10 changed files
with
94 additions
and
83 deletions.
There are no files selected for viewing
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,61 @@ | ||
using System.Collections.Concurrent; | ||
using System.Reflection; | ||
|
||
namespace ServiceLib.Common; | ||
|
||
public static class EmbedUtils | ||
{ | ||
private static readonly string _tag = "EmbedUtils"; | ||
private static readonly ConcurrentDictionary<string, string> _dicEmbedCache = new(); | ||
|
||
/// <summary> | ||
/// Get embedded text resources | ||
/// </summary> | ||
/// <param name="res"></param> | ||
/// <returns></returns> | ||
public static string GetEmbedText(string res) | ||
{ | ||
if (_dicEmbedCache.TryGetValue(res, out var value)) | ||
{ | ||
return value; | ||
} | ||
var result = string.Empty; | ||
|
||
try | ||
{ | ||
var assembly = Assembly.GetExecutingAssembly(); | ||
using var stream = assembly.GetManifestResourceStream(res); | ||
ArgumentNullException.ThrowIfNull(stream); | ||
using StreamReader reader = new(stream); | ||
result = reader.ReadToEnd(); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Logging.SaveLog(_tag, ex); | ||
} | ||
|
||
_dicEmbedCache.TryAdd(res, result); | ||
return result; | ||
} | ||
|
||
/// <summary> | ||
/// Get local storage resources | ||
/// </summary> | ||
/// <returns></returns> | ||
public static string? LoadResource(string? res) | ||
{ | ||
try | ||
{ | ||
if (File.Exists(res)) | ||
{ | ||
return File.ReadAllText(res); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Logging.SaveLog(_tag, ex); | ||
} | ||
|
||
return null; | ||
} | ||
} |
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
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
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