Public Class ClassControlValueCache

    Private Shared Property Cache As New Dictionary(Of String, Dictionary(Of String, DataTable))

    Public Shared Function LoadFromCache(formId As Integer, controlId As Integer) As DataTable

        Dim dict As Dictionary(Of String, DataTable) = GetCachedFormDict(formId.ToString())

        If IsNothing(dict) Then
            Return Nothing
        Else

            Dim dt As DataTable = GetCachedControlDict(dict, controlId.ToString())

            If IsNothing(dt) Then
                Return Nothing
            Else
                Return dt
            End If

        End If

    End Function

    Public Shared Function SaveToCache(formId As Integer, controlId As Integer, dt As DataTable) As DataTable

        Dim dict As Dictionary(Of String, DataTable) = GetCachedFormDict(formId.ToString())

        If IsNothing(dict) Then
            dict = SetCachedFormDict(formId.ToString(), New Dictionary(Of String, DataTable))
        End If

        SetCachedControlDict(dict, controlId.ToString(), dt)
        Return dt
    End Function


    Private Shared Function GetCachedFormDict(formId As Integer) As Dictionary(Of String, DataTable)

        If Cache.ContainsKey(formId.ToString()) Then
            Return Cache.Item(formId.ToString())
        Else
            Return Nothing
        End If

    End Function

    Private Shared Function SetCachedFormDict(formId As Integer, dict As Dictionary(Of String, DataTable)) As Dictionary(Of String, DataTable)
        Cache.Item(formId.ToString()) = dict
        Return dict
    End Function

    Private Shared Function GetCachedControlDict(dict As Dictionary(Of String, DataTable), controlId As Integer) As DataTable

        If dict.ContainsKey(controlId.ToString()) Then
            Return dict.Item(controlId.ToString())
        Else
            Return Nothing
        End If

    End Function

    Private Shared Function SetCachedControlDict(dict As Dictionary(Of String, DataTable), controlId As Integer, dt As DataTable) As DataTable
        dict.Item(controlId.ToString()) = dt
        Return dt
    End Function

End Class
