Skip to content

Commit 8c9a768

Browse files
committed
add SyncCacheManager to sample
1 parent 73d9065 commit 8c9a768

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

sample/SmartSql.Sample.AspNetCore/Controllers/UserController.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,22 @@ public class UserController : ControllerBase
1818
private readonly UserService _userService;
1919

2020
public UserController(IUserRepository userRepository
21-
,UserService userService)
21+
, UserService userService)
2222
{
2323
_userRepository = userRepository;
2424
_userService = userService;
2525
}
26+
2627
// GET api/values
2728
[HttpPost]
28-
public long AddWithTranWrap([FromBody]User user)
29+
public long AddWithTranWrap([FromBody] User user)
2930
{
3031
var id = _userService.AddWithTranWrap(user);
3132
return id;
3233
}
34+
3335
[HttpPost]
34-
public long AddWithTran([FromBody]User user)
36+
public long AddWithTran([FromBody] User user)
3537
{
3638
var id = _userService.AddWithTran(user);
3739
return id;
@@ -46,6 +48,12 @@ public User GetById(long id)
4648
return user;
4749
}
4850

51+
[HttpPost]
52+
public int Update(User user)
53+
{
54+
return _userRepository.Update(user);
55+
}
56+
4957
[HttpGet]
5058
public GetByPageResponse<User> GetByPage(int pageIndex = 1)
5159
{
@@ -55,16 +63,19 @@ public GetByPageResponse<User> GetByPage(int pageIndex = 1)
5563
PageIndex = pageIndex
5664
});
5765
}
66+
5867
[HttpGet]
5968
public IEnumerable<User> Query(int taken = 10)
6069
{
6170
return _userRepository.Query(taken);
6271
}
72+
6373
[HttpGet]
6474
public async Task<IEnumerable<User>> QueryAsync(int taken = 10)
6575
{
6676
return await _userRepository.QueryAsync(taken);
6777
}
78+
6879
[HttpGet]
6980
public async Task Mt(int id)
7081
{
@@ -86,7 +97,6 @@ await _userRepository.InsertAsync(new User
8697
_userRepository.SqlMapper.RollbackTransaction();
8798
throw;
8899
}
89-
90100
}
91101
}
92-
}
102+
}

sample/SmartSql.Sample.AspNetCore/DyRepositories/IUserRepository.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ public interface IUserRepository
1919
[Statement(Id = "QueryByPage")]
2020
TPageResult GetByPage<TPageResult>(object request);
2121
Task<IEnumerable<User>> QueryAsync([Param("Taken")]int taken);
22+
int Update(User entity);
2223
}
2324
}

sample/SmartSql.Sample.AspNetCore/SmartSql.Sample.AspNetCore.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<ProjectReference Include="..\..\src\SmartSql.Bulk.SqlServer\SmartSql.Bulk.SqlServer.csproj" />
2929
<ProjectReference Include="..\..\src\SmartSql.Bulk\SmartSql.Bulk.csproj" />
3030
<ProjectReference Include="..\..\src\SmartSql.Cache.Redis\SmartSql.Cache.Redis.csproj" />
31+
<ProjectReference Include="..\..\src\SmartSql.Cache.Sync\SmartSql.Cache.Sync.csproj" />
3132
<ProjectReference Include="..\..\src\SmartSql.DIExtension\SmartSql.DIExtension.csproj" />
3233
<ProjectReference Include="..\..\src\SmartSql.DyRepository\SmartSql.DyRepository.csproj" />
3334
<ProjectReference Include="..\..\src\SmartSql.InvokeSync.Kafka\SmartSql.InvokeSync.Kafka.csproj" />

sample/SmartSql.Sample.AspNetCore/Startup.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
using Microsoft.EntityFrameworkCore.Metadata;
77
using Microsoft.Extensions.Configuration;
88
using Microsoft.Extensions.DependencyInjection;
9+
using SmartSql.Cache.Sync;
910
using SmartSql.ConfigBuilder;
1011
using SmartSql.DIExtension;
12+
using SmartSql.InvokeSync;
1113
using SmartSql.InvokeSync.Kafka;
1214
using SmartSql.Sample.AspNetCore.Service;
1315
using Swashbuckle.AspNetCore.Swagger;
@@ -28,18 +30,23 @@ public IServiceProvider ConfigureServices(IServiceCollection services)
2830
{
2931
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
3032
services
31-
.AddSmartSql((builder) => { builder.UseProperties(Configuration); })
33+
.AddSmartSql((sp, builder) =>
34+
{
35+
builder.UseProperties(Configuration);
36+
var subscriber = sp.GetRequiredService<ISubscriber>();
37+
builder.UseCacheManager(new SyncCacheManager(subscriber));
38+
})
3239
.AddRepositoryFromAssembly(o =>
3340
{
3441
o.AssemblyString = "SmartSql.Sample.AspNetCore";
3542
o.Filter = (type) => type.Namespace == "SmartSql.Sample.AspNetCore.DyRepositories";
3643
})
37-
.AddInvokeSync(options => { })
44+
.AddInvokeSync(options => { })
3845
.AddKafkaPublisher(options =>
3946
{
4047
options.Servers = "localhost:9092";
4148
options.Topic = "smartsql.sync";
42-
options.Config.Add("group.id","SmartSql");
49+
options.Config.Add("group.id", "SmartSql");
4350
});
4451
services.AddSingleton<UserService>();
4552
RegisterConfigureSwagger(services);

src/SmartSql.Test/Maps/User.xml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@
99

1010
<SmartSqlMap Scope="User" xmlns="http://SmartSql.net/schemas/SmartSqlMap.xsd">
1111
<Caches >
12-
<Cache Id="RedisCache" Type="${RedisCacheProvider}">
13-
<Property Name="DatabaseId" Value="3"/>
14-
<Property Name="ConnectionString" Value="${Redis}"/>
15-
<FlushOnExecute Statement="Insert"/>
12+
<Cache Id="UserCache" Type="Lru">
1613
<FlushOnExecute Statement="Update"/>
1714
<FlushOnExecute Statement="Delete"/>
1815
</Cache>
@@ -106,7 +103,7 @@
106103
</Statement>
107104

108105
<!--获取表映射实体-->
109-
<Statement Id="GetEntity" >
106+
<Statement Id="GetEntity" Cache="UserCache" >
110107
Select Top 1 T.* From T_User T With(NoLock)
111108
<Where Min="1">
112109
<IsNotEmpty Prepend="And" Property="Id">

0 commit comments

Comments
 (0)