Skip to content

Commit

Permalink
feat: DistributeMemoryCacheTest
Browse files Browse the repository at this point in the history
  • Loading branch information
yaochangyu committed Nov 3, 2024
1 parent 786516d commit c97e6d6
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions Cache/Lab.Cache/Lab.Cache.Test/DistributeMemoryCacheTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using System.Text.Json;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Lab.Cache.Test;

public class DistributeMemoryCacheTest
{
private static IServiceProvider CreateServiceProvider()
{
Environment.SetEnvironmentVariable(nameof(Config.DEFAULT_CACHE_EXPIRATION), "00:00:05");

var configurationBuilder = new ConfigurationBuilder();
configurationBuilder.AddEnvironmentVariables() ;
var services = new ServiceCollection();
var configuration = configurationBuilder.Build();
services.AddSingleton<IConfiguration>(configuration);
services.AddSingleton(p =>
{
var expiration = configuration.GetValue<TimeSpan>(nameof(Config.DEFAULT_CACHE_EXPIRATION));

var options = new DistributedCacheEntryOptions()
{
AbsoluteExpirationRelativeToNow = expiration
};
return options;
});
services.AddDistributedMemoryCache();
var serviceProvider = services.BuildServiceProvider();
return serviceProvider;
}

[Fact]
public async Task 寫讀快取資料_Json()
{
var serviceProvider = CreateServiceProvider();
var cache = serviceProvider.GetService<IDistributedCache>();
var options = serviceProvider.GetService<DistributedCacheEntryOptions>();

var key = $"{nameof(DistributeMemoryCacheTest)}:Member:1";
var expected = JsonSerializer.Serialize(new { Name = "小心肝" });
await cache.SetStringAsync(key, expected, options);

var result = await cache.GetStringAsync(key);
Assert.Equal(expected, result);
}

[Fact]
public async Task 寫讀快取資料_String()
{
var serviceProvider = CreateServiceProvider();
var cache = serviceProvider.GetService<IDistributedCache>();
var options = serviceProvider.GetService<DistributedCacheEntryOptions>();

var key = $"{nameof(DistributeMemoryCacheTest)}:Member:2";
var expected = "小心肝";
await cache.SetStringAsync(key, expected, options);

var result = await cache.GetStringAsync(key);
Assert.Equal(expected, result);
}
}

0 comments on commit c97e6d6

Please sign in to comment.