-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HttpContext and Caching extension methods added
- Loading branch information
vahid
committed
Nov 25, 2024
1 parent
b90c878
commit 9c92744
Showing
7 changed files
with
88 additions
and
2 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,11 @@ | ||
namespace Jinget.Core.Attributes; | ||
|
||
/// <summary> | ||
/// name used as cache key. this name should be unique among models and its corresponding viewmodel to correct cache invalidation | ||
/// </summary> | ||
/// <param name="identifier"></param> | ||
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = false)] | ||
public class CacheTypeIdentifier(string identifier) : Attribute | ||
{ | ||
public string Identifier { get; set; } = identifier; | ||
} |
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,13 @@ | ||
namespace Jinget.Core.Enumerations; | ||
|
||
public enum CacheEntryType : byte | ||
{ | ||
[Description("specific")] | ||
SpecificItemWithId, | ||
|
||
[Description("firstorsingle")] | ||
FirstOrSingleItem, | ||
|
||
[Description("list")] | ||
ListItems | ||
} |
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,18 @@ | ||
namespace Jinget.Core.Utilities; | ||
|
||
public static class CachingUtility | ||
{ | ||
public static string GetKeyName(Type modelType, CacheEntryType type, object? id = null) | ||
{ | ||
var cacheTypeName = modelType.GetCustomAttribute<CacheTypeIdentifier>()?.Identifier.Replace("ViewModel", "") | ||
.Replace("Model", "") ?? string.Empty; | ||
if (string.IsNullOrWhiteSpace(cacheTypeName)) | ||
{ | ||
cacheTypeName = $"{modelType.Name.Replace("ViewModel", "").Replace("Model", "")}"; | ||
} | ||
|
||
return type == CacheEntryType.SpecificItemWithId | ||
? $"{cacheTypeName}_{CacheEntryType.SpecificItemWithId.GetDescription()}_{id}".ToLower() | ||
: $"{cacheTypeName}_{type.GetDescription()}".ToLower(); | ||
} | ||
} |
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,29 @@ | ||
namespace Jinget.Core.Tests.Utilities; | ||
|
||
[TestClass] | ||
public class CachingUtilityTests | ||
{ | ||
[TestMethod] | ||
public void should_return_cache_key_for_specific_cache_type() | ||
{ | ||
var expectedKey = "my-key_specific_1"; | ||
var result = CachingUtility.GetKeyName(typeof(CacheSample), CacheEntryType.SpecificItemWithId, 1); | ||
Assert.AreEqual(expectedKey, result); | ||
} | ||
|
||
[TestMethod] | ||
public void should_return_cache_key_for_first_or_single_cache_type() | ||
{ | ||
var expectedKey = "my-key_firstorsingle"; | ||
var result = CachingUtility.GetKeyName(typeof(CacheSample), CacheEntryType.FirstOrSingleItem); | ||
Assert.AreEqual(expectedKey, result); | ||
} | ||
|
||
[TestMethod] | ||
public void should_return_cache_key_for_list_cache_type() | ||
{ | ||
var expectedKey = "my-key_list"; | ||
var result = CachingUtility.GetKeyName(typeof(CacheSample), CacheEntryType.ListItems); | ||
Assert.AreEqual(expectedKey, result); | ||
} | ||
} |
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,9 @@ | ||
using Jinget.Core.Attributes; | ||
|
||
namespace Jinget.Core.Tests._BaseData; | ||
|
||
[CacheTypeIdentifier("My-Key")] | ||
public class CacheSample | ||
{ | ||
public int Id { get; set; } | ||
} |
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