在ASP.NET运行时创建动态锁 [英] Creating Dynamic Locks at Runtime in ASP.NET

查看:126
本文介绍了在ASP.NET运行时创建动态锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是以下假设适用于该code?我把code下的一些背景资料,但我不认为这是相关的。

Are the following assumptions valid for this code? I put some background info under the code, but I don't think it's relevant.

假设1:因为这是一个单一的应用程序,我正在做它由一个单一的过程中处理的假设。因此,静态变量线程之间共享,并宣布我的锁定对象的集合静态有效。

Assumption 1: Since this is a single application, I'm making the assumption it will be handled by a single process. Thus, static variables are shared between threads, and declaring my collection of lock objects statically is valid.

假设2:如果我知道这个值已经在字典中,我不需要锁定上阅读。我可以用一个ConcurrentDictionary,但我相信这将是一个安全的,因为我不枚举(或删除),并且该值会存在,而不是当我打电话更改 UnlockOnValue()

Assumption 2: If I know the value is already in the dictionary, I don't need to lock on read. I could use a ConcurrentDictionary, but I believe this one will be safe since I'm not enumerating (or deleting), and the value will exist and not change when I call UnlockOnValue().

假设3:我可以在领取钥匙锁,因为这引用不会改变,即使底层的数据结构确实

Assumption 3: I can lock on the Keys collection, since that reference won't change, even if the underlying data structure does.

private static Dictionary<String,Object> LockList = 
    new Dictionary<string,object>();

private void LockOnValue(String queryStringValue)
{
    lock(LockList.Keys)
    {
        if(!LockList.Keys.Contains(queryStringValue))
        {
            LockList.Add(screenName,new Object());
        }
        System.Threading.Monitor.Enter(LockList[queryStringValue]);
    }
}

private void UnlockOnValue(String queryStringValue)
{
    System.Threading.Monitor.Exit(LockList[queryStringValue]);
}

那我就用这个code,如:

Then I would use this code like:

LockOnValue(Request.QueryString["foo"])
//Check cache expiry
//if expired
    //Load new values and cache them.
//else
    //Load cached values
UnlockOnValue(Request.QueryString["foo"])

背景:我创建了基于查询字符串一个用户定义的变量下载数据在ASP.NET应用程序。值的数目将是相当有限的。我需要缓存每个值的结果在指定的时间段。

Background: I'm creating an app in ASP.NET that downloads data based on a single user-defined variable in the query string. The number of values will be quite limited. I need to cache the results for each value for a specified period of time.

办法:我决定使用本地文件来缓存数据,这是不是最好的选择,但我想尝试它,因为这是不关键的性能是不是一个大问题。我用每份期权2个文件,其中一个缓存失效日期,和一个与数据。

Approach: I decided to use local files to cache the data, which is not the best option, but I wanted to try it since this is non-critical and performance is not a big issue. I used 2 files per option, one with the cache expiry date, and one with the data.

问题:我不知道该怎么锁定的最佳方式是什么,我不是太熟悉.NET线程问题(的原因之一,我选择了这个方法)。基于什么是可用的,和我读,我觉得上面应该工作,但我不知道,想要第二个意见。

Issue: I'm not sure what the best way to do locking is, and I'm not overly familiar with threading issues in .NET (one of the reasons I chose this approach). Based on what's available, and what I read, I thought the above should work, but I'm not sure and wanted a second opinion.

推荐答案

您当前的解决方案看起来pretty不错。两件事情我会改变:

Your current solution looks pretty good. The two things I would change:

1:UnlockOnValue需要在finally块去。如果抛出一个异常,它永远不会释放锁。

1: UnlockOnValue needs to go in a finally block. If an exception is thrown, it will never release its lock.

2:LockOnValue有些低效的,因为它确实字典查找两次。这不是什么大不了的小字典,但对于较大的,你会想切换到TryGetValue。

2: LockOnValue is somewhat inefficient, since it does a dictionary lookup twice. This isn't a big deal for a small dictionary, but for a larger one you will want to switch to TryGetValue.

另外,你的假设持有3 - 至少目前是这样。但字典合同不保证该键的属性总是返回相同的对象。而且由于它是如此容易不靠这个,我会建议反对。每当我需要一个对象来锁定,我只是创建为唯一目的的对象。是这样的:

Also, your assumption 3 holds - at least for now. But the Dictionary contract makes no guarantee that the Keys property always returns the same object. And since it's so easy to not rely on this, I'd recommend against it. Whenever I need an object to lock on, I just create an object for that sole purpose. Something like:

private static Object _lock = new Object();

这篇关于在ASP.NET运行时创建动态锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