This commit is contained in:
ai.astrakhantsev 2022-11-03 13:58:40 +05:00
parent 36b07eb640
commit 4a2a94ddfb
2 changed files with 71 additions and 4 deletions

View File

@ -158,8 +158,11 @@ namespace AsbCloudInfrastructure.EfCache
{
try
{
cache = new CacheItem();
caches.Add(tag, cache);
if (!caches.ContainsKey(tag))
{
cache = new CacheItem();
caches.Add(tag, cache);
}
}
finally
{
@ -231,8 +234,11 @@ namespace AsbCloudInfrastructure.EfCache
{
try
{
cache = new CacheItem();
caches.Add(tag, cache);
if (!caches.ContainsKey(tag))
{
cache = new CacheItem();
caches.Add(tag, cache);
}
}
finally
{

View File

@ -0,0 +1,61 @@
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.EfCache;
using DocumentFormat.OpenXml.Spreadsheet;
using Mapster;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xunit;
namespace AsbCloudWebApi.Tests.CacheTests
{
public class CacheTest
{
private readonly string userCacheTag = "Users";
private static readonly TimeSpan cacheObsolence = TimeSpan.FromMinutes(15);
public IEnumerable<User> UsersCache = new List<User> {
new User { Id = 1, Name = "test1", Email = "test1@mail.com" },
new User { Id = 2, Name = "test2", Email = "test2@mail.com" },
new User { Id = 3, Name = "test3", Email = "test3@mail.com" },
new User { Id = 4, Name = "test4", Email = "test4@mail.com" },
new User { Id = 5, Name = "test5", Email = "test5@mail.com" },
new User { Id = 6, Name = "test6", Email = "test6@mail.com" },
new User { Id = 7, Name = "test7", Email = "test7@mail.com" }
};
[Fact]
public void Send_n_requests_cache_user()
{
const int iterations = 1230;
var cache = UsersCache.AsQueryable();
var cacheCount = UsersCache.Count();
var fail = false;
var tasks = new int[iterations].Select(_=> new Task(() =>
{
var data = cache.FromCache(userCacheTag, cacheObsolence, Convert).ToArray();
if (data.Count() != cacheCount)
fail = true;
}))
.ToArray();
foreach(var task in tasks)
task.Start();
Task.WaitAll(tasks);
Assert.False(fail);
}
private UserExtendedDto Convert(User entity)
{
var dto = entity.Adapt<UserExtendedDto>();
return dto;
}
}
}