VB.NET 属性 获取并缓存
VB.NET property get and cache
如果我在 VB.NET 中使用 属性 得到这个形状:
Public ReadOnly Property Thing(i as long) as double(,)
Get
Return SomeCalculation(i )
End Get
End Property
并且代码调用 多次 次 属性 得到相同的 i (在对另一个 iscen 等执行相同操作之后),结果是否会被缓存直到我使用新的 i 还是每次都会重新计算?
谢谢!
没有VB.NET中没有自动缓存来存储重复计算的结果。是否提供某种缓存取决于您。
例如,您可以使用字典
Dim cache As Dictionary(Of Long, Double(,)) = New Dictionary(Of Long, Double(,))
Public ReadOnly Property Thing(i as long) as double(,)
Get
Dim result As Double(,)
If Not cache.TryGetValue(i, result) Then
result = SomeCalculation(i)
cache.Add(i, result)
End If
Return result
End Get
End Property
当然,作为任何简单的解决方案,有几点需要考虑:
- 没有失效规则(缓存在
应用程序的生命周期)
- 它假设计算总是产生相同的结果
相同的输入
您可以为缓存值创建 class
Public Class LongCaches(Of MyType)
Protected MyDictionary Dictionary(Of Long, MyType) = New Dictionary(Of Long, MyType)
Public Delegate Function MyFunction(Of Long) As MyType
Protected MyDelegate As MyFunction
Public Calculate As Function(ByVal input As Long) As MyType
If Not MyDictionary.ContainsKey(input) Then
MyDictionary(input) = MyFunction.Invoke(input)
End If
Return MyDictionary(input)
End Function
Public Sub New(ByVal myfunc As MyFunction)
MyDelegate = myfunc
End Sub
End Caches
您需要像这样使用它:
Private _MyLongCacheProperty As LongCaches(Of Double(,))
Protected ReadOnly MyLongCacheProperty(i As Long) As LongCaches
Get
If _MyLongCacheProperty Is Nothing Then
_MyLongCacheProperty = New LongCaches(Of Double(,))(AddressOf SomeCalculation)
End If
Return _MyLongCacheProperty.Calculate(i)
End Get
End Property
注意:此代码未经测试,如有语法错误,请评论或编辑,不要投反对票。
如果我在 VB.NET 中使用 属性 得到这个形状:
Public ReadOnly Property Thing(i as long) as double(,)
Get
Return SomeCalculation(i )
End Get
End Property
并且代码调用 多次 次 属性 得到相同的 i (在对另一个 iscen 等执行相同操作之后),结果是否会被缓存直到我使用新的 i 还是每次都会重新计算?
谢谢!
没有VB.NET中没有自动缓存来存储重复计算的结果。是否提供某种缓存取决于您。
例如,您可以使用字典
Dim cache As Dictionary(Of Long, Double(,)) = New Dictionary(Of Long, Double(,))
Public ReadOnly Property Thing(i as long) as double(,)
Get
Dim result As Double(,)
If Not cache.TryGetValue(i, result) Then
result = SomeCalculation(i)
cache.Add(i, result)
End If
Return result
End Get
End Property
当然,作为任何简单的解决方案,有几点需要考虑:
- 没有失效规则(缓存在 应用程序的生命周期)
- 它假设计算总是产生相同的结果 相同的输入
您可以为缓存值创建 class
Public Class LongCaches(Of MyType)
Protected MyDictionary Dictionary(Of Long, MyType) = New Dictionary(Of Long, MyType)
Public Delegate Function MyFunction(Of Long) As MyType
Protected MyDelegate As MyFunction
Public Calculate As Function(ByVal input As Long) As MyType
If Not MyDictionary.ContainsKey(input) Then
MyDictionary(input) = MyFunction.Invoke(input)
End If
Return MyDictionary(input)
End Function
Public Sub New(ByVal myfunc As MyFunction)
MyDelegate = myfunc
End Sub
End Caches
您需要像这样使用它:
Private _MyLongCacheProperty As LongCaches(Of Double(,))
Protected ReadOnly MyLongCacheProperty(i As Long) As LongCaches
Get
If _MyLongCacheProperty Is Nothing Then
_MyLongCacheProperty = New LongCaches(Of Double(,))(AddressOf SomeCalculation)
End If
Return _MyLongCacheProperty.Calculate(i)
End Get
End Property
注意:此代码未经测试,如有语法错误,请评论或编辑,不要投反对票。