From 341fe6eb7e3468fbe7ceddded78463e142543327 Mon Sep 17 00:00:00 2001 From: RafaelJCamara Date: Sun, 29 Oct 2023 10:15:19 +0100 Subject: [PATCH] Add InMemory sample and improve Redis sample. --- .../CacheRegistration/CacheRegistration.md | 4 +- .../Controllers/WeatherForecastController.cs | 11 +++- .../Interfaces/IService.cs | 31 +++++++++ .../Nebula.Caching.InMemorySample.csproj | 1 + .../Nebula.Caching.InMemorySample/Program.cs | 65 ++++++++++++------- .../Services/ServiceImplementation.cs | 34 ++++++++++ .../appsettings.Development.json | 9 +++ .../Services/ServiceImplementation.cs | 6 +- 8 files changed, 134 insertions(+), 27 deletions(-) create mode 100644 docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/Interfaces/IService.cs create mode 100644 docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/Services/ServiceImplementation.cs diff --git a/docs/documentation/InMemory/CacheRegistration/CacheRegistration.md b/docs/documentation/InMemory/CacheRegistration/CacheRegistration.md index 03854cf..a1d28b9 100644 --- a/docs/documentation/InMemory/CacheRegistration/CacheRegistration.md +++ b/docs/documentation/InMemory/CacheRegistration/CacheRegistration.md @@ -11,7 +11,7 @@ public class Program // ... builder.Host.UseNebulaCaching(); - builder.Services.AddInMemoryChache(new Configurations + builder.Services.AddInMemoryChache(new InMemoryConfigurations { //some amazing optional configuration options }); @@ -26,7 +26,7 @@ The following options are, as of today, supported: ## ConfigurationSection -String that represents the section in your _appsettings.json_ where the cache configuration will be placed. By default the value here is **_"InMemory"_**.Example of configurations placed in such section may include the cache duration for your keys (if you end up choosing this path). Please refer to [this](../AttributeUsage/AttributeUsage.md) for more information on cache duration options. +String that represents the section in your _appsettings.json_ where the cache configuration will be placed. By default the value here is **_"InMemory"_**. Example of configurations placed in such section may include the cache duration for your keys (if you end up choosing this path). Please refer to [this](../AttributeUsage/AttributeUsage.md) for more information on cache duration options. ## DefaultCacheDurationInSeconds diff --git a/docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/Controllers/WeatherForecastController.cs b/docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/Controllers/WeatherForecastController.cs index c353fd9..974a260 100644 --- a/docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/Controllers/WeatherForecastController.cs +++ b/docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/Controllers/WeatherForecastController.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Mvc; +using Nebula.Caching.InMemorySample.Interfaces; namespace Nebula.Caching.InMemorySample.Controllers; @@ -13,14 +14,22 @@ public class WeatherForecastController : ControllerBase private readonly ILogger _logger; - public WeatherForecastController(ILogger logger) + private readonly IService _service; + + public WeatherForecastController(ILogger logger, IService service) { _logger = logger; + _service = service; } [HttpGet(Name = "GetWeatherForecast")] public IEnumerable Get() { + _service.MagicMethod(); + _service.AnotherMethod("Rafael"); + _service.OneMethod("Rafael", 1996); + _service.SomeMethod(); + return Enumerable.Range(1, 5).Select(index => new WeatherForecast { Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), diff --git a/docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/Interfaces/IService.cs b/docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/Interfaces/IService.cs new file mode 100644 index 0000000..d94192a --- /dev/null +++ b/docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/Interfaces/IService.cs @@ -0,0 +1,31 @@ + +using Nebula.Caching.InMemory.Attributes; + +namespace Nebula.Caching.InMemorySample.Interfaces +{ + public interface IService + { + //this method will use the cache duration from settings.json config file + [InMemoryCache] + string OneMethod(string name, int year); + + //this method will use the cache duration defined in the settings.json config file, not the default cache duration + //uses custom name + [InMemoryCache(CustomCacheName = "MagicName")] + int MagicMethod(); + + //this method will use the cache duration defined on the constructor + [InMemoryCache(CacheDurationInSeconds = 120)] + ComplexObject AnotherMethod(string someParameter); + + //this method will use the cache duration defined for this cache group on our settings.json config file + [InMemoryCache(CacheGroup = "GroupA")] + bool SomeMethod(); + } + + public class ComplexObject + { + public string Name { get; set; } = string.Empty; + public int Age { get; set; } + } +} \ No newline at end of file diff --git a/docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample.csproj b/docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample.csproj index 1b490bf..6a6d241 100644 --- a/docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample.csproj +++ b/docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample.csproj @@ -8,6 +8,7 @@ + diff --git a/docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/Program.cs b/docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/Program.cs index 8264bac..5a1a1ba 100644 --- a/docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/Program.cs +++ b/docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/Program.cs @@ -1,25 +1,44 @@ -var builder = WebApplication.CreateBuilder(args); +using Nebula.Caching.Common.Extensions; +using Nebula.Caching.InMemory.Extensions; +using Nebula.Caching.InMemory.Settings; +using Nebula.Caching.InMemorySample.Interfaces; +using Nebula.Caching.InMemorySample.Services; -// Add services to the container. - -builder.Services.AddControllers(); -// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle -builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); - -var app = builder.Build(); - -// Configure the HTTP request pipeline. -if (app.Environment.IsDevelopment()) +public class Program { - app.UseSwagger(); - app.UseSwaggerUI(); -} - -app.UseHttpsRedirection(); - -app.UseAuthorization(); - -app.MapControllers(); - -app.Run(); \ No newline at end of file + public static void Main(string[] args) + { + var builder = WebApplication.CreateBuilder(args); + + // Start cache configuration + builder.Host.UseNebulaCaching(); + builder.Services.AddInMemoryChache(new InMemoryConfigurations + { + ConfigurationSection = "InMemoryConfig", + DefaultCacheDurationInSeconds = 3600 + }); + // End cache configuration + + builder.Services.AddScoped(); + + builder.Services.AddControllers(); + builder.Services.AddEndpointsApiExplorer(); + builder.Services.AddSwaggerGen(); + + var app = builder.Build(); + + if (app.Environment.IsDevelopment()) + { + app.UseSwagger(); + app.UseSwaggerUI(); + } + + app.UseHttpsRedirection(); + + app.UseAuthorization(); + + app.MapControllers(); + + app.Run(); + } +} \ No newline at end of file diff --git a/docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/Services/ServiceImplementation.cs b/docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/Services/ServiceImplementation.cs new file mode 100644 index 0000000..19660f2 --- /dev/null +++ b/docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/Services/ServiceImplementation.cs @@ -0,0 +1,34 @@ +using Nebula.Caching.InMemorySample.Interfaces; + +namespace Nebula.Caching.InMemorySample.Services; + +public class ServiceImplementation : IService +{ + public string OneMethod(string name, int year) + { + Console.WriteLine($"Method {nameof(OneMethod)} executed."); + return $"{name} was born in {year}."; + } + + public int MagicMethod() + { + Console.WriteLine($"Method {nameof(MagicMethod)} executed."); + return 2023; + } + + public ComplexObject AnotherMethod(string someParameter) + { + Console.WriteLine($"Method {nameof(AnotherMethod)} executed."); + return new ComplexObject + { + Age = 2023, + Name = someParameter + }; + } + + public bool SomeMethod() + { + Console.WriteLine($"Method {nameof(SomeMethod)} executed."); + return false; + } +} \ No newline at end of file diff --git a/docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/appsettings.Development.json b/docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/appsettings.Development.json index 0c208ae..ce82401 100644 --- a/docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/appsettings.Development.json +++ b/docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/appsettings.Development.json @@ -4,5 +4,14 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } + }, + "InMemoryConfig" : { + "CacheSettings": { + "MagicName" : "04:00:00", + "Nebula-Caching-InMemorySample-Services--OneMethod--{name}--{year}" : "01:02:03" + }, + "CacheGroupSettings": { + "GroupA" : "02:23:15" + } } } diff --git a/docs/samples/Redis/Nebula.Caching.RedisSample/Nebula.Caching.RedisSample/Services/ServiceImplementation.cs b/docs/samples/Redis/Nebula.Caching.RedisSample/Nebula.Caching.RedisSample/Services/ServiceImplementation.cs index ba9da73..760f384 100644 --- a/docs/samples/Redis/Nebula.Caching.RedisSample/Nebula.Caching.RedisSample/Services/ServiceImplementation.cs +++ b/docs/samples/Redis/Nebula.Caching.RedisSample/Nebula.Caching.RedisSample/Services/ServiceImplementation.cs @@ -6,16 +6,19 @@ public class ServiceImplementation : IService { public string OneMethod(string name, int year) { + Console.WriteLine($"Method {nameof(OneMethod)} was executed."); return $"{name} was born in {year}."; } public int MagicMethod() { + Console.WriteLine($"Method {nameof(MagicMethod)} was executed."); return 2023; } public ComplexObject AnotherMethod(string someParameter) { + Console.WriteLine($"Method {nameof(AnotherMethod)} was executed."); return new ComplexObject { Age = 2023, @@ -24,7 +27,8 @@ public ComplexObject AnotherMethod(string someParameter) } public bool SomeMethod() - { + { + Console.WriteLine($"Method {nameof(SomeMethod)} was executed."); return false; } } \ No newline at end of file