Skip to content

Commit

Permalink
优化Redis接入,全面使用ICacheProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Dec 12, 2023
1 parent 583026e commit 456e84d
Show file tree
Hide file tree
Showing 31 changed files with 148 additions and 67 deletions.
8 changes: 4 additions & 4 deletions Zero.AntJob/Jobs/BuildProduct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ namespace Zero.AntJob.Jobs;
/// <summary>定时创建产品</summary>
internal class BuildProduct : Handler
{
private readonly ICache _cache;
private readonly ICacheProvider _cacheProvider;

public BuildProduct(ICache cache)
public BuildProduct(ICacheProvider cacheProvider)
{
var job = Job;
job.Start = DateTime.Today;
job.Step = 15;

_cache = cache;
_cacheProvider = cacheProvider;
}

protected override Int32 Execute(JobContext ctx)
Expand All @@ -39,7 +39,7 @@ protected override Int32 Execute(JobContext ctx)
};

// Redis去重
if (!_cache.Add($"product:{pi.Name}", DateTime.Now)) pi.Name = Rand.NextString(8);
if (!_cacheProvider.Cache.Add($"product:{pi.Name}", DateTime.Now)) pi.Name = Rand.NextString(8);

list.Add(pi);
}
Expand Down
11 changes: 7 additions & 4 deletions Zero.AntJob/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using AntJob.Providers;
using NewLife;
using NewLife.Caching;
using NewLife.Caching.Services;
using NewLife.Log;
using Zero.AntJob;
using Zero.AntJob.Jobs;
Expand All @@ -15,10 +16,12 @@
{
// 配置星尘。借助StarAgent,或者读取配置文件 config/star.config 中的服务器地址
var star = services.AddStardust(null);

// 配置缓存,使用 MemoryCache 或 Redis,其中Redis从配置中心读取配置信息
var cache = new MemoryCache { Capacity = 1_000_000 };
services.AddSingleton<ICache>(cache);

// 默认内存缓存,如有配置RedisCache可使用Redis缓存
services.AddSingleton<ICacheProvider, RedisCacheProvider>();

// 引入Redis,用于消息队列和缓存,单例,带性能跟踪。一般使用上面的ICacheProvider替代
//services.AddRedis("127.0.0.1:6379", "123456", 3, 5000);

// 注册服务
services.AddSingleton<ITestService, TestService>();
Expand Down
8 changes: 8 additions & 0 deletions Zero.AntJob/Zero.AntJob.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<Content Remove="appsettings.json" />
</ItemGroup>

<ItemGroup>
<None Include="appsettings.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NewLife.AntJob" Version="3.3.2023.814" />
<PackageReference Include="NewLife.AntJob.Extensions" Version="3.3.2023.814" />
Expand Down
21 changes: 21 additions & 0 deletions Zero.AntJob/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
//"StarServer": "http://s.newlifex.com:6600",
//"RedisCache": "server=127.0.0.1:6379;password=;db=3",
"ConnectionStrings": {
"Zero": "Data Source=..\\Data\\Zero.db;Provider=SQLite"

// 各种数据库连接字符串模版,连接名Zero对应Zero.Data/Projects/Model.xml中的ConnName
//"Zero": "Server=.;Port=3306;Database=zero;Uid=root;Pwd=root;Provider=MySql",
//"Zero": "Data Source=.;Initial Catalog=zero;user=sa;password=sa;Provider=SqlServer",
//"Zero": "Server=.;Database=zero;Uid=root;Pwd=root;Provider=PostgreSql",
//"Zero": "Data Source=Tcp://127.0.0.1/ORCL;User Id=scott;Password=tiger;Provider=Oracle"
}
}
2 changes: 1 addition & 1 deletion Zero.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// 配置星尘。自动读取配置文件 config/star.config 中的服务器地址
var star = services.AddStardust();

// 缓存服务
// 默认内存缓存,如有配置RedisCache可使用Redis缓存
services.AddSingleton<ICacheProvider, RedisCacheProvider>();

