Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 基于DependencyKey清理缓存_.net_Vb.net_Caching - Fatal编程技术网

.net 基于DependencyKey清理缓存

.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

根据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.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添加缓存项,则将永远找不到依赖项
然后,当您从缓存中删除OtherKey时,依赖项将自动删除。因此,此行将自动从缓存中删除Key1和Key2:

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")
希望有帮助