重构代码:移动Dict缓存处理到DictHelper类中

This commit is contained in:
Argo-MacBookPro 2018-10-30 23:01:36 +08:00
parent 45cbc0eb53
commit 771dbed0a6
5 changed files with 44 additions and 11 deletions

View File

@ -14,10 +14,21 @@ namespace Bootstrap.DataAccess.MongoDB
///
/// </summary>
/// <returns></returns>
public override IEnumerable<BootstrapDict> RetrieveDicts()
public override IEnumerable<BootstrapDict> RetrieveDicts() => MongoDbAccessManager.Dicts.Find(FilterDefinition<BootstrapDict>.Empty).ToList();
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public override bool DeleteDict(IEnumerable<string> value)
{
var dicts = MongoDbAccessManager.DBAccess.GetCollection<Dict>("Dicts");
return dicts.Find(FilterDefinition<Dict>.Empty).ToList();
var list = new List<WriteModel<BootstrapDict>>();
foreach (var id in value)
{
list.Add(new DeleteOneModel<BootstrapDict>(Builders<BootstrapDict>.Filter.Eq(md => md.Id, id)));
}
MongoDbAccessManager.Dicts.BulkWrite(list);
return true;
}
}
}

View File

@ -59,6 +59,16 @@ namespace Bootstrap.DataAccess.MongoDB
/// <summary>
///
/// </summary>
public static IMongoCollection<BootstrapDict> Dicts
{
get
{
return DBAccess.GetCollection<BootstrapDict>("Dicts");
}
}
/// <summary>
///
/// </summary>
public static IMongoCollection<User> Users
{
get
@ -66,7 +76,7 @@ namespace Bootstrap.DataAccess.MongoDB
return DBAccess.GetCollection<User>("Users");
}
}
private static void InitDb()
{
var connectString = DbAdapterManager.GetConnectionString("ba");

View File

@ -19,7 +19,7 @@ namespace Bootstrap.DataAccess
/// <param name="menuIds"></param>
/// <param name="dictIds"></param>
/// <param name="cacheKey"></param>
public static void ClearCache(IEnumerable<string> roleIds = null, IEnumerable<string> userIds = null, IEnumerable<string> groupIds = null, IEnumerable<string> menuIds = null, string dictIds = null, string cacheKey = null)
public static void ClearCache(IEnumerable<string> roleIds = null, IEnumerable<string> userIds = null, IEnumerable<string> groupIds = null, IEnumerable<string> menuIds = null, IEnumerable<string> dictIds = null, string cacheKey = null)
{
var cacheKeys = new List<string>();
var corsKeys = new List<string>();

View File

@ -26,7 +26,6 @@ namespace Bootstrap.DataAccess
using (DbCommand cmd = DbAccessManager.DBAccess.CreateCommand(CommandType.Text, sql))
{
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == value.Count();
CacheCleanUtility.ClearCache(dictIds: ids);
}
return ret;
}
@ -53,7 +52,6 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Define", dict.Define));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
}
CacheCleanUtility.ClearCache(dictIds: string.IsNullOrEmpty(dict.Id) ? string.Empty : dict.Id.ToString());
return ret;
}
/// <summary>
@ -74,7 +72,6 @@ namespace Bootstrap.DataAccess
cmd.Parameters.Add(DbAccessManager.DBAccess.CreateParameter("@Category", dict.Category));
ret = DbAccessManager.DBAccess.ExecuteNonQuery(cmd) == 1;
}
CacheCleanUtility.ClearCache(dictIds: string.Empty);
return ret;
}
/// <summary>

View File

@ -31,13 +31,23 @@ namespace Bootstrap.DataAccess
/// </summary>
/// <param name="value">需要删除的IDs</param>
/// <returns></returns>
public static bool DeleteDict(IEnumerable<string> value) => DbAdapterManager.Create<Dict>().DeleteDict(value);
public static bool DeleteDict(IEnumerable<string> value)
{
var ret = DbAdapterManager.Create<Dict>().DeleteDict(value);
CacheCleanUtility.ClearCache(dictIds: value);
return ret;
}
/// <summary>
/// 保存新建/更新的字典信息
/// </summary>
/// <param name="p"></param>
/// <returns></returns>
public static bool SaveDict(BootstrapDict p) => DbAdapterManager.Create<Dict>().SaveDict(p);
public static bool SaveDict(BootstrapDict p)
{
var ret = DbAdapterManager.Create<Dict>().SaveDict(p);
if (ret) CacheCleanUtility.ClearCache( new List<string>() { p.Id });
return ret;
}
/// <summary>
/// 保存网站个性化设置
/// </summary>
@ -45,7 +55,12 @@ namespace Bootstrap.DataAccess
/// <param name="code"></param>
/// <param name="category"></param>
/// <returns></returns>
public static bool SaveSettings(BootstrapDict dict) => DbAdapterManager.Create<Dict>().SaveSettings(dict);
public static bool SaveSettings(BootstrapDict dict)
{
var ret = DbAdapterManager.Create<Dict>().SaveSettings(dict);
if (ret) CacheCleanUtility.ClearCache(dictIds: new List<string>());
return ret;
}
/// <summary>
/// 获取字典分类名称
/// </summary>