Skip to content

Commit 984df6f

Browse files
committed
ReviewViewer: Hash combined cache key to optimize storage, lookup performance, and avoid exceeding maximum key length limits
1 parent 698de82 commit 984df6f

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

source/Common/PluginsCommon/Extensions/StringExtensions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ internal static byte[] MD5Bytes(this string s)
4141
}
4242
}
4343

44+
public static string ToHashedKey(this string input)
45+
{
46+
using (var sha = Security.Cryptography.SHA256.Create())
47+
{
48+
var inputBytes = Encoding.UTF8.GetBytes(input);
49+
var hashBytes = sha.ComputeHash(inputBytes);
50+
return Convert.ToBase64String(hashBytes);
51+
}
52+
}
53+
4454
internal static string GetSHA256Hash(this string input)
4555
{
4656
using (var sha = Security.Cryptography.SHA256.Create())
@@ -50,6 +60,14 @@ internal static string GetSHA256Hash(this string input)
5060
}
5161
}
5262

63+
public static byte[] GetSHA256HashByte(this string input)
64+
{
65+
using (var sha = System.Security.Cryptography.SHA256.Create())
66+
{
67+
return sha.ComputeHash(Encoding.UTF8.GetBytes(input));
68+
}
69+
}
70+
5371
internal static string UrlEncode(this string str)
5472
{
5573
if (string.IsNullOrEmpty(str))

source/Generic/ReviewViewer/Application/SteamReviewsCoordinator.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ public async Task<ReviewsResponseDto> GetReviewsAsync(
3737
string cursor = "*")
3838
{
3939
var cacheKey = $"{appId}_{Serialization.ToJson(options)}_{cursor}";
40-
var existingCache = _repository.Find(x => x.CacheKey == cacheKey).FirstOrDefault();
40+
var hashedCacheKey = cacheKey.ToHashedKey();
41+
var existingCache = _repository.Find(x => x.CacheKey == hashedCacheKey).FirstOrDefault();
4142
if (!forceRefresh && existingCache != null && (DateTime.UtcNow - existingCache.CreatedAt) < _cacheDuration)
4243
{
4344
return existingCache.Response;
@@ -53,7 +54,7 @@ public async Task<ReviewsResponseDto> GetReviewsAsync(
5354

5455
var newRecord = new ReviewsResponseRecord
5556
{
56-
CacheKey = cacheKey,
57+
CacheKey = hashedCacheKey,
5758
Response = freshData
5859
};
5960

0 commit comments

Comments
 (0)