mirror of
https://github.com/sourcegit-scm/sourcegit.git
synced 2024-12-24 20:57:19 -08:00
Merge pull request #132 from gadfly3173/refactor/models-user
refactor: Simplify FindOrAdd method by using ConcurrentDictionary's GetOrAdd
This commit is contained in:
commit
f973834dab
1 changed files with 6 additions and 12 deletions
|
@ -25,20 +25,14 @@ namespace SourceGit.Models
|
||||||
|
|
||||||
public static User FindOrAdd(string data)
|
public static User FindOrAdd(string data)
|
||||||
{
|
{
|
||||||
if (_caches.TryGetValue(data, out var value))
|
return _caches.GetOrAdd(data, key =>
|
||||||
{
|
{
|
||||||
return value;
|
var nameEndIdx = key.IndexOf('<', System.StringComparison.Ordinal);
|
||||||
}
|
var name = nameEndIdx >= 2 ? key.Substring(0, nameEndIdx - 1) : string.Empty;
|
||||||
else
|
var email = key.Substring(nameEndIdx + 1);
|
||||||
{
|
|
||||||
var nameEndIdx = data.IndexOf('<', System.StringComparison.Ordinal);
|
|
||||||
var name = nameEndIdx >= 2 ? data.Substring(0, nameEndIdx - 1) : string.Empty;
|
|
||||||
var email = data.Substring(nameEndIdx + 1);
|
|
||||||
|
|
||||||
User user = new User() { Name = name, Email = email };
|
return new User() { Name = name, Email = email };
|
||||||
_caches.TryAdd(data, user);
|
});
|
||||||
return user;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static ConcurrentDictionary<string, User> _caches = new ConcurrentDictionary<string, User>();
|
private static ConcurrentDictionary<string, User> _caches = new ConcurrentDictionary<string, User>();
|
||||||
|
|
Loading…
Reference in a new issue