循环遍历ASP.NET缓存对象中的键
发布时间:2020-05-24 15:08:33 所属栏目:asp.Net 来源:互联网
导读:ASP.NET中的缓存看起来像使用某种关联数组: // Insert some data into the cache:Cache.Insert(TestCache, someValue);// Retrieve the data like normal:someValue = Cache.Get(TestCache);// But, can be done associativ
|
ASP.NET中的缓存看起来像使用某种关联数组: // Insert some data into the cache:
Cache.Insert("TestCache",someValue);
// Retrieve the data like normal:
someValue = Cache.Get("TestCache");
// But,can be done associatively ...
someValue = Cache["TestCache"];
// Also,null checks can be performed to see if cache exists yet:
if(Cache["TestCache"] == null) {
Cache.Insert(PerformComplicatedFunctionThatNeedsCaching());
}
someValue = Cache["TestCache"];
如您所见,对缓存对象执行空检查非常有用. 但是我想实现一个可以清除缓存值的缓存清除功能 任何人都可以帮我找出一种循环存储缓存键的方法 static void DeleteMatchingCacheKey(string keyName) {
// This foreach implementation doesn't work by the way ...
foreach(Cache as c) {
if(c.Key.Contains(keyName)) {
Cache.Remove(c);
}
}
}
解决方法从任何集合类型中删除项目时不要使用foreach循环 – foreach循环依赖于使用枚举器,该枚举器不允许您从集合中删除项目(如果迭代的集合具有项目,则枚举器将抛出异常添加或删除它).使用简单的while循环缓存键: int i = 0;
while (i < Cache.Keys.Length){
if (Cache.Keys(i).Contains(keyName){
Cache.Remove(Cache.Keys(i))
}
else{
i ++;
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 哪里是.ASPXAUTH cookie
- asp.net-mvc – 在ASP.NET MVC中的NHibernate会话管理
- 使用VSCode,DNX和kestrel运行第一个ASP.NET 5应用程序会导致
- ASP.NET RadioButton混淆名称(组名)
- asp.net – 将tracelistener添加到web.config
- asp.net – 在资源文件中存储SQL查询是不好的做法吗?
- asp.net-mvc-3 – StringLength属性行为
- 在剃刀mvc 4 rc清空第一行
- asp.net-mvc – ASP.NET MVC 4邮政编码验证
- asp.net-core – ASP.NET Core WebAPI默认路由不起作用
推荐文章
站长推荐
- asp.net-mvc – 具有html帮助的布尔隐藏和隐藏
- asp.net – 返回新的RedirectResult()vs返回Redi
- asp.net – 错误:类型存在于两个目录中
- asp.net – ASPXGridView ClientSideEvents如何获
- asp.net-mvc – 将JSON.NET JObject转换为JsonRe
- asp.net – ReportViewer阻止其他功能,直到报告查
- asp.net – 在Visual Studio编辑器中配置%%块的
- ASP.NET应用程序通过Windows身份验证或表单身份验
- asp.net-mvc – 为什么我的视图模型中的内部成员
- asp.net – 多久我应该打开/关闭我的Booksleeve连
热点阅读
