Skip to content

Commit

Permalink
用户数据量较大时,使用单对象缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jun 29, 2018
1 parent dd4dcf8 commit 8ddd86c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
20 changes: 11 additions & 9 deletions XCode/Membership/用户.Biz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ static User()
// 不过这不是理由,同一个线程遇到同一个锁不会堵塞
// 发生死锁的可能性是这里引发EnsureInit,而另一个线程提前引发EnsureInit拿到锁
Meta.Factory.AdditionalFields.Add(__.Logins);

// 单对象缓存
var sc = Meta.SingleCache;
sc.FindSlaveKeyMethod = k => Find(__.Name, k);
sc.GetSlaveKeyMethod = e => e.Name;
}

/// <summary>首次连接数据库时初始化数据,仅用于实体类重载,用户不应该调用该方法</summary>
Expand Down Expand Up @@ -202,13 +207,10 @@ public static TEntity FindByID(Int32 id)
{
if (id <= 0) return null;

if (Meta.Count >= 1000)
return Find(__.ID, id);
else // 实体缓存
return Meta.Cache.Find(e => e.ID == id);
if (Meta.Count < 1000) return Meta.Cache.Find(e => e.ID == id);

// 实体缓存
//return Meta.SingleCache[id];
return Meta.SingleCache[id];
}

/// <summary>根据名称查找</summary>
Expand All @@ -218,10 +220,10 @@ public static TEntity FindByName(String name)
{
if (name.IsNullOrEmpty()) return null;

if (Meta.Count >= 1000)
return Find(__.Name, name);
else // 实体缓存
return Meta.Cache.Find(e => e.Name.EqualIgnoreCase(name));
if (Meta.Count < 1000) return Meta.Cache.Find(e => e.Name.EqualIgnoreCase(name));

// 单对象缓存
return Meta.SingleCache.GetItemWithSlaveKey(name) as TEntity;
}

/// <summary>根据邮箱地址查找</summary>
Expand Down
5 changes: 0 additions & 5 deletions XCode/Membership/菜单.Biz.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
using NewLife.Reflection;
using NewLife.Collections;
using NewLife.Threading;
#if NET4
using System.Threading.Tasks;
#else
using TaskEx = System.Threading.Tasks.Task;
#endif

namespace XCode.Membership
{
Expand Down

0 comments on commit 8ddd86c

Please sign in to comment.