我听过并读过一个字符串无法更改(不可变?).我想这应该是正确的.但我也听说过两个内容相同的字符串共享相同的内存空间(或者你称之为内存空间).它是否正确?
如果是这样,这是否意味着如果我创建一个包含数千个字符串的List,如果大多数字符串彼此相等,那么根本不会占用太多空间?
我试图弄清楚,已经提出了与我的ImageProcessor库的问题在这里添加项目到缓存中,当在那里我得到间歇性文件访问错误.
System.IO.IOException:进程无法访问文件'D:\ home\site\wwwroot\app_data\cache\0\6\5\f\2\7\065f27fc2c8e843443d210a1e84d1ea28bbab6c4.webp',因为它正被另一个进程使用.
我编写了一个类,用于基于散列网址生成的密钥执行异步锁定,但似乎我在实现中遗漏了一些东西.
我的锁类
public sealed class AsyncDuplicateLock
{
/// <summary>
/// The collection of semaphore slims.
/// </summary>
private static readonly ConcurrentDictionary<object, SemaphoreSlim> SemaphoreSlims
= new ConcurrentDictionary<object, SemaphoreSlim>();
/// <summary>
/// Locks against the given key.
/// </summary>
/// <param name="key">
/// The key that identifies the current object.
/// </param>
/// <returns>
/// The disposable <see cref="Task"/>.
/// </returns>
public IDisposable Lock(object key)
{
DisposableScope releaser = new DisposableScope(
key,
s =>
{
SemaphoreSlim locker; …Run Code Online (Sandbox Code Playgroud) 当用户输入query以搜索:
如果用户执行的搜索时间为10秒,并且他们不耐烦地刷新页面,我们不希望它再次启动查询.这应该被锁定.
但是,如果运行昂贵的查询,我们不希望锁定执行较便宜搜索的其他用户.
要解决这个问题,我需要多个锁.
这就是我目前实现它的方式:
private static readonly object MasterManualSearchLock = new object();
private static readonly Dictionary<string, object> ManualSearchLocks = new Dictionary<string, object>();
/// <summary>
/// Search the manual
/// </summary>
public static SearchResponse DoSearch(string query, Manual forManual)
{
var tokens = Search.Functions.TokeniseSearchQuery(query);
var tokenHash = Search.Functions.GetUniqueHashOfTokens(tokens);
var cacheIndex = Settings.CachePrefix + "SavedManualSearch_" + tokenHash;
var context = HttpContext.Current;
if (context.Cache[cacheIndex] == null)
{
// Create lock if it doesn't exist …Run Code Online (Sandbox Code Playgroud)