Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6653027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:12:58+00:00 2026-05-26T01:12:58+00:00

I was previously using the application object to cache data that never changes. I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:12:58+00:00Added an answer on May 26, 2026 at 1:12 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Jeff has previously blogged about using the cache to perform "out of band" processing
Can I access the ASP.NET Cache object from Application_Start? I was previously using it
Currently I am loading data from a previous session into my application using the
I'm developing a graph control in WPF. I have previously developed it using GDI
I have previously written user interfaces using .NET and Windows Forms. I'm about to
Using VB 6 Once I run the project, How to display a previously selected
I would like to decrypt a file that I previously encrypted with C# using
I have an application that consists of a WPF UI with an infrastructure layer
I am developing a REST API layer for my application using Zend_Rest_Controller. I have
Previously all application level resources in a project I am authoring were stored in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.