Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpRuntime Cache vs. static dictionary/fields

What are the main pros and cons for using HttpRuntime Cache against using simple static field?

I need to store data in scope of entire ASP.NET application.

HttpRuntime.Cache["MyData"] = someHashtable;

vs.

private static System.Collections.Hashtable _myData;
public static System.Collections.Hashtable MyData
{
    get
    {
        if (_myData == null)
        {
            _myData = new System.Collections.Hashtable();
            // TODO: Load data
        }
        return _myData;
    }
}
like image 790
Radek Stromský Avatar asked Aug 03 '11 07:08

Radek Stromský


1 Answers

Objects in HttpRuntime.Cache have unknown expiry periods unless explicitly set (meaning that objects can expire any time), whereas objects within your HashTable live for as your application pool is alive (unless you manually remove an entry). The HttpRuntime.Cache also allows you to set various other characteristics, such as (optional) cache item priority and expiry time.

like image 181
foxy Avatar answered Sep 23 '22 11:09

foxy