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 6232149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:04:05+00:00 2026-05-24T10:04:05+00:00

In the following code i want to achieve this: every time a user asks

  • 0

In the following code i want to achieve this: every time a user asks for a specific piece of content, i first check if I already got this content from the DB on the same day.
If so – I shall return the cached content. If not – i first re-retrieve the content from the DB ,return it to the user and cache it for the next requests from this day, and so on ..
What i want to know is if it is a good code practice for saving (a lot of ) DB time on popular content . (I care less about how the code example looks, as just made it up for this question and it’s not going to be used as-is in my app ..)

the code example:

public class ContentCachingExample
{
    private static DateTime _lastRequestTime;
    private static MyContent _cachedContent;

    private static MyContent GetContent()
    {
        // compare dates - content will be re-retrieved from DB once a day.
        if (DateTime.Now.Date> _lastRequestTime.Date) 
        {
            _lastRequestTime = DateTime.Now;
            _cachedContent = GetContentFromDb();
        }
        return _cachedContent;
    }

    private static MyContent GetContentFromDb()
    {
        // get content from DB
    }

    public class MyContent
    {
        public string Property1 { get; set; }
        public int Property2 { get; set; }
    }
 }
  • 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-24T10:04:06+00:00Added an answer on May 24, 2026 at 10:04 am

    What you are doing is a common pattern.

    You should consider some of the libraries that exist, particularly if you ever need to fine-tune the caching mechanism.

    David has mentioned System.Web.Cache

    There is also Enterprise Library Caching Blocks

    However, if you only need the current capabilities of your code, then what you have is fine.

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

Sidebar

Related Questions

I have the following code that I want to achieve the following with. Check
In the following code I want to replace every occurrence of U.S.A with united
I have the following code I want to run, but the problem is $this->type
The following code does not want to compile. See the included error message. Code:
In following code, I want to extend the behaviour of a class by deriving/subclassing
The following code will generate a link to the page I want to get
I want to run the following code: ajaxUpdate(10); With a delay of 1 second
I want to use the following code to login to a website which returns
I want to execute python code from C# with following code. static void Main(string[]
I have the following code: [ [NSDate date] descriptionWithLocale: @yyyy-MM-dd ] I want it

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.