当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Delegate for Cache.Insert

问题描述:

I want to be able to pass in my actual parsing function in my cache update callback. How can I optimize my code duplication below using delegates? Thanks

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

^^^^^^^^^^^^^^^^^^^^^^^^^^

}

网友答案:

Like this:

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();
}
网友答案:

It absolutely does make sense to pass in the call back as you mention in the comment in your code.

Just change:

public void getContent() {...}

to:

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

And change itemUpdateCallBack to take Func<TypeOfValue> parsecallback in as an argument too.

From outside, you need something like this:

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

Either that, or you could pass it into your constructor. It is less flexible but is perfect for situations where you know that this will always be the same for a given object.

分享给朋友:
您可能感兴趣的文章:
随机阅读: