Skip to content

Commit 36a978a

Browse files
committed
Add FusionCacheExercise
1 parent 52f5e98 commit 36a978a

12 files changed

+240
-3
lines changed

AutoMapperExercise/AutoMapperExercise.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,8 @@
77
<Nullable>enable</Nullable>
88
</PropertyGroup>
99

10+
<ItemGroup>
11+
<PackageReference Include="AutoMapper" Version="13.0.1" />
12+
</ItemGroup>
13+
1014
</Project>

AutoMapperExercise/MappingProfile.cs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using AutoMapper;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace AutoMapperExercise
9+
{
10+
public class MappingProfile : Profile
11+
{
12+
public MappingProfile()
13+
{
14+
CreateMap<PersonInfo, PersonInfoDto>();
15+
}
16+
}
17+
}

AutoMapperExercise/PersonInfo.cs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace AutoMapperExercise
8+
{
9+
public class PersonInfo
10+
{
11+
public string FirstName { get; set; }
12+
public string LastName { get; set; }
13+
public int Age { get; set; }
14+
public string Nationality { get; set; }
15+
}
16+
}

AutoMapperExercise/PersonInfoDto.cs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace AutoMapperExercise
8+
{
9+
public class PersonInfoDto
10+
{
11+
public string FirstName { get; set; }
12+
public string LastName { get; set; }
13+
public int Age { get; set; }
14+
public string Nationality { get; set; }
15+
}
16+
}

AutoMapperExercise/Program.cs

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
1-
namespace AutoMapperExercise
1+
using AutoMapper;
2+
3+
namespace AutoMapperExercise
24
{
35
internal class Program
46
{
57
static void Main(string[] args)
68
{
7-
Console.WriteLine("Hello, World!");
9+
var configuration = new MapperConfiguration(cfg => {
10+
cfg.AddProfile<MappingProfile>();
11+
//或者下面这种方式
12+
//cfg.CreateMap<PersonInfo, PersonInfoDto>();
13+
});
14+
var mapper = configuration.CreateMapper();
15+
16+
var personInfo = new PersonInfo
17+
{
18+
FirstName = "大东",
19+
LastName = "陈",
20+
Age = 18,
21+
Nationality = "中国"
22+
};
23+
var personInfoDto = mapper.Map<PersonInfoDto>(personInfo);
824
}
925
}
1026
}

DotNetExercises.sln

+13-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BouncyCastleExercise", "Bou
1515
EndProject
1616
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChartjsExercise", "ChartjsExercise\ChartjsExercise.csproj", "{B7653CF0-57B2-4F2A-A993-8E68945728E2}"
1717
EndProject
18-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GenericRepositoryExercise", "GenericRepositoryExercise\GenericRepositoryExercise.csproj", "{23932422-6C88-4E87-A41E-28263CA5F74E}"
18+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GenericRepositoryExercise", "GenericRepositoryExercise\GenericRepositoryExercise.csproj", "{23932422-6C88-4E87-A41E-28263CA5F74E}"
19+
EndProject
20+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "IdGeneratorExercise", "IdGeneratorExercise\IdGeneratorExercise.csproj", "{9F7B777C-4942-4079-97B3-026E28B44C9F}"
21+
EndProject
22+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FusionCacheExercise", "FusionCacheExercise\FusionCacheExercise.csproj", "{1B5936BA-F3EA-4E6E-B86C-9ED0CC5EB9C2}"
1923
EndProject
2024
Global
2125
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -51,6 +55,14 @@ Global
5155
{23932422-6C88-4E87-A41E-28263CA5F74E}.Debug|Any CPU.Build.0 = Debug|Any CPU
5256
{23932422-6C88-4E87-A41E-28263CA5F74E}.Release|Any CPU.ActiveCfg = Release|Any CPU
5357
{23932422-6C88-4E87-A41E-28263CA5F74E}.Release|Any CPU.Build.0 = Release|Any CPU
58+
{9F7B777C-4942-4079-97B3-026E28B44C9F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
59+
{9F7B777C-4942-4079-97B3-026E28B44C9F}.Debug|Any CPU.Build.0 = Debug|Any CPU
60+
{9F7B777C-4942-4079-97B3-026E28B44C9F}.Release|Any CPU.ActiveCfg = Release|Any CPU
61+
{9F7B777C-4942-4079-97B3-026E28B44C9F}.Release|Any CPU.Build.0 = Release|Any CPU
62+
{1B5936BA-F3EA-4E6E-B86C-9ED0CC5EB9C2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
63+
{1B5936BA-F3EA-4E6E-B86C-9ED0CC5EB9C2}.Debug|Any CPU.Build.0 = Debug|Any CPU
64+
{1B5936BA-F3EA-4E6E-B86C-9ED0CC5EB9C2}.Release|Any CPU.ActiveCfg = Release|Any CPU
65+
{1B5936BA-F3EA-4E6E-B86C-9ED0CC5EB9C2}.Release|Any CPU.Build.0 = Release|Any CPU
5466
EndGlobalSection
5567
GlobalSection(SolutionProperties) = preSolution
5668
HideSolutionNode = FALSE
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
12+
<PackageReference Include="ZiggyCreatures.FusionCache" Version="1.0.0" />
13+
</ItemGroup>
14+
15+
</Project>
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using ZiggyCreatures.Caching.Fusion;
2+
3+
namespace FusionCacheExercise
4+
{
5+
public class FusionCacheService
6+
{
7+
private readonly IFusionCache _cache;
8+
9+
public FusionCacheService(IFusionCache cache)
10+
{
11+
_cache = cache;
12+
}
13+
14+
public async Task<PersonInfo> GetValueAsync(string key)
15+
{
16+
var cachedValue = await _cache.GetOrDefaultAsync<PersonInfo>(key).ConfigureAwait(false);
17+
if (cachedValue != null)
18+
{
19+
cachedValue.CacheMsg = "缓存中的值";
20+
return cachedValue;
21+
}
22+
else
23+
{
24+
//从数据库或其他数据源获取值
25+
var value = GetValueFromDataSource(key);
26+
//将值存入缓存,设置过期时间等
27+
await _cache.SetAsync(key, value, TimeSpan.FromMinutes(10)).ConfigureAwait(false);
28+
return value;
29+
}
30+
}
31+
32+
private PersonInfo GetValueFromDataSource(string key)
33+
{
34+
var personInfo = new PersonInfo
35+
{
36+
UserName = "追逐时光者",
37+
Age = 18,
38+
Nationality = "中国",
39+
CacheMsg = "默认值"
40+
};
41+
return personInfo;
42+
}
43+
}
44+
}

FusionCacheExercise/PersonInfo.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace FusionCacheExercise
2+
{
3+
public class PersonInfo
4+
{
5+
public string UserName { get; set; }
6+
7+
public int Age { get; set; }
8+
9+
public string Nationality { get; set; }
10+
11+
public string CacheMsg { get; set; }
12+
}
13+
}

FusionCacheExercise/Program.cs

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using ZiggyCreatures.Caching.Fusion;
3+
4+
namespace FusionCacheExercise
5+
{
6+
internal class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
//创建服务集合
11+
var services = new ServiceCollection();
12+
13+
//服务注册
14+
services.AddScoped<FusionCacheService>();
15+
var entryOptions = new FusionCacheEntryOptions().SetDuration(TimeSpan.FromMinutes(10));
16+
services.AddFusionCache()
17+
.WithDefaultEntryOptions(entryOptions)
18+
.WithPostSetup((sp, c) =>
19+
{
20+
c.DefaultEntryOptions.Duration = TimeSpan.FromMinutes(5);
21+
});
22+
23+
using var serviceProvider = services.BuildServiceProvider();
24+
25+
var myService = serviceProvider.GetRequiredService<FusionCacheService>();
26+
27+
for (int i = 0; i < 2; i++)
28+
{
29+
var value = myService.GetValueAsync("FusionCacheExerciseKey").Result;
30+
Console.WriteLine($"{value.CacheMsg} {value.UserName}{value.Age}{value.Nationality}");
31+
}
32+
}
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Yitter.IdGenerator" Version="1.0.14" />
12+
</ItemGroup>
13+
14+
</Project>

IdGeneratorExercise/Program.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using Yitter.IdGenerator;
2+
3+
namespace IdGeneratorExercise
4+
{
5+
internal class Program
6+
{
7+
static void Main(string[] args)
8+
{
9+
#region 第一步:全局初始化(应用程序启动时执行一次)
10+
11+
// 创建 IdGeneratorOptions 对象,可在构造函数中输入 WorkerId:
12+
// options.WorkerIdBitLength = 10; // 默认值6,限定 WorkerId 最大值为2^6-1,即默认最多支持64个节点。
13+
// options.SeqBitLength = 6; // 默认值6,限制每毫秒生成的ID个数。若生成速度超过5万个/秒,建议加大 SeqBitLength 到 10。
14+
// options.BaseTime = Your_Base_Time; // 如果要兼容老系统的雪花算法,此处应设置为老系统的BaseTime。
15+
// WorkerId:WorkerId,机器码,最重要参数,无默认值,必须 全局唯一(或相同 DataCenterId 内唯一),必须 程序设定,缺省条件(WorkerIdBitLength取默认值)时最大值63,理论最大值 2^WorkerIdBitLength-1(不同实现语言可能会限定在 65535 或 32767,原理同 WorkerIdBitLength 规则)。不同机器或不同应用实例 不能相同,你可通过应用程序配置该值,也可通过调用外部服务获取值。
16+
// ...... 其它参数参考 IdGeneratorOptions 定义。
17+
var idGeneratorOptions = new IdGeneratorOptions(1) { WorkerIdBitLength = 6 };
18+
// 保存参数(务必调用,否则参数设置不生效):
19+
YitIdHelper.SetIdGenerator(idGeneratorOptions);
20+
// 以上过程只需全局一次,且应在生成ID之前完成。
21+
22+
#endregion
23+
24+
#region 第二步:生成分布式ID
25+
26+
for (int i = 0; i < 1000; i++)
27+
{
28+
// 初始化后,在任何需要生成ID的地方,调用以下方法:
29+
var newId = YitIdHelper.NextId();
30+
Console.WriteLine($"Number{i}{newId}");
31+
}
32+
33+
#endregion
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)