2

我希望能够在我的缓存更新回调中传递我的实际解析函数。如何使用委托优化下面的代码重复?谢谢

//intial setup code
public void getJSONContent() //can I pass itemUpdateCallback in here? Does it make sense?
{

    Content = (String)HttpContext.Current.Cache[Path];

    if (Content == null)
    {                
        Content = parseXMLContent();

        HttpContext.Current.Cache.Insert(
        key,
        Content,
        new CacheDependency(Path),
        Cache.NoAbsoluteExpiration,
        Cache.NoSlidingExpiration,    
        jsonUpdateCallback); //callback in the event of my file in cache has changed
        ^^^^^^^^^^^^^^^^^^

    }
}

private void jsonUpdateCallback(string key, CacheItemUpdateReason reason, out object value, out CacheDependency dependency, out DateTime exipriation, out TimeSpan slidingExpiration)
{
    dependency = new CacheDependency(key);
    exipriation = Cache.NoAbsoluteExpiration;
    slidingExpiration = Cache.NoSlidingExpiration;
    value = jsonXMLContent(); //how can pass this function into here, so I can can have different parse functions using the same code?
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
}

//intial setup code
public void getXMLContent() //can I pass itemUpdateCallback in here? Does it make sense?
{

    Content = (String)HttpContext.Current.Cache[Path];

    if (Content == null)
    {                
        Content = parseXMLContent();

        HttpContext.Current.Cache.Insert(
        key,
        Content,
        new CacheDependency(Path),
        Cache.NoAbsoluteExpiration,
        Cache.NoSlidingExpiration,    
        xmlUpdateCallback); //callback in the event of my file in cache has changed
        ^^^^^^^^^^^^^^^^^^

    }
}

private void xmlUpdateCallback(string key, CacheItemUpdateReason reason, out object value, out CacheDependency dependency, out DateTime exipriation, out TimeSpan slidingExpiration)
{
    dependency = new CacheDependency(key);
    exipriation = Cache.NoAbsoluteExpiration;
    slidingExpiration = Cache.NoSlidingExpiration;
    value = parseXMLContent(); //how can pass this function into here, so I can can have different parse functions using the same code?
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
}
4

2 回答 2

1

正如您在代码中的注释中提到的那样,传递回调绝对是有意义的。

只是改变:

public void getContent() {...}

到:

public void getContent(Func<TypeOfValue> parsecallback) {...}

并将 itemUpdateCallBack 更改为也将Func<TypeOfValue> parsecallback其作为参数。

从外面,你需要这样的东西:

Func<TypeOfValue> func = () =>
{
    MethodCall1();
    MethodCall2();
    return MethodCall3();
};
myObj.getContent(func);

要么,要么你可以将它传递给你的构造函数。它不太灵活,但非常适合您知道对于给定对象始终相同的情况。

于 2012-06-26T00:22:46.893 回答
1

像这样:

public void getXMLContent()
{
    getContent(parseXmlContent);
}

public void getContent(Func<string> parseContent)
{

    Content = (String)HttpContext.Current.Cache[Path];

    if (Content == null)
    {                
        Content = parseContent();

        HttpContext.Current.Cache.Insert(
        key,
        Content,
        new CacheDependency(Path),
        Cache.NoAbsoluteExpiration,
        Cache.NoSlidingExpiration,    
        delegate(string key2, CacheItemUpdateReason reason, out object value, out CacheDependency dependency, out DateTime expiration, out TimeSpan slidingExpiration) {
            itemUpdateCallback(key2, reason, parseContent, out value, out dependency, out expiration, out slidingExpiration);
        }); 
    }
}

private void itemUpdateCallback(string key, CacheItemUpdateReason reason, Func<string> parseContent, out object value, out CacheDependency dependency, out DateTime exipriation, out TimeSpan slidingExpiration)
{
    dependency = new CacheDependency(key);
    exipriation = Cache.NoAbsoluteExpiration;
    slidingExpiration = Cache.NoSlidingExpiration;
    value = parseContent();
}
于 2012-06-26T00:26:22.123 回答