简体   繁体   中英

Caching in asp.net?

Recently a question raised in my group. I have a sections list in database.it is nothing but names of sections like Dubai section, NA section, Canada Section. This list updated very rarely in the database.

Now the question, Is there any mechanism in asp.net that we can keep the list in Server Cache memory and refresh the cache every 24 hours? Why group need is, they dont want call DB all the time. Just they want read it from Server memory.

Please share me if we have that kind of facility in asp.net.

examples and blogs will be more helpful.

I would create a user control just for the purpose of displaying this list of Sections and use the outputcache directive on the control:

On the first line of this user control, have something like:

<% @ OutputCache Duration="86400" VaryByParam="none" %> 

*Duration is given on seconds above.

You could use the application cache . eg

// Add an object with a cache of 1 day.
Cache.Insert("CacheItem6", "Cached Item 6",
    null, DateTime.Now.AddDays(1), 
    System.Web.Caching.Cache.NoSlidingExpiration);

Straight from MSDN -- Caching .

Then to retrieve your value, you could implement the following:

string cachedString;
cachedString = (string)Cache["CacheItem6"];

You can also get a notification when the object is removed .

You can do that with Cache object. More info here .

You probably need something like this:

Cache.Insert("DSN", connectionString, null, DateTime.Now.AddHours(24), Cache.NoSlidingExpiration);

This will cache connectionString object under a key named DSN for 24 hours.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM