forked from ZiggyCreatures/FusionCache
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
113 lines (91 loc) · 2.54 KB
/
Copy pathProgram.cs
File metadata and controls
113 lines (91 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using Microsoft.AspNetCore.OutputCaching;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using Serilog;
using ZiggyCreatures.Caching.Fusion;
using ZiggyCreatures.Caching.Fusion.Serialization.SystemTextJson;
namespace WebAppTest
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Is(Serilog.Events.LogEventLevel.Debug)
.WriteTo.Console()
.CreateLogger();
var services = builder.Services;
services.AddLogging(configure => configure.AddSerilog());
// PICK A CACHE NAME
var cacheName = "Bar";
// MEMORY CACHE (DIRECT REFERENCE, TO SIMULATE A COLD START)
var memoryCache = new MemoryCache(new MemoryCacheOptions());
// ADD AND CONFIGURE FUSION CACHE
services.AddFusionCache(cacheName)
.WithLogger(sp => sp.GetRequiredService<ILogger<FusionCache>>())
.WithMemoryCache(memoryCache)
.WithSerializer(new FusionCacheSystemTextJsonSerializer())
.WithDistributedCache(new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions())));
// ADD AND CONFIGURE FUSION OUTPUT CACHE
services.AddFusionOutputCache(options =>
{
options.CacheName = cacheName;
});
// ADD AND OUTPUT CACHE
services.AddOutputCache(options =>
{
options.AddPolicy("Expire2", builder =>
builder.Expire(TimeSpan.FromSeconds(2))
);
options.AddPolicy("Expire5", builder =>
builder.Expire(TimeSpan.FromSeconds(5))
);
options.AddPolicy("Expire60", builder =>
builder.Expire(TimeSpan.FromSeconds(60))
);
});
builder.Services.AddControllers();
var app = builder.Build();
// Configure the HTTP request pipeline.
app.UseHttpsRedirection();
app.UseOutputCache();
app.UseAuthorization();
// MVC STYLE
app.MapControllers();
// MINIMAL API STYLE
app
.MapGet(
"/minimal/now",
() => DateTimeOffset.UtcNow
);
app
.MapGet(
"/minimal/now-cached-2",
() => DateTimeOffset.UtcNow
)
.CacheOutput("Expire2");
app
.MapGet(
"/minimal/now-cached-60",
() => DateTimeOffset.UtcNow
)
.CacheOutput("Expire60");
app
.MapGet(
"/minimal/now-cached-5",
[OutputCache(PolicyName = "Expire5")] async (context) =>
{
await context.Response.WriteAsJsonAsync(DateTimeOffset.UtcNow);
}
);
app
.MapGet(
"/clear/l1",
() => memoryCache.Clear()
);
app.Run();
}
}
}