0

我想使用应用程序缓存在我的 ASP.net 3.5 网站上创建一个应用程序范围的提要。我用来填充缓存的数据获取速度很慢,可能长达 10 秒(来自远程服务器的数据馈送)。我的问题/困惑是,构建缓存管理的最佳方法是什么。

private const string CacheKey = "MyCachedString";
private static string lockString = "";

public string GetCachedString()
{
    string data = (string)Cache[CacheKey];
    string newData = "";

    if (data == null)
    {
        // A - Should this method call go here?
        newData = SlowResourceMethod();

        lock (lockString)
        {
            data = (string)Cache[CacheKey];

            if (data != null)
            {
               return data;
            }

            // B - Or here, within the lock?
            newData = SlowResourceMethod();
            Cache[CacheKey] = data = newData;
        }
    }

    return data;
}

实际方法将由 HttpHandler (.ashx) 提供。

如果我在“A”点收集数据,我会缩短锁定时间,但最终可能会多次调用外部资源(来自所有试图引用提要的网页)。如果我把它放在'B'点,锁定时间会很长,我认为这是一件坏事。

最好的方法是什么,或者我可以使用更好的模式吗?

任何意见,将不胜感激。

4

1 回答 1

1

我在代码上添加注释。

private const string CacheKey = "MyCachedString";
private static readonly object syncLock = new object();

public string GetCachedString()
{
    string data = (string)Cache[CacheKey];
    string newData = "";

    // start to check if you have it on cache
    if (data == null)
    {
        // A - Should this method call go here?
        // absolut not here
        // newData = SlowResourceMethod();

        // we are now here and wait for someone else to make it or not
        lock (syncLock)
        {
            // now lets see if some one else make it...
            data = (string)Cache[CacheKey];

            // we have it, send it
            if (data != null)
            {
               return data;
            }

            // not have it, now is the time to look for it.
            // B - Or here, within the lock?
            newData = SlowResourceMethod();
            // set it on cache
            Cache[CacheKey] = data = newData;
        }
    }

    return data;
}

对我来说更好的是使用mutex和锁定取决于名称CacheKey,而不是锁定所有资源和非相关资源。使用互斥锁,一个基本的简单示例将是:

private const string CacheKey = "MyCachedString";
public string GetCachedString()
{
    string data = (string)Cache[CacheKey];
    string newData = "";

    // start to check if you have it on cache
    if (data == null)
    {
        // lock it base on resource key 
        //  (note that not all chars are valid for name)
        var mut = new Mutex(true, CacheKey);

        try
        {   
            // Wait until it is safe to enter.
            // but also add 30 seconds max
            mut.WaitOne(30000);

            // now lets see if some one else make it...
            data = (string)Cache[CacheKey];

            // we have it, send it
            if (data != null)
            {
               return data;
            }

            // not have it, now is the time to look for it.
            // B - Or here, within the lock?
            newData = SlowResourceMethod();
            // set it on cache
            Cache[CacheKey] = data = newData;

        }
        finally
        {
            // Release the Mutex.
            mut.ReleaseMutex();
        }    
    }

    return data;
}

您还可以
通过使用 ASP.NET 中的文件来阅读图像缓存问题

于 2013-06-06T12:21:49.127 回答