Skip to content

Commit

Permalink
HttpContext and Caching extension methods added
Browse files Browse the repository at this point in the history
  • Loading branch information
vahid committed Nov 25, 2024
1 parent b90c878 commit 9c92744
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 2 deletions.
11 changes: 11 additions & 0 deletions 01-Core/Jinget.Core/Attributes/CacheTypeIdentifier.cs
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;
}
13 changes: 13 additions & 0 deletions 01-Core/Jinget.Core/Enumerations/CacheEntryType.cs
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ public static class HttpContextExtensions
/// Check if given request is a multipart request or not
/// </summary>
public static bool IsMultipartContentType(this HttpContext context) =>
context.Request.GetTypedHeaders() != null &&
context.Request.GetTypedHeaders().ContentType != null &&
context.Request.GetTypedHeaders().ContentType.MediaType.Value.ToLower().StartsWith("multipart/form-data");

Expand All @@ -17,4 +16,7 @@ public static string GetIpAddress(this HttpContext context) =>
context.Connection.RemoteIpAddress == null
? "Unknown"
: context.Connection.RemoteIpAddress.ToString();

public static bool EndpointIsAuthorized(this HttpContext httpContext)
=> httpContext.GetEndpoint()?.Metadata.GetMetadata<AuthorizeAttribute>() != null;
}
18 changes: 18 additions & 0 deletions 01-Core/Jinget.Core/Utilities/CachingUtility.cs
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();
}
}
29 changes: 29 additions & 0 deletions Tests/Jinget.Core.Tests/Utilities/CachingUtilityTests.cs
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);
}
}
9 changes: 9 additions & 0 deletions Tests/Jinget.Core.Tests/_BaseData/CacheSample.cs
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; }
}
6 changes: 5 additions & 1 deletion Tests/Jinget.WebAPI.Tests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,13 @@
logger.LogError(httpContextAccessor.HttpContext, "Sample Error message1!");
logger.LogError(httpContextAccessor.HttpContext, "Sample Error message2!");
logger.LogError(httpContextAccessor.HttpContext, "Sample Error message3!");

throw new Exception("Sample exception");
});
app.MapGet("unauthorized", (IHttpContextAccessor httpContextAccessor, ILogger<SampleModel> logger) =>
{
return Results.Unauthorized();
});
app.MapGet("successlog", () => "Hello vahid");
app.MapGet("detailedlog", () => "Sample Success");
app.MapPost("save", (viewmodel vm) => vm);
Expand Down

0 comments on commit 9c92744

Please sign in to comment.