---
title: "How to Caching of Hashtable in MVC?"  
description: "How to Caching of Hashtable in MVC?"  
author: "Anonymous User"  
published: 2014-10-30  
updated: 2014-10-30  
canonical: https://www.mindstick.com/forum/2461/how-to-caching-of-hashtable-in-mvc  
category: "asp.net mvc"  
tags: ["c#", "asp.net mvc", "mvc4", "hashtable"]  
reading_time: 2 minutes  

---

# How to Caching of Hashtable in MVC?

I have a [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server), [simple](https://yourviews.mindstick.com/story/1469/5-simple-ways-to-stay-fit-amp-healthy) function from a [public](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers) class.\
That function creates an Hashtable object with information given from [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax), the part of his function like this:

```
try {       while ( dataReader.Read() )       {           Hashtable table1= new Hashtable();           Hashtable table2= new Hashtable();            table1.Add( dataReader["field1"].ToString(), Localization.English(dataReader["field1"]);            table2.Add( dataReader["field2"].ToString(), Localization.French(dataReader["field2"]);        }     }catch ( Exception e ){    Console.WriteLine( e.Message );}
```

\

I want to [cache](https://www.mindstick.com/blog/222/clearing-cache-in-asp-dot-net) these two Hashtables and use them in other classes. As I know, I cannot use HttpRuntime.Cache even if I used System.Web namespace.

I saw that HttpRuntime.Cache could be used in [Model class](https://www.mindstick.com/forum/160267/what-is-the-role-of-the-range-data-annotation-and-when-to-use-it-in-a-model-class).

Are there other way to cache these Hashtables ?

## Replies

### Reply by Elena Glibart

If you have two projects , like a Models or Entities project and your web project all you need to do is add a reference in the Models project to System.Web. If you did not add a reference to System.Web then you won't have access to HttpRuntime or HttpContect even though there is a namespace for System.Web.

After you do so, you'll have access to:

```
System.Web.HttpRuntime.Cache
```

and could use\

```
HttpRuntime.Cache.Insert(                            CacheKey,                            CacheValue,                            null,                            DateTime.Now.AddMinutes(CacheDuration),                            Cache.NoSlidingExpiration                        );                        Hashtable table1 = HttpRuntime.Cache[CacheKey] as Hashtable;                        HttpRuntime.Cache.Remove(CacheKey);
```

You'll also have access to\

```
System.Web.HttpContext.Current.Application
```

**and could use**\

```
System.Web.HttpContext.Current.Application.Add("table1",myHashTable);Hashtable table1 = System.Web.HttpContext.Current.Application["table1"] as Hashtable;
```


---

Original Source: https://www.mindstick.com/forum/2461/how-to-caching-of-hashtable-in-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
