Search code examples
c#asp.netdelegatescallbackcache-dependency

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

Solution

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