委托Cache.Insert

时间:2012-06-25 23:52:30

标签: c# asp.net delegates callback cache-dependency

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

//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?
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
}

2 个答案:

答案 0 :(得分:1)

如你在代码中的评论中提到的那样传递回调绝对是有意义的。

只需改变:

public void getContent() {...}

为:

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

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

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

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

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

答案 1 :(得分: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();
}