开发者

Remoting cache object

开发者 https://www.devze.com 2023-03-26 06:28 出处:网络
I\'m using the following code in a singleton remoting object MBRO. This function is only called on the server side.

I'm using the following code in a singleton remoting object MBRO. This function is only called on the server side.

''' <summary>
''' Return a cached DataCentricObject
''' </summary>
''' <created_by>CodingBarfield</created_by>
''' <date>04-08-2011</date>
Function DataCentricObjectName(ByVal intID As Integer) As String
    Try
        SyncLock dictDataCentricObject
            If Not dictDataCentricObject.ContainsKey(intID) Then
                Dim st As struct = dcLoader.LoadRecord(intID)
                dictDataCentricObject(intID) = st.Descript
            End If

            Return dictDataCentricObject(intID)
        End SyncLock

    Catch ex As Exception
        Throw New Exception("Error in GetTargName", ex)
    End Try
End Function


Private dictDataCentricObject As New Dictionary(Of Integer, String)
Dim dcLoader As New DataCentricObject

The LoadRecord function simply reads a line from a database table and copies the fields into 开发者_Python百科a little data structure.

The question

  • Is this code multithreading safe (in a remoting environment)
  • Are there any performance benefits for different code


It depends on what dcLoader.LoadRecord does. I'm going to take a guess that this just reads some data and does not update any state, I'm also assuming that other accesors of dictDataCentricObject lock on that object. If this is true then I think this code is thread-safe.

If dcLoader.LoadRecord was inexpensive you could do it before your lock to improve concurrency. However, I suspect it results in a call to the database so for overall performance it may be better to stay where it is. This does mean that calls to the function will serialise on access to a more expensive resource. If, and only if, this causes you a performance problem you could implement some caching around dcLoader.LoadRecord.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号