我正在考虑我的 asp.net 应用程序中的两种模式,我想通过静态方法或使用 asp.net 缓存来减少代码执行的开销。它们如下。
静态方法
Private Shared _transforms As New System.Collections.Generic.Dictionary(Of System.Type, MyObject)() Private Shared Function Load(theType As System.Type) As MyObject Dim lockAquired As Boolean If Not _transforms.ContainsKey(theType) Then Try lockAquired = Monitor.TryEnter(_synchInstanceLock, LOCK_TIME_OUT) '...DO MY MEMORY INTENSIVE TASK HERE _transforms.Add(theType, doc) Catch ex As Exception Throw (ex) Finally If lockAquired = True Then Monitor.Exit(_synchInstanceLock) End If End Try End If Return _transforms(theType)
结束功能
ASP.NET 缓存,注意:Cache 只是 ASP.NET Application[""] Cache 的包装器
Private Function Load(theTypeAs System.Type) As MyObject Dim doc As MyObject = Nothing Dim lockAquired As Boolean Try lockAquired = Monitor.TryEnter(_synchInstanceLock, LOCK_TIME_OUT) Dim key As String = GenerateCacheKey(GetType(MyObject), theType.Name) Dim cacheData As MyObject = TryCast(Cache(key), MyObject) If cacheData Is Nothing Then '...DO MY MEMORY INTENSIVE TASK HERE Cache.Insert(key, doc) Else doc = TryCast(cacheData, MyObject) End If Catch ex As Exception Throw (ex) Finally If lockAquired = True Then Monitor.Exit(_synchInstanceLock) End If End Try Return doc
结束功能
它适用于 .NET 3.5
有人可以建议在性能和线程安全方面哪个更好吗?
谢谢