VB.NET属性获取和缓存

时间:2016-12-30 13:58:46

标签: vb.net performance properties encapsulation

如果我在VB.NET中使用这种形状的属性:

   <body>
    <div class="megawrapper">
      <div class="home-page">
       </div>
    </div>
   </body>

.megawrapper {
    position: absolute;
    overflow-x: hidden;
    overflow-y: hidden;
    width: 100%;
    height: 100%;
}

.home-page {    
    padding: 30px;
    background-image: url(images/street.jpg);
    background-position: 50% 50%;
    background-size: cover;
    background-repeat: no-repeat;
}

并且代码调用很多次属性获得相同的i(在对另一个iscen执行相同操作之后),结果将被缓存,直到我使用新的i或将它每次重新计算?

谢谢!

2 个答案:

答案 0 :(得分:1)

VB.NET中没有自动缓存来存储重复计算的结果。由您来提供某种缓存。

例如,您可以使用词典

Public Sub PerformCopy()
    MkDir "D:\To\" & Format(Date, "dd-mm-yyyy")
    CopyData "E:\From\", "D:\To\ & Format(Date, 'dd-mm-yyyy')& '\'"
End Sub

Public Sub CopyData(ByVal FromPath As String, ByVal ToPath As String)
Dim FSO As Object
Dim Fdate As Date
Dim FileInFromFolder As Object
Dim FolderInFromFolder As Object
    Set FSO = CreateObject("scripting.filesystemobject")
    'First loop through files
    For Each FileInFromFolder In FSO.getfolder(FromPath).Files
        Fdate = Int(FileInFromFolder.DateLastModified)

    If Fdate >= Date - 3 Then
        FileInFromFolder.Copy ToPath
    End If

    'Next loop throug folders
    For Each FolderInFromFolder In FSO.getfolder(FromPath).SubFolders
        CopyData FolderInFromFolder.Path, ToPath
    Next FolderInFromFolder
    Next
End Sub

当然,作为任何简单的解决方案,都需要考虑以下几点:

  • 没有失效规则(缓存仍然有效 应用程序的生命周期)
  • 假设计算产生的结果总是相同的 相同的输入

答案 1 :(得分:1)

您可以为缓存值创建一个类

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

注意:此代码未经测试,如果存在语法错误,请注释或编辑而不是downvote。