gpt4 book ai didi

c# - Cache.Insert 委托(delegate)

转载 作者:太空宇宙 更新时间:2023-11-03 11:20:28 26 4
gpt4 key购买 nike

我希望能够在我的缓存更新回调中传递我的实际解析函数。如何使用委托(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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com