Skip to content

Commit

Permalink
perf: The same service that optimizes the GUID change cannot get the …
Browse files Browse the repository at this point in the history
…cache, so that another service cannot get the cache
  • Loading branch information
ZGGSONG committed Jan 14, 2024
1 parent 5b1ff9b commit e9c06c1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion STranslate/Helper/ConfigHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ JsonSerializer serializer
(int)ServiceType.ApiService => new TranslatorApi(),
(int)ServiceType.BaiduService => new TranslatorBaidu(),
(int)ServiceType.BingService => new TranslatorBing(),
//TODO: 更多其他服务在这里添加
//TODO: 新接口需要适配
_ => throw new NotSupportedException($"Unsupported ServiceType: {type}")
};

Expand Down
16 changes: 11 additions & 5 deletions STranslate/ViewModels/InputViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using STranslate.Model;
using STranslate.Util;
using STranslate.ViewModels.Preference;
using STranslate.ViewModels.Preference.Services;

namespace STranslate.ViewModels
{
Expand Down Expand Up @@ -184,6 +185,7 @@ await Parallel.ForEachAsync(
}

//根据不同服务类型区分
//TODO: 新接口需要适配
switch (service.Type)
{
case ServiceType.ApiService:
Expand Down Expand Up @@ -435,7 +437,7 @@ protected override IList<JsonProperty> CreateProperties(Type type, MemberSeriali
/// </summary>
public class CurrentTranslatorConverter : JsonConverter<ITranslator>
{
public override ITranslator ReadJson(
public override ITranslator? ReadJson(
JsonReader reader,
Type objectType,
ITranslator? existingValue,
Expand All @@ -451,13 +453,17 @@ JsonSerializer serializer

// 从 JSON 中提取 Identify 字段的值,用于确定具体实现类
var identify = jsonObject["Identify"]!.Value<string>();
var type = jsonObject["Type"]!.Value<int>();
ITranslator translator;

// 根据 Identify 查找匹配的翻译服务
// TODO: 优化删除配置后Identify更新后与数据库不一致导致的报错问题
translator =
translators.FirstOrDefault(x => x.Identify.ToString() == identify)
?? throw new NotSupportedException($"Unsupported Service: {identify}");
//TODO: 新接口需要适配
translator = translators.FirstOrDefault(x => x.Identify.ToString() == identify) ?? type switch
{
(int)ServiceType.BaiduService => new TranslatorBaidu(),
(int)ServiceType.BingService => new TranslatorBing(),
_ => new TranslatorApi(),
};

// 从 JSON 中提取 Data 字段的值,设置到 translator 的 Data 属性中
translator.Data = jsonObject["Data"]!.Value<string>()!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ JsonSerializer serializer
(int)ServiceType.ApiService => new TranslatorApi(),
(int)ServiceType.BaiduService => new TranslatorBaidu(),
(int)ServiceType.BingService => new TranslatorBing(),
//TODO: 更多其他服务在这里添加
//TODO: 新接口需要适配
_ => throw new NotSupportedException($"Unsupported ServiceType: {type}")
};

Expand Down
3 changes: 3 additions & 0 deletions STranslate/ViewModels/Preference/ServiceViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public partial class ServiceViewModel : ObservableObject
public ServiceViewModel()
{
//添加默认支持服务
//TODO: 新接口需要适配
TransServices.Add(new TranslatorApi());
TransServices.Add(new TranslatorBaidu());
TransServices.Add(new TranslatorBing());
Expand Down Expand Up @@ -78,6 +79,7 @@ private void TogglePage(ITranslator service)
tmpIndex = SelectedIndex;

string head = "STranslate.Views.Preference.Service.";
//TODO: 新接口需要适配
var name = service.Type switch
{
ServiceType.ApiService => string.Format("{0}TextApiServicePage", head),
Expand All @@ -100,6 +102,7 @@ private void Add(List<object> list)
{
var service = list.First();

//TODO: 新接口需要适配
CurTransServiceList.Add(service switch
{
TranslatorApi api => api.DeepClone(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ public async Task<object> TranslateAsync(object request, CancellationToken token
if (string.IsNullOrEmpty(resp))
throw new Exception("请求结果为空");

//TODO: 有问题
var ret = JsonConvert.DeserializeObject<ResponseBing[]>(resp ?? "");

//如果出错就将整个返回信息写入取值处
Expand Down

0 comments on commit e9c06c1

Please sign in to comment.