哈希表中的asp.net静态缓存

发布于 2024-12-08 15:33:08 字数 2829 浏览 0 评论 0原文

我之前使用应用程序对象来缓存永不更改的数据。我正在重写该项目,并发现应用程序对象是一个禁忌,它只是为了经典 ASP 的遗留支持。

我知道我也可以使用缓存 - 但我不想这样做,因为我将其用于需要无效的数据。

因此,我正在研究静态数据的静态变量(有意义)。

我的问题是,我不是在每个类中指定静态变量,而是考虑使用哈希表来存储所有数据并将其包装到自己的类中 - 就像工厂一样。

像这样的事情:

''' <summary>
''' Methods and properties related to the access and management of the local static memory cache.
''' Fastest type of cache but not available across applications, web farm or web garden environments.
''' Use this cache when data is static or can be stale across application instances.
''' </summary>
''' <remarks></remarks>
Public Class LocalStaticCache

    'Internal data holder:
    Private Shared _objCache As Hashtable = Hashtable.Synchronized(New Hashtable)

    Private Sub New()
    End Sub

    ''' <summary>
    ''' Gets or sets an object in cache. Returns Nothing if the object does not exist.
    ''' </summary>
    ''' <param name="key">The name of the object.</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Property Item(key As String) As Object
        Get
            If String.IsNullOrEmpty(key) Then Return Nothing
            Return _objCache(key)
        End Get
        Private Set(value As Object)
            _objCache(key) = value
        End Set
    End Property

    ''' <summary>
    ''' Insert an object into the cache.
    ''' </summary>
    ''' <param name="key">The unique object key.</param>
    ''' <param name="value">The object to store in the cache.</param>
    ''' <remarks></remarks>
    Public Shared Sub Insert(key As String,
                             value As Object)
        If Not String.IsNullOrWhiteSpace(key) Then

            If _objCache.ContainsKey(key) Then
                'If the key already exists in the Cache it will overwrite only if the objects differ:
                Interlocked.CompareExchange(Item(key), value, value)
                Return
            End If

            'store the item to the cache:
            Item(key) = value
        End If
    End Sub

    ''' <summary>
    ''' Remove an object from the cache.
    ''' </summary>
    ''' <param name="key">The key of the object to remove.</param>
    ''' <remarks></remarks>
    Public Shared Sub Remove(key As String)
        If _objCache.ContainsKey(key) Then
            _objCache.Remove(key)
        End If
    End Sub

End Class

您认为将所有静态数据存储到哈希表中对于性能来说是一个好主意吗? 或者每个类在其类内都有自己的静态数据持有者会更好吗?

该线程安全吗?注意:我正在实现 Hashtable.Synchronized 和 Interlocked.CompareExchange 来防止竞争条件 - 但是锁定和争用呢?

请注意,一旦第一次设置数据,数据就永远不会更改(哈希表中的项目永远不需要更新)。

我有数据集和大块记录集作为内存流来存储。

有什么想法或指示吗?

谢谢。

I was previously using the application object to cache data that never changes. I am rewriting the project and have found out the aplication object is a no-no and is just there for legacy support from clasic ASP.

I know I can also use the cache - but I don't want to as I use that for data that needs to be invalidated.

I am therefore looking at static variables for static data (makes sense).

My question is, rather than specifying static variables in each class, I was thinking of using a hashtable to store all the data and wrapping it into its own class - like a factory.

Something like this:

''' <summary>
''' Methods and properties related to the access and management of the local static memory cache.
''' Fastest type of cache but not available across applications, web farm or web garden environments.
''' Use this cache when data is static or can be stale across application instances.
''' </summary>
''' <remarks></remarks>
Public Class LocalStaticCache

    'Internal data holder:
    Private Shared _objCache As Hashtable = Hashtable.Synchronized(New Hashtable)

    Private Sub New()
    End Sub

    ''' <summary>
    ''' Gets or sets an object in cache. Returns Nothing if the object does not exist.
    ''' </summary>
    ''' <param name="key">The name of the object.</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Property Item(key As String) As Object
        Get
            If String.IsNullOrEmpty(key) Then Return Nothing
            Return _objCache(key)
        End Get
        Private Set(value As Object)
            _objCache(key) = value
        End Set
    End Property

    ''' <summary>
    ''' Insert an object into the cache.
    ''' </summary>
    ''' <param name="key">The unique object key.</param>
    ''' <param name="value">The object to store in the cache.</param>
    ''' <remarks></remarks>
    Public Shared Sub Insert(key As String,
                             value As Object)
        If Not String.IsNullOrWhiteSpace(key) Then

            If _objCache.ContainsKey(key) Then
                'If the key already exists in the Cache it will overwrite only if the objects differ:
                Interlocked.CompareExchange(Item(key), value, value)
                Return
            End If

            'store the item to the cache:
            Item(key) = value
        End If
    End Sub

    ''' <summary>
    ''' Remove an object from the cache.
    ''' </summary>
    ''' <param name="key">The key of the object to remove.</param>
    ''' <remarks></remarks>
    Public Shared Sub Remove(key As String)
        If _objCache.ContainsKey(key) Then
            _objCache.Remove(key)
        End If
    End Sub

End Class

Do you think storing all static data into a hashtable is this a good idea for performance,
or would it be better for each class to have its own static data holders inside their class?

Is this thread safe? Note: I am implementing Hashtable.Synchronized and Interlocked.CompareExchange to prevent race conditions - but what about locking and contention?

Note, the data is never changed once it has been set the first time round (the items in the hashtable never need updating).

I have datasets and large chunks of record sets as memory streams to store.

Any thoughts or pointers?

thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

从来不烧饼 2024-12-15 15:33:08

您最好直接使用应用程序存储,而不是编写自己的静态哈希表并重新实现应用程序。

另一种选择是使用 ASP.NET Cache 对象,因为数据在不使用时可以删除。

从哈希表中添加和删除数据是线程安全的(仅从一个线程写入)。如果在 Application_Start 上初始化数据,则不必使用任何锁,因为数据不会更改。

如果数据永远不应该被删除,我会根据数据的上下文将数据存储在不同的类中。为此目的,使用多个带有延迟初始化的单例类。

对于新应用程序,我将不再使用数据集(或记录集?)。您最好使用实体框架,您可以在其中基本上生成数据访问层。

希望这有帮助。

Instead of writing your own static Hashtable and reimplementing Application you better use the Application Storage directly.

Another option is to use the ASP.NET Cache object, because the data can be deleted when not used.

Adding and removing data from a Hashtable is thread safe (write from one thread only). If you initialize the data on Application_Start you don't have to use any locks because the data does not change.

If the data never should be deleted I would store the data in different classes depending on the context of the data. Use several singleton classes with lazy initialisation for this purpose.

For new applications I would not use datasets (or recordsets?) any longer. You better use the entity framework where you can basically generate your data access layer.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文