.net 基于DependencyKey清理缓存
根据datetime值,我将值插入缓存:.net 基于DependencyKey清理缓存,.net,vb.net,caching,.net,Vb.net,Caching,根据datetime值,我将值插入缓存: Dim Value1 As String = "value1" Dim Value2 As String = "value2" Dim Key1 As String = "Key" & New Date(2014, 1, 1, 1, 1, 0).ToString Dim Key2 As String = "Key" & New Date(2014, 1, 1, 1, 2, 0).ToString HttpContext.Current.C
Dim Value1 As String = "value1"
Dim Value2 As String = "value2"
Dim Key1 As String = "Key" & New Date(2014, 1, 1, 1, 1, 0).ToString
Dim Key2 As String = "Key" & New Date(2014, 1, 1, 1, 2, 0).ToString
HttpContext.Current.Cache.Insert(Key1, Value1)
HttpContext.Current.Cache.Insert(Key2, Value2)
HttpContext.Current.Cache.Remove("OtherKey")
是否可以使用另一个缓存项作为CacheDependency使这些缓存项无效
我尝试了以下方法,但不起作用:
Dim Keys As String() = New String(0) {}
Keys(0) = "OtherKey"
Dim MyDependency As New System.Web.Caching.CacheDependency(Nothing, Keys)
HttpContext.Current.Cache.Insert(Key1, Value1, MyDependency)
HttpContext.Current.Cache.Insert(Key2, Value2, MyDependency)
'To clear all cache items:
HttpContext.Current.Cache.Remove("OtherKey")
当我使用这个(没有remove语句)时,在缓存中永远找不到这些项。我在这里做错了什么?如果要将CacheDependency用于另一个缓存键:
- 必须在缓存中找到您所依赖的密钥(即OtherKey)(请参阅)。否则,将无法在缓存中找到您的项目
- 您需要为每个项创建不同的CacheDependency实例。否则,您将得到错误
尝试从多个缓存条目引用CacheDependency对象(请参阅的答案)
' Make sure OtherKey is found in the cache
HttpContext.Current.Cache.Insert("OtherKey", "SomeValue")
' Add the items with a different CacheDependency instance per item
Dim Keys As String() = New String(0) {}
Keys(0) = "OtherKey"
HttpContext.Current.Cache.Insert(Key1, Value1, New System.Web.Caching.CacheDependency(Nothing, Keys))
HttpContext.Current.Cache.Insert(Key2, Value2, New System.Web.Caching.CacheDependency(Nothing, Keys))
- 请注意,如果不使用key OtherKey添加缓存项,则将永远找不到依赖项
Dim Value1 As String = "value1"
Dim Value2 As String = "value2"
Dim Key1 As String = "Key" & New Date(2014, 1, 1, 1, 1, 0).ToString
Dim Key2 As String = "Key" & New Date(2014, 1, 1, 1, 2, 0).ToString
HttpContext.Current.Cache.Insert(Key1, Value1)
HttpContext.Current.Cache.Insert(Key2, Value2)
HttpContext.Current.Cache.Remove("OtherKey")
希望有帮助