Skip to content

Commit

Permalink
Merge pull request #76 from DiskTools/gan601
Browse files Browse the repository at this point in the history
邮件缓存
  • Loading branch information
GaN8373 authored Jun 27, 2023
2 parents 262a5d7 + d50592e commit 242332d
Show file tree
Hide file tree
Showing 19 changed files with 437 additions and 341 deletions.
37 changes: 30 additions & 7 deletions Mail/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Mail.Extensions;
using Mail.Pages;
using Mail.Services;
using Mail.Services.Data;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Toolkit.Uwp.Helpers;
using SqlSugar;
Expand Down Expand Up @@ -38,7 +40,8 @@ private void RegisterServices(IServiceCollection services)

services.AddSingleton<OutlookService, OutlookService>()
.AddSingleton<ICacheService, CacheService>()
.AddSingleton<ISqlSugarClient>(_ =>
.AddSingleton<IMemoryCache>(_ => new MemoryCache(new MemoryCacheOptions()))
.AddSingleton<ISqlSugarClient>(factory =>
{
var client = new SqlSugarScope(new ConnectionConfig
{
Expand All @@ -58,15 +61,35 @@ private void RegisterServices(IServiceCollection services)
}
}
},
});
//调式代码 用来打印SQL
client.Aop.OnLogExecuting = (sql, pars) =>
}, db =>
{
Trace.WriteLine(
$"ExecSql: {sql}\r\n{client.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))}");
};
var cache = factory.GetRequiredService<IMemoryCache>();
db.Aop.DataExecuting = (_, entityInfo) =>
{
var value = entityInfo.EntityValue;
var key = entityInfo.OperationType + value.GetHashCode();
if (cache.Get(key) is not null) return;

cache.Set(key, 1, TimeSpan.FromSeconds(2));
db.GetDbOperationEvent().OnExecEvent(value, entityInfo.OperationType);
};
//db.Aop.DataExecuted = (entity, entityInfo) => { Trace.WriteLine($"DataExecuted: {entity}"); };

#if DEBUG
//调式代码 用来打印SQL
db.Aop.OnLogExecuting = (sql, pars) =>
{
Trace.WriteLine(
$"ExecSql: {sql}\r\n{db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))}\n");
};
#endif
});

client.DbMaintenance.CreateDatabase();
client.CodeFirst.InitTables<MailFolderData>();
client.CodeFirst.InitTables<MailMessageData>();
client.CodeFirst.InitTables<MailMessageContentData>();
client.CodeFirst.InitTables<MailMessageRecipientData>();
return client;
})
.BuildServiceProvider();
Expand Down
33 changes: 6 additions & 27 deletions Mail/Extensions/DbClientExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Mail.Interfaces;
using SqlSugar;

namespace Mail.Extensions;
Expand All @@ -15,66 +14,46 @@ namespace Mail.Extensions;
/// </summary>
public static class DbClientExtension
{
private static readonly DbOperationEvent<DbEntity> DbOperationEvent = new();
private static readonly DbOperationEvent DbOperationEvent = new();

/// <summary>
/// 只有扩展方法会调用, 懒得写ISqlSugarClient的AOP
/// </summary>
/// <param name="Client"></param>
/// <returns></returns>
public static DbOperationEvent<DbEntity> GetDbOperationEvent(this ISqlSugarClient Client)
public static DbOperationEvent GetDbOperationEvent(this ISqlSugarClient Client)
{
return DbOperationEvent;
}

public static async Task<int> SaveOrUpdate<T>(this ISqlSugarClient Client, T entity,
CancellationToken CancellationToken = default) where T : DbEntity, new()
CancellationToken CancellationToken = default) where T : class, new()
{
var x = await Client.Storageable(entity).WhereColumns(x => x.Id).ToStorageAsync();
var x = await Client.Storageable(entity).ToStorageAsync();
var executeCommandAsync = await x.AsInsertable.ExecuteCommandAsync(CancellationToken);
if (executeCommandAsync > 0)
{
DbOperationEvent.OnSave(entity);
return executeCommandAsync;
}

var commandAsync = await x.AsUpdateable.ExecuteCommandAsync(CancellationToken);
if (commandAsync > 0)
{
DbOperationEvent.OnUpdate(entity);
}

return commandAsync;
}

