Skip to content

Commit 6666457

Browse files
committed
将方法入参统一改为接口
1 parent 6a2c5a4 commit 6666457

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+495
-397
lines changed

Benchmark/Farseer.Net.Benchmark/Other/TestUsingPooled.cs

+27-57
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ public TestUsingPooled()
1919
public void Test()
2020
{
2121
TestClassConvert();
22-
// TestToList();
23-
// TestToPooledList();
24-
// TestToList_ToPooledList();
2522
}
2623

2724
public void TestClassConvert()
@@ -52,69 +49,42 @@ public void TestClassConvert()
5249
lst.AsEnumerable().ToList();
5350
}, 3);
5451

55-
TestAllocatedBytes("PooledList.Add()", () =>
52+
TestAllocatedBytes("lst.Select(o => o.Name)", () =>
5653
{
57-
using PooledList<UserPO> lst = new(100000);
58-
for (int i = 0; i < 100000; i++)
59-
{
60-
lst.Add(new UserPO() { Name = "123123" });
61-
}
62-
}, 10);
54+
var lst2 = lst.Select(o => o.Name);
55+
}, 3);
6356

64-
TestAllocatedBytes("List.Add()", () =>
57+
TestAllocatedBytes("foreach (var s in lst.Select(o => o.Name))", () =>
6558
{
66-
List<UserPO> lst = new(100000);
67-
for (int i = 0; i < 100000; i++)
68-
{
69-
lst.Add(new UserPO() { Name = "123123" });
70-
}
71-
}, 10);
72-
}
59+
foreach (var s in lst.Select(o => o.Name)) { }
60+
}, 3);
7361

74-
public void TestToList()
75-
{
76-
GC.Collect();
77-
var baseAllocated = GC.GetTotalAllocatedBytes();
78-
Console.WriteLine($"准备测试ToList的堆分配大小");
79-
for (int j = 1; j <= 5; j++)
62+
TestAllocatedBytes("lst.Select(o => o.Name).Count()", () =>
8063
{
81-
for (int i = 0; i < 1000; i++)
82-
{
83-
var lst2 = lst.Select(o => o.Name).ToList();
84-
}
85-
Console.WriteLine($"{j}/5:ToList:当前已分配堆大小:{(GC.GetTotalAllocatedBytes() - baseAllocated).ToString("N0")} byte");
86-
}
87-
Console.WriteLine("---------------------------");
88-
}
64+
lst.Select(o => o.Name).Count();
65+
}, 3);
8966

90-
public void TestToPooledList()
91-
{
92-
GC.Collect();
93-
var baseAllocated = GC.GetTotalAllocatedBytes();
94-
Console.WriteLine($"准备测试ToPooledList的堆分配大小");
95-
for (int j = 1; j <= 5; j++)
67+
TestAllocatedBytes("lst.Select(o => o.Name).ToList()", () =>
9668
{
97-
for (int i = 0; i < 1000; i++)
98-
{
99-
using var lst2 = lst.Select(o => o.Name).ToPooledList();
100-
}
101-
Console.WriteLine($"{j}/5:ToPooledList:当前已分配堆大小:{(GC.GetTotalAllocatedBytes() - baseAllocated).ToString("N0")} byte");
102-
}
103-
}
69+
var lst2 = lst.Select(o => o.Name).ToList();
70+
}, 3);
10471

105-
public void TestToList_ToPooledList()
106-
{
107-
GC.Collect();
108-
var baseAllocated = GC.GetTotalAllocatedBytes();
109-
Console.WriteLine($"准备测试ToList().ToPooledList的堆分配大小");
110-
for (int j = 1; j <= 5; j++)
72+
TestAllocatedBytes("lst.Select(o => o.Name).ToPooledList()", () =>
11173
{
112-
for (int i = 0; i < 1000; i++)
113-
{
114-
using var lst2 = lst.Select(o => o.Name).ToList().ToPooledList();
115-
}
116-
Console.WriteLine($"{j}/5:ToList().ToPooledList:当前已分配堆大小:{(GC.GetTotalAllocatedBytes() - baseAllocated).ToString("N0")} byte");
117-
}
74+
using var lst2 = lst.Select(o => o.Name).ToPooledList();
75+
}, 3);
76+
77+
TestAllocatedBytes("PooledList.Add()", () =>
78+
{
79+
using PooledList<UserPO> lst = new();
80+
lst.Add(new UserPO() { Name = "123123" });
81+
}, 3);
82+
83+
TestAllocatedBytes("List.Add()", () =>
84+
{
85+
List<UserPO> lst = new();
86+
lst.Add(new UserPO() { Name = "123123" });
87+
}, 3);
11888
}
11989

12090
public void TestAllocatedBytes(string testName, Action act, int count)

Farseer.Net.sln

