应用程序缓存和缓慢的过程 [英] Application Cache and Slow Process

查看:125
本文介绍了应用程序缓存和缓慢的过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的应用程序缓存我ASP.net 3.5的网站创建一个应用广泛的饲料。我使用填充缓存中的数据是缓慢的获得,也许长达10秒(从远程服务器的数据饲料)。我的问题/困惑是,什么是构建高速缓存管理的最佳途径。

I want to create an application wide feed on my ASP.net 3.5 web site using the application cache. The data that I am using to populate the cache is slow to obtain, maybe up to 10 seconds (from a remote server's data feed). My question/confusion is, what is the best way to structure the cache management.

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;
}

实际的方法将被$ P $由psented和HttpHandler的(ashx的)。

The actual method would be presented by and HttpHandler (.ashx).

如果我收集点'A'的数据,我把锁的时间短,但最终可能会调用外部资源多次(从网页上的所有尝试引用饲料)。如果我把它点'B',锁定时间会很长,这我假设是一件坏事。

If I collect the data at point 'A', I keep the lock time short, but might end up calling the external resource many times (from web pages all trying to reference the feed). If I put it at point 'B', the lock time will be long, which I am assuming is a bad thing.

什么是最好的办法,或者是有,我可以用一个更好的模式?

What is the best approach, or is there a better pattern that I could use?

任何意见将是AP preciated。

Any advice would be appreciated.

推荐答案

我想补充的code的意见。

I add the comments on the code.

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;
}

我最好是使用互斥和锁定取决于名称 CacheKey ,而不是锁定所有的资源和非相对的。随着互斥一种碱性简单的例子是:

Better for me is to use mutex and lock depended on the name CacheKey and not lock all resource and the non relative one. With mutex one basic simple example will be:

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;
}

您还可以读取结果
<一href=\"http://stackoverflow.com/questions/10554928/image-caching-issue-by-using-files-in-asp-net\">Image在ASP.NET中使用文件缓存的问题

这篇关于应用程序缓存和缓慢的过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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