リストのオブジェクトhttpcontext.ます。キャッシュ
-
23-08-2019 - |
質問
があるので、同じキャッシュのためのすべてのオブジェクトのキャッシュ?感動的なオブジェクトの作成とい定期的にリストパージ出し物がいなくなります。
解決
はい、あなたはどちらかの指標キャッシュキーに基づいてすることができ、またはあなたが内容を反復処理することができます:
For Each c In Cache
' Do something with c
Next
' Pardon my VB syntax if it's wrong
他のヒント
var keysToClear = (from System.Collections.DictionaryEntry dict in HttpContext.Cache
let key = dict.Key.ToString()
where key.StartsWith("Something_")
select key).ToList();
foreach (var key in keysToClear)
{
HttpContext.Cache.Remove(key);
}
あなたは、オブジェクトを列挙することができます:
System.Web.HttpContext.Current.Cache.GetEnumerator()
ここでVBの関数は、キャッシュを反復処理し、DataTableの表現を返すことです。
Private Function CreateTableFromHash() As DataTable
Dim dtSource As DataTable = New DataTable
dtSource.Columns.Add("Key", System.Type.GetType("System.String"))
dtSource.Columns.Add("Value", System.Type.GetType("System.String"))
Dim htCache As Hashtable = CacheManager.GetHash()
Dim item As DictionaryEntry
If Not IsNothing(htCache) Then
For Each item In htCache
dtSource.Rows.Add(New Object() {item.Key.ToString, item.Value.ToString})
Next
End If
Return dtSource
End Function
このビットが使用した、以下のコードを簡単に対して繰り返し処理を実行すべてのキャッシュの項目をいくつかのカスタムロジックの撤去キャッシュの項目に含まれる特定のstringにつけられた名前が使われている。
私もバージョンコードVB.Net どのC#.
VB.Net 版
Dim cacheItemsToRemove As New List(Of String)()
Dim key As String = Nothing
'loop through all cache items
For Each c As DictionaryEntry In System.Web.HttpContext.Current.Cache
key = DirectCast(c.Key, String)
If key.Contains("prg") Then
cacheItemsToRemove.Add(key)
End If
Next
'remove the selected cache items
For Each k As var In cacheItemsToRemove
System.Web.HttpContext.Current.Cache.Remove(k)
Next
C#版
List<string> cacheItemsToRemove = new List<string>();
string key = null;
//loop through all cache items
foreach (DictionaryEntry c in System.Web.HttpContext.Current.Cache)
{
key = (string)c.Key;
if (key.Contains("prg"))
{
cacheItemsToRemove.Add(key);
}
}
//remove the selected cache items
foreach (var k in cacheItemsToRemove)
{
System.Web.HttpContext.Current.Cache.Remove(k);
}
あなたは潜在的に<のhref =「http://msdn.microsoft.com/en-us/library/system.web.caching.cache.aspx」のrel =「nofollowをnoreferrer」から項目を削除することにしたいので、 > Cache
のオブジェクトこれは、繰り返し処理中に除去することを許可していないので、(IEnumerable
として)それを反復するためにひどく便利ではありません。ただし、インデックスによって項目にアクセスすることができないことを考えると、それが唯一のソリューションです。
LINQのビットは、しかし、問題を単純化することができます。次のようなものを試してください:
var cache = HttpContext.Current.Cache;
var itemsToRemove = cache.Where(item => myPredicateHere).ToArray();
foreach (var item in itemsToRemove)
cache.Remove(itemsToRemove.Key);
反復における各item
タイプの DictionaryEntry
であることに注意してくださいのます。
ジェフだったらまで依存関係のごキャッシュされます。それは適切な方法を行っています。論理的にグループごキャッシュされたデータ項目と設定の依存関係を用。このように要な時に必要な期限が切グループ全体のタッチすることで、お客様の依存関係およびそれらはすべて消えました。
なんなのか理解している リストのオブジェクト ます。