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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:17:43+00:00 2026-05-19T17:17:43+00:00

At my office, it has been deemed that we are to put our AppSettings

  • 0

At my office, it has been deemed that we are to put our AppSettings for the Web.Config in the database. As such, I created the following, but have some doubts about a couple aspects of the code.

So my question is:
The line containing “Cache cache = new Cache()” in the UTILITY class is probably wrong because it creates a NEW cache object.

Q: So, what should I be doing for that line?

…any help is appreciated.

The overall objective was to be able to make a call like this:

Utility.GetConfigurationValue(ConfigurationSection.AppSettings, "myVariable");

…and have it retrieve from the cache or the database auto-magically.

THE UTILITY CODE:

public static class Utility
{
    #region "Configurations"

    public static String GetConfigurationValue(ConfigurationSection section, String key)
    {
        Configurations config = new Configurations();
        Cache cache = new Cache(); // <--- This is probably wrong!!!!

        if (!cache.TryGetItemFromCache<Configurations>(out config))
        {
            config.List(SNCLavalin.US.Common.Enumerations.ConfigurationSection.AppSettings);
            cache.AddToCache<Configurations>(config, DateTime.Now.AddMinutes(15));
        }

        var result = (from record in config
                      where record.Key == key
                      select record).FirstOrDefault();

        return (result == null) ? null : result.Value;
    }

    #endregion
}

THE EXTENSION CODE:

public static class Extensions
{
    #region "System.Web.Caching"

    public static void Remove<T>(this Cache cache) where T : class
    {
        cache.Remove(typeof(T).Name);
    }
    public static void AddToCache<T>(this Cache cache, object item, DateTime absoluteExpiration) where T : class
    {
        T outItem = null;
        if (cache.TryGetItemFromCache<T>(out outItem))
            return;

        cache.Insert(typeof(T).Name,
                item,
                null,
                absoluteExpiration,
                System.Web.Caching.Cache.NoSlidingExpiration,
                System.Web.Caching.CacheItemPriority.Normal,
                null);
    }
    public static bool TryGetItemFromCache<T>(this Cache cache, out T item) where T : class
    {
        item = cache.Get(typeof(T).Name) as T;
        return item != null;
    }

    #endregion
}

THE LIST-CLASS CODE:

public class Configurations : List<Configuration>
{
    #region CONSTRUCTORS

    public Configurations() : base()
    {
        initialize();
    }
    public Configurations(int capacity) : base(capacity)
    {
        initialize();
    }
    public Configurations(IEnumerable<Configuration> collection) : base(collection)
    {
        initialize();
    }

    #endregion

    #region PROPERTIES & FIELDS

    private Crud _crud;

    #endregion

    #region EVENTS
    #endregion

    #region METHODS

    private void initialize()
    {
        _crud = new Crud("CurrentDbConnection");
    }

    public Configurations List(ConfigurationSection section)
    {
        using (DbCommand dbCommand = _crud.Db.GetStoredProcCommand("spa_LIST_SecConfiguration"))
        {
            _crud.Db.AddInParameter(dbCommand, "@Section", DbType.String, section.ToString());

            _crud.List(dbCommand, PopulateFrom);
        }

        return this;
    }

    public void PopulateFrom(DataTable table)
    {
        this.Clear();

        foreach (DataRow row in table.Rows)
        {
            Configuration instance = new Configuration();
            instance.PopulateFrom(row);
            this.Add(instance);
        }
    }

    #endregion
}

THE ITEM-CLASS CODE:

public class Configuration
{
#region CONSTRUCTORS

public Configuration()
{
    initialize();
}

#endregion

#region PROPERTIES & FIELDS

private Crud _crud;

public string Section { get; set; }
public string Key { get; set; }
public string Value { get; set; }

#endregion

#region EVENTS
#endregion

#region METHODS

private void initialize()
{
    _crud = new Crud("CurrentDbConnection");
    Clear();
}

public void Clear()
{
    this.Section = "";
    this.Key = "";
    this.Value = "";
}
public void PopulateFrom(DataRow row)
{
    Clear();

    this.Section = row["Section"].ToString();
    this.Key = row["Key"].ToString();
    this.Value = row["Value"].ToString();
}

#endregion

}

  • 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-19T17:17:44+00:00Added an answer on May 19, 2026 at 5:17 pm

    You have correctly identified the problem – the current code creates a new Cache instance on each call to GetConfigurationValue which defeats the purpose of caching. You need to make the Cache instance static rather than creating a new instance each time.

    public static class Utility
    {
        private static Cache cache = new Cache();  // Static class variable
    
        #region "Configurations"
    
        public static String GetConfigurationValue(ConfigurationSection section, String key)
        {
            Configurations config = new Configurations();
            // Cache cache = new Cache(); --- removed
    
            ...
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a split access 2007 database that has been running fine on a
OK folks, here's a fun one for you. So our back office system has
My office mate created a web service application to be consumed by a .Net
I work in an office which has been doing Agile for a while now.
We have an ASP.NET 3.5 application that has been in production for over a
We have been going back and forth a lot around our office lately about
I have a document which has been sloppily authored. It's a dictionary that contains
I feel that my professional exposure has been biased towards Visio, by the choice
Imagine that I have a model that describes the printers that an office has.
I know google drive has a sync folder now and that most office documents

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.