Browse Source

Refactor entity cache to use decimal for price and enhance FindManyAsync/GetManyAsync to handle duplicate IDs

pull/25088/head
maliming 5 months ago
parent
commit
81d34da46c
No known key found for this signature in database GPG Key ID: A646B9CB645ECEA4
  1. 6
      docs/en/framework/infrastructure/entity-cache.md
  2. 9
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheBase.cs
  3. 8
      framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs
  4. 24
      framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityCache_Tests.cs

6
docs/en/framework/infrastructure/entity-cache.md

@ -26,7 +26,7 @@ public class Product : AggregateRoot<Guid>
public string Name { get; set; }
public string Description { get; set; }
public float Price { get; set; }
public decimal Price { get; set; }
public int StockCount { get; set; }
}
```
@ -72,7 +72,7 @@ public class ProductDto : EntityDto<Guid>
{
public string Name { get; set; }
public string Description { get; set; }
public float Price { get; set; }
public decimal Price { get; set; }
public int StockCount { get; set; }
}
```
@ -189,7 +189,7 @@ public class ProductCacheDto
{
public Guid Id { get; set; }
public string Name { get; set; }
public float Price { get; set; }
public decimal Price { get; set; }
}
```

9
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheBase.cs

@ -49,7 +49,8 @@ public abstract class EntityCacheBase<TEntity, TEntityCacheItem, TKey> :
public virtual async Task<List<TEntityCacheItem?>> FindManyAsync(IEnumerable<TKey> ids)
{
var idArray = ids.ToArray();
var cacheItems = await GetOrAddManyCacheItemsAsync(idArray);
var distinctIds = idArray.Distinct().ToArray();
var cacheItems = await GetOrAddManyCacheItemsAsync(distinctIds);
#pragma warning disable CS8714
var cacheItemDict = cacheItems.ToDictionary(x => x.Key, x => x.Value);
#pragma warning restore CS8714
@ -77,7 +78,8 @@ public abstract class EntityCacheBase<TEntity, TEntityCacheItem, TKey> :
public virtual async Task<List<TEntityCacheItem>> GetManyAsync(IEnumerable<TKey> ids)
{
var idArray = ids.ToArray();
var cacheItems = await GetOrAddManyCacheItemsAsync(idArray);
var distinctIds = idArray.Distinct().ToArray();
var cacheItems = await GetOrAddManyCacheItemsAsync(distinctIds);
#pragma warning disable CS8714
var cacheItemDict = cacheItems.ToDictionary(x => x.Key, x => x.Value);
#pragma warning restore CS8714
@ -109,7 +111,8 @@ public abstract class EntityCacheBase<TEntity, TEntityCacheItem, TKey> :
var missingKeyArray = missingKeys.ToArray();
var entities = await Repository.GetListAsync(
x => missingKeyArray.Contains(x.Id)
x => missingKeyArray.Contains(x.Id),
includeDetails: true
);
#pragma warning disable CS8714
var entityDict = entities.ToDictionary(e => e.Id);

8
framework/src/Volo.Abp.Ddd.Domain/Volo/Abp/Domain/Entities/Caching/EntityCacheServiceCollectionExtensions.cs

@ -80,6 +80,14 @@ public static class EntityCacheServiceCollectionExtensions
options.ConfigureCache<EntityCacheItemWrapper<TEntityCacheItem>>(cacheOptions ?? GetDefaultCacheOptions());
});
if (typeof(TEntity) == typeof(TEntityCacheItem))
{
services.Configure<AbpSystemTextJsonSerializerModifiersOptions>(options =>
{
options.Modifiers.Add(new AbpIncludeNonPublicPropertiesModifiers<TEntity, TKey>().CreateModifyAction(x => x.Id));
});
}
return services;
}

24
framework/test/Volo.Abp.TestApp/Volo/Abp/TestApp/Testing/EntityCache_Tests.cs

@ -196,6 +196,30 @@ public abstract class EntityCache_Tests<TStartupModule> : TestAppTestBase<TStart
productCacheItem.Price.ShouldBe(decimal.Zero);
}
[Fact]
public async Task FindMany_Should_Handle_Duplicate_Ids()
{
var ids = new[] { TestDataBuilder.ProductId, TestDataBuilder.ProductId };
var products = await ProductEntityCache.FindManyAsync(ids);
products.Count.ShouldBe(2);
products[0].ShouldNotBeNull();
products[0]!.Id.ShouldBe(TestDataBuilder.ProductId);
products[1].ShouldNotBeNull();
products[1]!.Id.ShouldBe(TestDataBuilder.ProductId);
}
[Fact]
public async Task GetMany_Should_Handle_Duplicate_Ids()
{
var ids = new[] { TestDataBuilder.ProductId, TestDataBuilder.ProductId };
var products = await ProductEntityCache.GetManyAsync(ids);
products.Count.ShouldBe(2);
products[0].Id.ShouldBe(TestDataBuilder.ProductId);
products[1].Id.ShouldBe(TestDataBuilder.ProductId);
}
[Fact]
public async Task ReplaceEntityCache_Should_Use_Custom_Mapping()
{

Loading…
Cancel
Save