-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add InMemory sample and improve Redis sample.
- Loading branch information
1 parent
f812268
commit 341fe6e
Showing
8 changed files
with
134 additions
and
27 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
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
31 changes: 31 additions & 0 deletions
31
...Memory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/Interfaces/IService.cs
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,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; } | ||
} | ||
} |
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
65 changes: 42 additions & 23 deletions
65
docs/samples/InMemory/Nebula.Caching.InMemorySample/Nebula.Caching.InMemorySample/Program.cs
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 |
---|---|---|
@@ -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(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...la.Caching.InMemorySample/Nebula.Caching.InMemorySample/Services/ServiceImplementation.cs
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,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; | ||
} | ||
} |
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