-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/DigitalPlatform/chord
- Loading branch information
Showing
7 changed files
with
175 additions
and
12 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
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,118 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
using DigitalPlatform.SIP.Server; | ||
|
||
namespace dp2Capo | ||
{ | ||
/// <summary> | ||
/// 登录信息缓存机制。避免高密度的登录操作过度耗费 CPU 资源 | ||
/// </summary> | ||
public class LoginCache | ||
{ | ||
Hashtable _table = new Hashtable(); | ||
|
||
// 储存信息 | ||
public void Set(SipChannel channel, | ||
string userName, | ||
string password, | ||
string style) | ||
{ | ||
lock (_table.SyncRoot) | ||
{ | ||
// 限制 hashtable 最大元素数 | ||
if (_table.Count > 10000) | ||
_table.Clear(); | ||
|
||
_table[channel.GetHashCode()] = new LoginCacheItem | ||
{ | ||
ChannelCode = channel.GetHashCode(), | ||
UserName = userName, | ||
Password = password, | ||
Style = style, | ||
}; | ||
} | ||
} | ||
|
||
// 清除 | ||
public void Clear(SipChannel channel) | ||
{ | ||
lock (_table.SyncRoot) | ||
{ | ||
_table.Remove(channel.GetHashCode()); | ||
} | ||
} | ||
|
||
// 是否包含? | ||
public bool Contains(SipChannel channel, | ||
string userName, | ||
string password, | ||
string style) | ||
{ | ||
var code = channel.GetHashCode(); | ||
lock (_table.SyncRoot) | ||
{ | ||
var item = _table[code] as LoginCacheItem; | ||
if (item == null) | ||
return false; | ||
if (item.UserName != userName | ||
|| item.Password != password | ||
|| item.Style != style) | ||
{ | ||
_table.Remove(code); | ||
return false; | ||
} | ||
|
||
item.Touch(); | ||
return true; | ||
} | ||
} | ||
|
||
// 清除衰老的事项 | ||
public void CleanOld(TimeSpan length) | ||
{ | ||
DateTime now = DateTime.Now; | ||
lock (_table.SyncRoot) | ||
{ | ||
List<int> delete_keys = new List<int>(); | ||
foreach(int key in _table.Keys) | ||
{ | ||
var item = _table[key] as LoginCacheItem; | ||
if (now - item.CreatedTime > length) | ||
delete_keys.Add(key); | ||
} | ||
|
||
foreach(int key in delete_keys) | ||
{ | ||
_table.Remove(key); | ||
} | ||
} | ||
} | ||
} | ||
|
||
class LoginCacheItem | ||
{ | ||
public int ChannelCode { get; set; } | ||
public string UserName { get; set; } | ||
public string Password { get; set; } | ||
public string Style { get; set; } | ||
|
||
public DateTime CreatedTime { get; set; } | ||
public DateTime LastTime { get; set; } | ||
|
||
public LoginCacheItem() | ||
{ | ||
CreatedTime = DateTime.Now; | ||
LastTime = CreatedTime; | ||
} | ||
|
||
public void Touch() | ||
{ | ||
LastTime = DateTime.Now; | ||
} | ||
} | ||
} |
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
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
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
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
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