csharp Caching objects in Application

//using method:
public static List<string> GetStrings(bool refresh)
{
	if (refresh || System.Web.HttpContext.Current.Application["Strings"] == null)
	{
		List<string> strings = MyClass.GetStrings();//hit database to retrieve list
		System.Web.HttpContext.Current.Application["Strings"] = strings;//store list in cache
		return strings;
	}
	else
	{
		try
		{
			return (List<string>)System.Web.HttpContext.Current.Application["Strings"];
		}
		catch
		{
			//need to perform if block again
		}
	}
}

//using property:
private static List<string> _Strings;
public static List<string> Strings
{
	get {
		if (_Strings == null)
		{
			_Strings = GetStrings();
		}

		return _Strings;
	}
}
The above demonstrates caching objects in the Application and also Lazy Loading.

Updated: Tuesday 22nd May 2012, 08:31pm

There are 0 comments

Leave a comment of your own

Comments are currently closed.