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

  • SEARCH
  • Home
  • 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 4618144
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:13:28+00:00 2026-05-22T02:13:28+00:00

Long time listener – first time caller. I am hoping to get some advice.

  • 0

Long time listener – first time caller. I am hoping to get some advice. I have been reading about caching in .net – both with System.Web.Caching and System.Runtime.Caching. I am wondering what additional benefits I can get vs simply creating a static variable with locking. My current (simple minded) caching method is like this:

public class Cache
{
    private static List<Category> _allCategories;
    private static readonly object _lockObject = new object();

    public static List<Category> AllCategories
    {
        get
        {
            lock (_lockObject)
            {
                if (_allCategories == null)
                {
                    _allCategories = //DB CALL TO POPULATE
                }
            }
            return _allCategories;
        }
    }
}

Other than expiration (and I wouldn’t want this to expire) I am at a loss to see what the benefit of using the built in caching are.

Maybe there are benefits for more complex caching scenarios that don’t apply to me – or maybe I am just missing something (would not be the first time).

So, what is the advantage of using cache if I want a cache that never expires? Doesn’t static variables do this?

  • 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-22T02:13:29+00:00Added an answer on May 22, 2026 at 2:13 am

    First of all, Xaqron makes a good point that what you’re talking about probably doesn’t qualify as caching. It’s really just a lazily-loaded globally-accessible variable. That’s fine: as a practical programmer, there’s no point bending over backward to implement full-on caching where it’s not really beneficial. If you’re going to use this approach, though, you might as well be Lazy and let .NET 4 do the heavy lifting:

    private static Lazy<IEnumerable<Category>> _allCategories
        = new Lazy<IEnumerable<Category>>(() => /* Db call to populate */);
    
    public static IEnumerable<Category> AllCategories 
    { 
        get { return _allCategories.Value; } 
    }
    

    I took the liberty of changing the type to IEnumerable<Category> to prevent callers from thinking they can add to this list.

    That said, any time you’re accessing a public static member, you’re missing out on a lot of flexibility that Object-Oriented Programming has to offer. I’d personally recommend that you:

    1. Rename the class to CategoryRepository (or something like that),
    2. Make this class implement an ICategoryRepository interface, with a GetAllCategories() method on the interface, and
    3. Have this interface be constructor-injected into any classes that need it.

    This approach will make it possible for you to unit test classes that are supposed to do things with all the categories, with full control over which "categories" are tested, and without the need for a database call.

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

Sidebar

Related Questions

I’m a long time listener, but first time caller here… Hoping you can help
Long time listener, first time caller... In Access 2007 I have a column named
long time listener, first time caller. I have a matrix of icons that can
long time listener first time caller... I have this function (I don;t recall where
Long time listener, first time caller. I'm a 6 month old dev from Code
long time listener, first time caller... I am creating a splash screen derived from
long-time listener, first-time caller! I’m trying to connect to MySQL using the ADO.NET Entity
As they say on the radio - long time listener first time caller.... Here's
long time listener. First time caller... Not strictly a PHP question as it involves
Long-time listener, first-time caller. I'm relatively new to XPath and looked at several other

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.