+13
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Benchmark", "Benchmark", "{
8686
EndProject
8787
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Farseer.Net.Benchmark", "Benchmark\Farseer.Net.Benchmark\Farseer.Net.Benchmark.csproj", "{C28FC186-0FC0-4DBD-A4D8-5CBBE8360333}"
8888
EndProject
89+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Study", "Study", "{0B43AD20-6A65-4C69-991A-BDB3821F2D51}"
90+
EndProject
91+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Study", "Study\Study\Study.csproj", "{BEC94E2E-D307-499A-8583-304C673E90D1}"
92+
EndProject
8993
Global
9094
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9195
Debug|Any CPU = Debug|Any CPU
@@ -340,6 +344,14 @@ Global
340344
{C28FC186-0FC0-4DBD-A4D8-5CBBE8360333}.Release|Any CPU.Build.0 = Release|Any CPU
341345
{C28FC186-0FC0-4DBD-A4D8-5CBBE8360333}.Release|x64.ActiveCfg = Release|Any CPU
342346
{C28FC186-0FC0-4DBD-A4D8-5CBBE8360333}.Release|x64.Build.0 = Release|Any CPU
347+
{BEC94E2E-D307-499A-8583-304C673E90D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
348+
{BEC94E2E-D307-499A-8583-304C673E90D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
349+
{BEC94E2E-D307-499A-8583-304C673E90D1}.Debug|x64.ActiveCfg = Debug|Any CPU
350+
{BEC94E2E-D307-499A-8583-304C673E90D1}.Debug|x64.Build.0 = Debug|Any CPU
351+
{BEC94E2E-D307-499A-8583-304C673E90D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
352+
{BEC94E2E-D307-499A-8583-304C673E90D1}.Release|Any CPU.Build.0 = Release|Any CPU
353+
{BEC94E2E-D307-499A-8583-304C673E90D1}.Release|x64.ActiveCfg = Release|Any CPU
354+
{BEC94E2E-D307-499A-8583-304C673E90D1}.Release|x64.Build.0 = Release|Any CPU
343355
EndGlobalSection
344356
GlobalSection(SolutionProperties) = preSolution
345357
HideSolutionNode = FALSE
@@ -376,6 +388,7 @@ Global
376388
{EC26E9A7-5030-449E-8D3F-E37B04030894} = {33414C74-7920-4F78-8480-44BE2184D5E1}
377389
{0E9888FC-D4EA-413A-89F7-0016244A5016} = {BA9C8E90-5B9E-464C-B0CA-7988E2CA850B}
378390
{C28FC186-0FC0-4DBD-A4D8-5CBBE8360333} = {6E0FEE87-1277-4E10-9BE3-ED5F9DC0A398}
391+
{BEC94E2E-D307-499A-8583-304C673E90D1} = {0B43AD20-6A65-4C69-991A-BDB3821F2D51}
379392
EndGlobalSection
380393
GlobalSection(ExtensibilityGlobals) = postSolution
381394
SolutionGuid = {22E81E03-33DB-418D-90D2-FD1B560FEF14}

Framework/Farseer.Net.AspNetCore/BadRequestException.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Linq;
2+
using Collections.Pooled;
23
using FS.Core.Net;
34
using FS.Extends;
45
using Microsoft.AspNetCore.Mvc;
@@ -22,7 +23,7 @@ public void OnResultExecuting(ResultExecutingContext context)
2223
{
2324
if (badResult.Value is ValidationProblemDetails val)
2425
{
25-
var lstError = val.Errors.SelectMany(selector: o => o.Value).ToList();
26+
var lstError = val.Errors.SelectMany(selector: o => o.Value);
2627
var error = lstError.ToString(sign: ",");
2728
badResult.Value = ApiResponseJson.Error(statusMessage: error, statusCode: badResult.StatusCode.GetValueOrDefault());
2829
return;

Framework/Farseer.Net.Cache.Redis/GetCacheInMemoryAndRedis.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public async Task SaveItemAsync<TEntity, TEntityId>(CacheKey<TEntity, TEntityId>
201201
/// <summary>
202202
/// 将LIST保存到缓存中
203203
/// </summary>
204-
public void SaveList<TEntity, TEntityId>(CacheKey<TEntity, TEntityId> cacheKey, PooledList<TEntity> lst)
204+
public void SaveList<TEntity, TEntityId>(CacheKey<TEntity, TEntityId> cacheKey, IEnumerable<TEntity> lst)
205205
{
206206
_redisCache.SaveList(cacheKey: cacheKey, lst: lst);
207207
// ReSharper disable once MethodHasAsyncOverload
@@ -211,7 +211,7 @@ public void SaveList<TEntity, TEntityId>(CacheKey<TEntity, TEntityId> cacheKey,
211211
/// <summary>
212212
/// 将LIST保存到缓存中
213213
/// </summary>
214-
public async Task SaveListAsync<TEntity, TEntityId>(CacheKey<TEntity, TEntityId> cacheKey, PooledList<TEntity> lst)
214+
public async Task SaveListAsync<TEntity, TEntityId>(CacheKey<TEntity, TEntityId> cacheKey, IEnumerable<TEntity> lst)
215215
{
216216
await _redisCache.SaveListAsync(cacheKey: cacheKey, lst: lst);
217217
// ReSharper disable once MethodHasAsyncOverload

Framework/Farseer.Net.Cache.Redis/GetCacheInRedis.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,15 @@ public Task SaveItemAsync<TEntity, TEntityId>(CacheKey<TEntity, TEntityId> cache
122122
/// <summary>
123123
/// 将LIST保存到缓存中
124124
/// </summary>
125-
public void SaveList<TEntity, TEntityId>(CacheKey<TEntity, TEntityId> cacheKey, PooledList<TEntity> lst)
125+
public void SaveList<TEntity, TEntityId>(CacheKey<TEntity, TEntityId> cacheKey, IEnumerable<TEntity> lst)
126126
{
127127
_redisCacheManager.HashSetTransaction(key: cacheKey.Key, lst: lst, funcDataKey: cacheKey.GetField, funcData: null, expiry: cacheKey.RedisExpiry);
128128
}
129129

130130
/// <summary>
131131
/// 将LIST保存到缓存中
132132
/// </summary>
133-
public Task SaveListAsync<TEntity, TEntityId>(CacheKey<TEntity, TEntityId> cacheKey, PooledList<TEntity> lst) => _redisCacheManager.HashSetTransactionAsync(key: cacheKey.Key, lst: lst, funcDataKey: cacheKey.GetField, funcData: null, expiry: cacheKey.RedisExpiry);
133+
public Task SaveListAsync<TEntity, TEntityId>(CacheKey<TEntity, TEntityId> cacheKey, IEnumerable<TEntity> lst) => _redisCacheManager.HashSetTransactionAsync(key: cacheKey.Key, lst: lst, funcDataKey: cacheKey.GetField, funcData: null, expiry: cacheKey.RedisExpiry);
134134

135135
/// <summary>
136136
/// 删除整个缓存

Framework/Farseer.Net.Cache.Redis/IRedisCacheManager.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ public interface IRedisCacheManager : IDisposable
3434
/// <summary>
3535
/// 事务,批量写入HASH
3636
/// </summary>
37-
void HashSetTransaction<TEntity, TEntityId>(string key, PooledList<TEntity> lst, Func<TEntity, TEntityId> funcDataKey, Func<TEntity, string> funcData = null, TimeSpan? expiry = null);
37+
void HashSetTransaction<TEntity, TEntityId>(string key, IEnumerable<TEntity> lst, Func<TEntity, TEntityId> funcDataKey, Func<TEntity, string> funcData = null, TimeSpan? expiry = null);
3838

3939
/// <summary>
4040
/// 事务,批量写入HASH
4141
/// </summary>
42-
Task HashSetTransactionAsync<TEntity, TEntityId>(string key, PooledList<TEntity> lst, Func<TEntity, TEntityId> funcDataKey, Func<TEntity, string> funcData = null, TimeSpan? expiry = null);
42+
Task HashSetTransactionAsync<TEntity, TEntityId>(string key, IEnumerable<TEntity> lst, Func<TEntity, TEntityId> funcDataKey, Func<TEntity, string> funcData = null, TimeSpan? expiry = null);
4343

4444
/// <summary>
4545
/// 事务锁

Framework/Farseer.Net.Cache.Redis/RedisCacheManager.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Threading.Tasks;
45
using Collections.Pooled;
56
using FS.Cache.Redis.Configuration;
@@ -87,9 +88,9 @@ public virtual void Dispose()
8788
/// <summary>
8889
/// 事务,批量写入HASH
8990
/// </summary>
90-
public void HashSetTransaction<TEntity, TEntityId>(string key, PooledList<TEntity> lst, Func<TEntity, TEntityId> funcDataKey, Func<TEntity, string> funcData = null, TimeSpan? expiry = null)
91+
public void HashSetTransaction<TEntity, TEntityId>(string key, IEnumerable<TEntity> lst, Func<TEntity, TEntityId> funcDataKey, Func<TEntity, string> funcData = null, TimeSpan? expiry = null)
9192
{
92-
if (lst == null || lst.Count == 0) return;
93+
if (lst == null || !lst.Any()) return;
9394
if (funcData == null) funcData = po => JsonConvert.SerializeObject(value: po);
9495

9596
var transaction = Db.CreateTransaction();
@@ -110,9 +111,9 @@ public void HashSetTransaction<TEntity, TEntityId>(string key, PooledList<TEntit
110111
/// <summary>
111112
/// 事务,批量写入HASH
112113
/// </summary>
113-
public Task HashSetTransactionAsync<TEntity, TEntityId>(string key, PooledList<TEntity> lst, Func<TEntity, TEntityId> funcDataKey, Func<TEntity, string> funcData = null, TimeSpan? expiry = null)
114+
public Task HashSetTransactionAsync<TEntity, TEntityId>(string key, IEnumerable<TEntity> lst, Func<TEntity, TEntityId> funcDataKey, Func<TEntity, string> funcData = null, TimeSpan? expiry = null)
114115
{
115-
if (lst == null || lst.Count == 0) return Task.FromResult(result: 0);
116+
if (lst == null || !lst.Any()) return Task.FromResult(result: 0);
116117
funcData ??= po => JsonConvert.SerializeObject(value: po);
117118

118119
var transaction = Db.CreateTransaction();

0 commit comments

Comments
 (0)