-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathProgram.cs
54 lines (47 loc) · 1.99 KB
/
Program.cs
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
using System;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.AzureHealth.DataServices.Caching;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace MemoryCacheAndRedisProvider
{
public static class Program
{
private static MyServiceConfig config;
internal static async Task Main()
{
IConfigurationBuilder cbuilder = new ConfigurationBuilder()
.AddUserSecrets(Assembly.GetExecutingAssembly(), true)
.AddEnvironmentVariables("AZURE_");
IConfigurationRoot root = cbuilder.Build();
config = new();
root.Bind(config);
IHostBuilder builder = Host.CreateDefaultBuilder()
.ConfigureServices(services =>
{
services.AddMemoryCache();
services.AddRedisCacheBackingStore(options =>
{
options.ConnectionString = config.ConnectionString;
});
services.AddJsonObjectMemoryCache(options =>
{
options.CacheItemExpiry = TimeSpan.FromSeconds(5.0);
});
services.AddScoped<IMyService, MyService>();
});
IHost app = builder.Build();
app.RunAsync().GetAwaiter();
string cacheKey = "key1";
TestCacheItem item = new() { Name = "testitem", Value = "testvalue" };
IMyService myservice = app.Services.GetRequiredService<IMyService>();
await myservice.SetAsync<TestCacheItem>(cacheKey, item);
TestCacheItem cachedItem = await myservice.GetAsync<TestCacheItem>(cacheKey);
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"Cached item....\r\nName {cachedItem.Name}\r\nValue {cachedItem.Value}");
Console.ResetColor();
}
}
}