Skip to content

Commit

Permalink
Add InMemory sample and improve Redis sample.
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelJCamara committed Oct 29, 2023
1 parent f812268 commit 341fe6e
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using Nebula.Caching.InMemorySample.Interfaces;

namespace Nebula.Caching.InMemorySample.Controllers;

Expand All @@ -13,14 +14,22 @@ public class WeatherForecastController : ControllerBase

private readonly ILogger<WeatherForecastController> _logger;

public WeatherForecastController(ILogger<WeatherForecastController> logger)
private readonly IService _service;

public WeatherForecastController(ILogger<WeatherForecastController> logger, IService service)
{
_logger = logger;
_service = service;
}

[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> 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)),
Expand Down
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.9"/>
<PackageReference Include="NebulaCaching.InMemory" Version="0.1.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0"/>
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -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();
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<IService, ServiceImplementation>();

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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -24,7 +27,8 @@ public ComplexObject AnotherMethod(string someParameter)
}

public bool SomeMethod()
{
{
Console.WriteLine($"Method {nameof(SomeMethod)} was executed.");
return false;
}
}

0 comments on commit 341fe6e

Please sign in to comment.