重构代码:移动Log缓存处理到LogHelper类中

This commit is contained in:
Argo-MacBookPro 2018-10-30 22:11:27 +08:00
parent f912f0cacb
commit b65676166e
5 changed files with 20 additions and 19 deletions

View File

@ -13,11 +13,13 @@ namespace Bootstrap.DataAccess.MongoDB
///
/// </summary>
/// <returns></returns>
public override IEnumerable<DataAccess.Log> RetrieveLogs()
{
var log = MongoDbAccessManager.DBAccess.GetCollection<DataAccess.Log>("Logs");
return log.Find(l => l.LogTime >= DateTime.Now.AddDays(-7)).ToList();
}
public override IEnumerable<DataAccess.Log> RetrieveLogs() => MongoDbAccessManager.Logs.Find(l => l.LogTime >= DateTime.Now.AddDays(-7)).ToList();
/// <summary>
/// 删除日志信息
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private static void DeleteLogAsync() => System.Threading.Tasks.Task.Run(() => MongoDbAccessManager.Logs.DeleteMany(log => log.LogTime < DateTime.Now.AddDays(-7)));
/// <summary>
///
/// </summary>
@ -25,9 +27,8 @@ namespace Bootstrap.DataAccess.MongoDB
/// <returns></returns>
public override bool SaveLog(DataAccess.Log log)
{
var logs = MongoDbAccessManager.DBAccess.GetCollection<DataAccess.Log>("Logs");
log.LogTime = DateTime.Now;
logs.InsertOne(log);
MongoDbAccessManager.Logs.InsertOne(log);
return true;
}
}

View File

@ -39,11 +39,11 @@ namespace Bootstrap.DataAccess.MongoDB
/// <summary>
///
/// </summary>
public static IMongoCollection<DataAccess.User> Users
public static IMongoCollection<DataAccess.Log> Logs
{
get
{
return DBAccess.GetCollection<DataAccess.User>("Users");
return DBAccess.GetCollection<DataAccess.Log>("Logs");
}
}
/// <summary>
@ -72,6 +72,7 @@ namespace Bootstrap.DataAccess.MongoDB
private static void InitClassMap()
{
BsonSerializer.RegisterSerializer(DateTimeSerializer.LocalInstance);
if (!BsonClassMap.IsClassMapRegistered(typeof(BootstrapDict)))
{
BsonClassMap.RegisterClassMap<BootstrapDict>(md =>

View File

@ -1,5 +1,4 @@
using Longbow;
using Longbow.Cache;
using Longbow.Configuration;
using System;
using System.Collections.Generic;
@ -46,7 +45,7 @@ namespace Bootstrap.DataAccess.SQLite
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private void DeleteLogAsync()
private static void DeleteLogAsync()
{
System.Threading.Tasks.Task.Run(() =>
{
@ -74,7 +73,6 @@ namespace Bootstrap.DataAccess.SQLite
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@RequestUrl", p.RequestUrl));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
}
CacheManager.Clear(LogHelper.RetrieveLogsDataKey);
DeleteLogAsync();
return ret;
}

View File

@ -1,8 +1,6 @@
using Longbow.Cache;
using Longbow.Data;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess
{
@ -26,6 +24,11 @@ namespace Bootstrap.DataAccess
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static bool SaveLog(Log p) => DbAdapterManager.Create<Log>().SaveLog(p);
public static bool SaveLog(Log p)
{
var ret = DbAdapterManager.Create<Log>().SaveLog(p);
if (ret) CacheManager.Clear(RetrieveLogsDataKey);
return ret;
}
}
}

View File

@ -1,5 +1,4 @@
using Longbow.Cache;
using Longbow.Configuration;
using Longbow.Configuration;
using System;
using System.Collections.Generic;
using System.Data;
@ -79,7 +78,7 @@ namespace Bootstrap.DataAccess
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private void DeleteLogAsync()
private static void DeleteLogAsync()
{
System.Threading.Tasks.Task.Run(() =>
{
@ -107,7 +106,6 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@RequestUrl", p.RequestUrl));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
}
CacheManager.Clear(LogHelper.RetrieveLogsDataKey);
DeleteLogAsync();
return ret;
}