// 初始化Redis、MQTT、RocketMQ,注册服务到容器
Expand Down
2 changes: 1 addition & 1 deletion Zero.Console/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
}
},
"AllowedHosts": "*",
//"StarServer": "http://star.newlifex.com:6600",
//"StarServer": "http://s.newlifex.com:6600",
//"RedisCache": "server=127.0.0.1:6379;password=;db=3",
"ConnectionStrings": {
"Zero": "Data Source=..\\Data\\Zero.db;Provider=SQLite"
Expand Down
2 changes: 1 addition & 1 deletion Zero.Desktop/ClientSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ClientSetting : Config<ClientSetting>
#region 加载/保存
protected override void OnLoaded()
{
if (Server.IsNullOrEmpty()) Server = "http://star.newlifex.com:6600";
if (Server.IsNullOrEmpty()) Server = "http://s.newlifex.com:6600";

base.OnLoaded();
}
Expand Down
3 changes: 2 additions & 1 deletion Zero.Desktop/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ static void Main()

if (set.IsNew) "学无先后达者为师,欢迎使用新生命零代客户端!".SpeechTip();

EntityFactory.InitAll();
// 预热数据层,执行自动建表等操作
_ = EntityFactory.InitAllAsync();

// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
Expand Down
3 changes: 2 additions & 1 deletion Zero.Desktop/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
}
},
"AllowedHosts": "*",
//"StarServer": "http://star.newlifex.com:6600",
//"StarServer": "http://s.newlifex.com:6600",
//"RedisCache": "server=127.0.0.1:6379;password=;db=3",
"ConnectionStrings": {
"Membership": "Data Source=..\\Data\\Membership.db;Provider=SQLite",
"Log": "Data Source=..\\Data\\Log.db;Provider=SQLite",
Expand Down
10 changes: 9 additions & 1 deletion Zero.HttpServer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using NewLife.Http;
using NewLife.Caching.Services;
using NewLife.Caching;
using NewLife.Http;
using NewLife.Log;
using NewLife.Model;
using NewLife.Remoting;
Expand All @@ -13,6 +15,12 @@
// 配置星尘。自动读取配置文件 config/star.config 中的服务器地址
var star = services.AddStardust();

// 默认内存缓存,如有配置RedisCache可使用Redis缓存
services.AddSingleton<ICacheProvider, RedisCacheProvider>();

// 引入Redis,用于消息队列和缓存,单例,带性能跟踪。一般使用上面的ICacheProvider替代
//services.AddRedis("127.0.0.1:6379", "123456", 3, 5000);

var server = new HttpServer
{
Port = 8080,
Expand Down
1 change: 1 addition & 0 deletions Zero.HttpServer/Zero.HttpServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NewLife.Redis" Version="5.5.2023.1201" />
<PackageReference Include="NewLife.Stardust" Version="2.9.2023.1201" />
<PackageReference Include="NewLife.XCode" Version="11.10.2023.1201" />
</ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion Zero.HttpServer/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
}
},
"AllowedHosts": "*",
//"StarServer": "http://star.newlifex.com:6600",
//"StarServer": "http://s.newlifex.com:6600",
//"RedisCache": "server=127.0.0.1:6379;password=;db=3",
"ConnectionStrings": {
"Zero": "Data Source=..\\Data\\Zero.db;Provider=SQLite"

Expand Down
8 changes: 8 additions & 0 deletions Zero.RpcServer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using NewLife;
using NewLife.Caching.Services;
using NewLife.Caching;
using NewLife.Log;
using NewLife.Model;
using NewLife.Remoting;
Expand All @@ -13,6 +15,12 @@
// 配置星尘。自动读取配置文件 config/star.config 中的服务器地址、应用标识、密钥
var star = services.AddStardust();

// 默认内存缓存,如有配置RedisCache可使用Redis缓存
services.AddSingleton<ICacheProvider, RedisCacheProvider>();

// 引入Redis,用于消息队列和缓存,单例,带性能跟踪。一般使用上面的ICacheProvider替代
//services.AddRedis("127.0.0.1:6379", "123456", 3, 5000);

// 实例化RPC服务端,指定端口,同时在Tcp/Udp/IPv4/IPv6上监听
var server = new ApiServer(12346)
{
Expand Down
1 change: 1 addition & 0 deletions Zero.RpcServer/Zero.RpcServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

<ItemGroup>
<PackageReference Include="NewLife.Core" Version="10.6.2023.1201" />
<PackageReference Include="NewLife.Redis" Version="5.5.2023.1201" />
<PackageReference Include="NewLife.Stardust" Version="2.9.2023.1201" />
</ItemGroup>

Expand Down
3 changes: 2 additions & 1 deletion Zero.RpcServer/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
}
},
"AllowedHosts": "*",
//"StarServer": "http://star.newlifex.com:6600",
//"StarServer": "http://s.newlifex.com:6600",
//"RedisCache": "server=127.0.0.1:6379;password=;db=3",
"ConnectionStrings": {
"Zero": "Data Source=..\\Data\\Zero.db;Provider=SQLite"

Expand Down
23 changes: 21 additions & 2 deletions Zero.Server/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using NewLife;
using NewLife.Agent;
using NewLife.Caching.Services;
using NewLife.Caching;
using NewLife.Log;
using NewLife.Model;
using NewLife.Net;
using NewLife.Remoting;
using NewLife.Threading;
Expand All @@ -11,13 +14,29 @@ namespace Zero.Server;

internal class Program
{
private static void Main(String[] args) => new MyServices().Main(args);
private static void Main(String[] args)
{
// 初始化对象容器,提供注入能力
var services = ObjectContainer.Current;
services.AddSingleton(XTrace.Log);

// 配置星尘。自动读取配置文件 config/star.config 中的服务器地址
var star = services.AddStardust();

// 默认内存缓存,如有配置RedisCache可使用Redis缓存
services.AddSingleton<ICacheProvider, RedisCacheProvider>();

var svc = new MyServices { ServiceProvider = services.BuildServiceProvider() };
svc.Main(args);
}
}

/// <summary>代理服务例子。自定义服务程序可参照该类实现。</summary>
public class MyServices : ServiceBase
{
#region 属性
public IServiceProvider ServiceProvider { get; set; }

/// <summary>性能跟踪器</summary>
public ITracer Tracer { get; set; }

Expand All @@ -44,7 +63,7 @@ public MyServices()
protected override void StartWork(String reason)
{
// 配置星尘。自动读取配置文件 config/star.config 中的服务器地址、应用标识、密钥
_star = new StarFactory();
_star = ServiceProvider.GetService<StarFactory>();
Tracer = _star.Tracer;

InitNetServer();
Expand Down
1 change: 1 addition & 0 deletions Zero.Server/Zero.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<ItemGroup>
<PackageReference Include="NewLife.Agent" Version="10.6.2023.1201" />
<PackageReference Include="NewLife.Core" Version="10.6.2023.1201" />
<PackageReference Include="NewLife.Redis" Version="5.5.2023.1201" />
<PackageReference Include="NewLife.Stardust" Version="2.9.2023.1201" />
<PackageReference Include="NewLife.XCode" Version="11.10.2023.1201" />
</ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion Zero.Server/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
}
},
"AllowedHosts": "*",
//"StarServer": "http://star.newlifex.com:6600",
//"StarServer": "http://s.newlifex.com:6600",
//"RedisCache": "server=127.0.0.1:6379;password=;db=3",
"ConnectionStrings": {
"Zero": "Data Source=..\\Data\\Zero.db;Provider=SQLite"

Expand Down
13 changes: 12 additions & 1 deletion Zero.TcpServer/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Net.Sockets;
using NewLife;
using NewLife.Caching.Services;
using NewLife.Caching;
using NewLife.Log;
using NewLife.Model;
using Stardust;
Expand All @@ -20,14 +22,23 @@ private static async Task Main(String[] args)
// 配置星尘。自动读取配置文件 config/star.config 中的服务器地址、应用标识、密钥
var star = services.AddStardust();

// 默认内存缓存,如有配置RedisCache可使用Redis缓存
services.AddSingleton<ICacheProvider, RedisCacheProvider>();

// 引入Redis,用于消息队列和缓存,单例,带性能跟踪。一般使用上面的ICacheProvider替代
//services.AddRedis("127.0.0.1:6379", "123456", 3, 5000);

// 注入消息处理器,可注入多个
services.AddTransient<IMsgHandler, MyHandler>();

//var provider = services.BuildServiceProvider();
var provider = services.BuildServiceProvider();

// 实例化网络服务端,指定端口,同时在Tcp/Udp/IPv4/IPv6上监听
var server = new MyNetServer
{
Port = 12345,
ServiceProvider = provider,

Log = XTrace.Log,
SessionLog = XTrace.Log,
Tracer = star?.Tracer,
Expand Down
1 change: 1 addition & 0 deletions Zero.TcpServer/Zero.TcpServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

<ItemGroup>
<PackageReference Include="NewLife.Core" Version="10.6.2023.1201" />
<PackageReference Include="NewLife.Redis" Version="5.5.2023.1201" />
<PackageReference Include="NewLife.Stardust" Version="2.9.2023.1201" />
<PackageReference Include="NewLife.XCode" Version="11.10.2023.1201" />
</ItemGroup>
Expand Down
3 changes: 2 additions & 1 deletion Zero.TcpServer/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
}
},
"AllowedHosts": "*",
//"StarServer": "http://star.newlifex.com:6600",
//"StarServer": "http://s.newlifex.com:6600",
//"RedisCache": "server=127.0.0.1:6379;password=;db=3",
"ConnectionStrings": {
"Zero": "Data Source=..\\Data\\Zero.db;Provider=SQLite"

Expand Down
20 changes: 6 additions & 14 deletions Zero.Web/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using NewLife;
using NewLife.Caching;
using NewLife.Caching;
using NewLife.Caching.Services;
using NewLife.Cube;
using NewLife.Log;
using XCode;
Expand All @@ -19,15 +19,11 @@
// 配置星尘。借助StarAgent,或者读取配置文件 config/star.config 中的服务器地址
var star = services.AddStardust(null);

// 启用星尘配置中心。分布式部署或容器化部署推荐使用,单机部署不推荐使用
var config = star.Config;
// 默认内存缓存,如有配置RedisCache可使用Redis缓存
services.AddSingleton<ICacheProvider, RedisCacheProvider>();

// 默认内存缓存,如有配置可使用Redis缓存
var cache = new MemoryCache();
if (config != null && !config["redisCache"].IsNullOrEmpty())
services.AddSingleton<ICache>(p => new FullRedis(p, "redisCache") { Name = "Cache", Tracer = star.Tracer });
else
services.AddSingleton<ICache>(cache);
// 引入Redis,用于消息队列和缓存,单例,带性能跟踪。一般使用上面的ICacheProvider替代
//services.AddRedis("127.0.0.1:6379", "123456", 3, 5000);

// 启用接口响应压缩
services.AddResponseCompression();
Expand All @@ -44,10 +40,6 @@

var app = builder.Build();

// 预热数据层,执行自动建表等操作
// 连接名 Zero 对应连接字符串名字,同时也对应 Zero.Data/Projects/Model.xml 头部的 ConnName
EntityFactory.InitConnection("Zero");

// 使用Cube前添加自己的管道
if (app.Environment.IsDevelopment())
app.UseDeveloperExceptionPage();
Expand Down
3 changes: 2 additions & 1 deletion Zero.Web/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
},
"AllowedHosts": "*",
"Urls": "http://*:6000;https://*:6001",
//"StarServer": "http://star.newlifex.com:6600",
//"StarServer": "http://s.newlifex.com:6600",
//"RedisCache": "server=127.0.0.1:6379;password=;db=3",
"ConnectionStrings": {
"Zero": "Data Source=..\\Data\\Zero.db;Provider=SQLite",
"Membership": "Data Source=..\\Data\\Membership.db;Provider=SQLite",
Expand Down
Loading

0 comments on commit 456e84d

Please sign in to comment.