c# - Cache.Insert 的委托(delegate)

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

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

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

最佳答案

像这样:

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

关于c# - Cache.Insert 的委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11199019/

相关文章:

asp.net - SEO:带有和不带有破折号 "/"和 ASP.NET MVC 的重复 URL

asp.net - 在 aspnet mvc 成员资格中获取当前登录的用户 ID

c# - 如何在被调用的方法中获取事件名称

c# - 使用带有委托(delegate)的线程池?

c# - 为什么 FormShown 事件不会在 C# 中触发?

c# - 为什么当我将 CommandParameter 设置为某个 Binding 时我的 ICommand.CanExecute(object) 参数为空,但当我将其设置为某个静态值时为非空?

asp.net - 在 Razor View 中发出未编码的字符串

objective-c - 在objective-c中直接调用delegate的方法

c# - 比较两个不同长度的整数数组时提高性能

c# - 从我的 Web API 调用第 3 方 Web API 时 Web 服务器挂起