public static async Task<int> SaveOrUpdate<T>(this ISqlSugarClient Client, IEnumerable<T> entity,
CancellationToken CancellationToken = default) where T : DbEntity, new()
CancellationToken CancellationToken = default) where T : class, new()
{
var dbEntities = entity.ToList();
var x = await Client.Storageable(dbEntities).WhereColumns(x => x.Id).ToStorageAsync();
var x = await Client.Storageable(dbEntities).ToStorageAsync();
var executeCommandAsync = await x.AsInsertable.ExecuteCommandAsync(CancellationToken);
if (executeCommandAsync > 0)
{
foreach (var e in dbEntities) DbOperationEvent.OnSave(e);
return executeCommandAsync;
}

var commandAsync = await x.AsUpdateable.ExecuteCommandAsync(CancellationToken);
if (commandAsync > 0)
foreach (var e in dbEntities)
DbOperationEvent.OnUpdate(e);

return commandAsync;
}

public static async Task<int> Remove<T>(this ISqlSugarClient Client, T entity) where T : DbEntity, new()
{
var result = await Client.Deleteable<T>().WhereColumns(entity, x => x.Id).ExecuteCommandAsync();
if (result > 0)
{
DbOperationEvent.OnRemove(entity);
}

return result;
}
}
28 changes: 6 additions & 22 deletions Mail/Extensions/DbOperationEvent.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Mail.Interfaces;
using SqlSugar;

namespace Mail.Extensions;

Expand All @@ -8,30 +8,14 @@ namespace Mail.Extensions;
/// 创建者: GaN<br/>
/// 创建时间: 2023/06/15
/// </summary>
public class DbOperationEvent<T> where T : DbEntity
public class DbOperationEvent
{
public delegate void DbSave(T entity);
public delegate void DataExecuting(object entity, DataFilterType Type);

public delegate void DbUpdate(T entity);
public event DataExecuting? ExecEvent;

public delegate void DbRemove(T entity);

public event DbSave? SaveEvent;
public event DbUpdate? UpdateEvent;
public event DbRemove? RemoveEvent;

public virtual void OnSave(T Entity)
{
SaveEvent?.Invoke(Entity);
}

public virtual void OnUpdate(T Entity)
{
UpdateEvent?.Invoke(Entity);
}

public virtual void OnRemove(T Entity)
public void OnExecEvent(object Entity, DataFilterType Type)
{
RemoveEvent?.Invoke(Entity);
ExecEvent?.Invoke(Entity, Type);
}
}
43 changes: 43 additions & 0 deletions Mail/Extensions/MailMessageDataExtentions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Collections.Generic;
using Mail.Services.Data;
using Mail.Services.Data.Enums;

namespace Mail.Extensions;

/// <summary>
/// comment<br/>
/// <br/>
/// 创建者: GaN<br/>
/// 创建时间: 2023/06/18
/// </summary>
public static class MailMessageDataExtentions
{
public static IEnumerable<MailMessageRecipientData> GetRecipientData(this MailMessageData Data)
{
var msgId = Data.Id;
foreach (var to in Data.To)
{
to.Id = msgId;
to.RecipientType = RecipientType.To;
yield return to;
}

foreach (var to in Data.CC)
{
to.Id = msgId;
to.RecipientType = RecipientType.Cc;
yield return to;
}

foreach (var to in Data.Bcc)
{
to.Id = msgId;
to.RecipientType = RecipientType.Bcc;
yield return to;
}

Data.Sender.Id = msgId;
Data.Sender.RecipientType = RecipientType.Sender;
yield return Data.Sender;
}
}
6 changes: 4 additions & 2 deletions Mail/Mail.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,12 @@
<Compile Include="Extensions\ColorExtensions.cs" />
<Compile Include="Extensions\DbClientExtension.cs" />
<Compile Include="Extensions\DbOperationEvent.cs" />
<Compile Include="Extensions\MailMessageDataExtentions.cs"/>
<Compile Include="Interfaces\DbEntity.cs" />
<Compile Include="Extensions\Graph\MsalProvider.cs" />
<Compile Include="Extensions\RunCatch.cs" />
<Compile Include="Models\EditMailOption.cs" />
<Compile Include="Models\LoadMailMessageOption.cs"/>
<Compile Include="Pages\BlankPage.xaml.cs">
<DependentUpon>BlankPage.xaml</DependentUpon>
</Compile>
Expand All @@ -149,9 +151,9 @@
<Compile Include="Services\CacheService.cs" />
<Compile Include="Services\Data\Enums\EditMailType.cs" />
<Compile Include="Services\Data\Enums\MailType.cs" />
<Compile Include="Services\Data\Enums\RecipientType.cs"/>
<Compile Include="Services\Data\IMailMessageAttachmentData.cs" />
<Compile Include="Services\Data\MailFolderData.cs" />
<Compile Include="Services\Data\MailFolderDetailData.cs" />
<Compile Include="Services\Data\MailMessageFileAttachmentData.cs" />
<Compile Include="Services\Data\MailMessageAttachmentData.cs" />
<Compile Include="Services\Data\MailMessageContentData.cs" />
Expand Down Expand Up @@ -335,7 +337,7 @@
<Version>5.1.2</Version>
</PackageReference>
<PackageReference Include="SqlSugar">
<Version>5.1.4.83</Version>
<Version>5.1.4.84</Version>
</PackageReference>
<PackageReference Include="System.Data.SQLite">
<Version>1.0.118</Version>
Expand Down
26 changes: 26 additions & 0 deletions Mail/Models/LoadMailMessageOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace Mail.Models;

/// <summary>
/// comment<br/>
/// <br/>
/// 创建者: GaN<br/>
/// 创建时间: 2023/06/20
/// </summary>
public class LoadMailMessageOption
{
public LoadMailMessageOption(string FolderId)
{
this.FolderId = FolderId;
}

public LoadMailMessageOption(string FolderId, bool IsFocusedTab)
{
this.FolderId = FolderId;
this.IsFocusedTab = IsFocusedTab;
}

public string FolderId { get; set; }
public bool IsFocusedTab { get; set; } = true;
public int StartIndex { get; set; }
public int LoadCount { get; set; } = 30;
}
20 changes: 13 additions & 7 deletions Mail/Pages/MailFolderDetailsPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ namespace Mail.Pages
public sealed partial class MailFolderDetailsPage : Page
{
private readonly AsyncLock SelectionChangeLocker = new();
private MailIncrementalLoadingObservableCollection<MailMessageListDetailViewModel>? PreviewSource;
private MailIncrementalLoadingObservableCollection? PreviewSource;
private MailFolderData? NavigationData;
private bool IsFocusedTab { get; set; } = true;
private IMailService Service;
Expand Down Expand Up @@ -93,19 +93,22 @@ private async Task RefreshData()
App.Services.GetService<ICacheService>()!.AddOrReplaceCache(contacts);
*/

var isFocusedTab = IsFocusedTab && data.Type == MailFolderType.Inbox;
var option = new LoadMailMessageOption(data.Id, isFocusedTab);

DetailsView.ItemsSource = PreviewSource =
new MailIncrementalLoadingObservableCollection<MailMessageListDetailViewModel>(service, data.Type,
mailFolder, (messageData) => new MailMessageListDetailViewModel(messageData),
IsFocusTab: IsFocusedTab);
new MailIncrementalLoadingObservableCollection(service,
mailFolder,
IsFocusTab: isFocusedTab);

IAsyncEnumerable<MailMessageData> dataSet;
if (data.Type == MailFolderType.Inbox && service is IMailService.IFocusFilterSupport filterService)
{
dataSet = filterService.GetMailMessageAsync(data.Id, IsFocusedTab);
dataSet = filterService.GetMailMessageAsync(option);
}
else
{
dataSet = service.GetMailMessageAsync(mailFolder.Id);
dataSet = service.GetMailMessageAsync(option);
}

await foreach (var messageData in dataSet)
Expand All @@ -118,8 +121,11 @@ private async Task RefreshData()
EmptyContentText.Text = "No available email";
}
}
catch (Exception)
catch (Exception e)
{
#if DEBUG
Trace.WriteLine(e);
#endif
EmptyContentText.Text = "Sync failed";
}
}
Expand Down
3 changes: 1 addition & 2 deletions Mail/Services/CacheService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ namespace Mail.Services;

public class CacheService : ICacheService
{

private readonly Dictionary<Type, object> Caches = new();

public void AddOrReplaceCache<T>(T cache) where T : class
{
Caches[typeof(T)] = cache;
Expand Down
Loading

0 comments on commit 242332d

Please sign in to comment.