重构代码:移动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> /// </summary>
/// <returns></returns> /// <returns></returns>
public override IEnumerable<DataAccess.Log> RetrieveLogs() public override IEnumerable<DataAccess.Log> RetrieveLogs() => MongoDbAccessManager.Logs.Find(l => l.LogTime >= DateTime.Now.AddDays(-7)).ToList();
{ /// <summary>
var log = MongoDbAccessManager.DBAccess.GetCollection<DataAccess.Log>("Logs"); /// 删除日志信息
return log.Find(l => l.LogTime >= DateTime.Now.AddDays(-7)).ToList(); /// </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>
/// ///
/// </summary> /// </summary>
@ -25,9 +27,8 @@ namespace Bootstrap.DataAccess.MongoDB
/// <returns></returns> /// <returns></returns>
public override bool SaveLog(DataAccess.Log log) public override bool SaveLog(DataAccess.Log log)
{ {
var logs = MongoDbAccessManager.DBAccess.GetCollection<DataAccess.Log>("Logs");
log.LogTime = DateTime.Now; log.LogTime = DateTime.Now;
logs.InsertOne(log); MongoDbAccessManager.Logs.InsertOne(log);
return true; return true;
} }
} }

View File

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

View File

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

View File

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