-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
31 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace XCode.Common; | ||
|
||
/// <summary> | ||
/// 基于键的锁定器 | ||
/// </summary> | ||
internal class KeyedLocker<TEntity> | ||
{ | ||
private static object[] Lockers; | ||
static KeyedLocker() | ||
{ | ||
int Length = 8; | ||
var temp = new object[Length]; | ||
for (int i = 0; i < Length; i++) temp[i] = new object(); | ||
Lockers = temp; | ||
} | ||
|
||
public static object SharedLock(string key) | ||
{ | ||
if (key is null) throw new ArgumentNullException(nameof(key)); | ||
var code = key.GetHashCode(); | ||
return Lockers[Math.Abs(code % Lockers.Length)]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters